remote shutdown
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2009
    Location
    9ja://coalcity
    Posts
    259

    remote shutdown

    I need to be able to remotely shut down 2-or-more PCs from one Laptop on a LAN network. How should I go about it? (meanwhile, the network is pinging fine ... )

  2. #2
    Join Date
    Jun 2005
    Location
    UK
    Posts
    4,426

    Re: remote shutdown

    Hello twohot

    The way I do it here is to use ssh.

    In a terminal window:

    Code:
    ssh <network identity of remote machine>
    That demands the password of the user you are using and logging in as. It assumes you have the same user identity/password on both machines.

    Then:

    Code:
    su -
    to gain root control of the remote machine and finally:

    Code:
    shutdown -h now
    will shut the remote machine down.

    Code:
    shutdown -r now
    will reboot it.

    Hope that helps.

  3. #3
    Join Date
    Jun 2009
    Location
    9ja://coalcity
    Posts
    259

    Re: remote shutdown

    Well, since the PCs are not physically far from each other (less than 2 metres) and are few in number, it would take less time to reach out and press the power button (followed by "enter"). I was thinking of something more automated ... unless there is a way to issue the ssh & shutdown commands to all PCs, simultaneously.

  4. #4
    Join Date
    Jun 2007
    Location
    Lake Mary, Florida
    Age
    54
    Posts
    1,088

    Re: remote shutdown

    Hello,
    You can, but it would require ssh with root ID And that is not a good idea
    Could setup a cron job to shutdown the servers/PC

  5. #5
    Join Date
    Nov 2008
    Location
    ~
    Age
    32
    Posts
    175

    Re: remote shutdown

    Setting up an RSA public key you can avoid typing the password (beware that this has security implications in case someone get access to your private key).

    Then on the remote machines you can set sudo to allow your non-root user tu run shutdown without password, with something like this in /etc/sudoers

    Code:
    user  ALL=NOPASSWD: /sbin/shutdown
    To shutdown all the machines you will just run
    Code:
    for ip in 192.168.1.1 192.168.1.2 192.168.1.3; do
     ssh user@$ip -i ~/.ssh/id_rsa "sudo shutdown -h now"
    done

  6. #6
    Join Date
    Jun 2009
    Location
    9ja://coalcity
    Posts
    259

    Re: remote shutdown

    Quote Originally Posted by aesir
    Setting up an RSA public key you can avoid typing the password (beware that this has security implications in case someone get access to your private key).

    Then on the remote machines you can set sudo to allow your non-root user tu run shutdown without password, with something like this in /etc/sudoers

    Code:
    user  ALL=NOPASSWD: /sbin/shutdown
    To shutdown all the machines you will just run
    Code:
    for ip in 192.168.1.1 192.168.1.2 192.168.1.3; do
     ssh user@$ip -i ~/.ssh/id_rsa "sudo shutdown -h now"
    done
    Interesting. I'll check that out.
    Wondering what the first line of the "for" statement will look like for a larger network.

    ---------- Post added at 10:57 AM CDT ---------- Previous post was at 10:52 AM CDT ----------

    Quote Originally Posted by scott32746
    Hello,
    You can, but it would require ssh with root ID And that is not a good idea
    Could setup a cron job to shutdown the servers/PC
    ... that is, if there is a way to execute a particular job at request. In that case, one should be able to access cron jobs remotely from a server. How is that done? It will be interesting to know how to schedule a shutdown at idle time ... this is one task no one has addressed to my satisfaction.

  7. #7
    Join Date
    Nov 2008
    Location
    ~
    Age
    32
    Posts
    175

    Re: remote shutdown

    Quote Originally Posted by twohot
    Interesting. I'll check that out.
    Wondering what the first line of the "for" statement will look like for a larger network.
    to run on the whole 192.168.0.0/16 block
    Code:
    for c in `seq 0 255`; do 
     for d in `seq 0 255`; do 
      ip="192.168.$c.$d";
      ...
     done
    done
    if you have a hard-to-generate pattern you can put the list of ip in a text file and read from it
    Code:
    while read ip; do
     ...
    done < iplist
    Quote Originally Posted by twohot
    ... that is, if there is a way to execute a particular job at request. In that case, one should be able to access cron jobs remotely from a server. How is that done? It will be interesting to know how to schedule a shutdown at idle time ... this is one task no one has addressed to my satisfaction.
    I wonder why you would remotely activate a cron job, which as the name suggest is supposed to automatically run on scheduled times; and it would be harder to implement than just running the command with ssh.

    To shutdown at idle time you can create a cron job with a script that runs "shutdow -h now" when some condition is true, for example
    Code:
    avgload=$(echo "scale=0; `cat /proc/loadavg | cut -d " " -f 3`/0.01" | bc)
    [ $avgload -le 30 ] && shutdown -h now

  8. #8
    Join Date
    Jun 2009
    Location
    9ja://coalcity
    Posts
    259

    Thumbs up Re: remote shutdown

    Very Coool

    It is a good time to learn bash tricks.
    Thanks ... for helping a scripting novice. Much appreciated! I'll decode at my pace ... and report back when I hit the wall

  9. #9
    Join Date
    Jun 2009
    Location
    9ja://coalcity
    Posts
    259

    Re: remote shutdown

    Hi People!

    I finally gave your suggestions a critical look (I had little or no bash experience before). I was able to extract bits of code to construct what I needed. Here are a few of my observations:

    Aesir's code ...
    Code:
    for ip in 192.168.1.1 192.168.1.2 192.168.1.3; do
     ssh user@$ip -i ~/.ssh/id_rsa "sudo shutdown -h now"
    done
    ... Is lacking a "-t" switch for the ssh which removes the need for tty. Sudo requires tty over ssh by default, forcing the client to type passwords -- which is what we don't want. Again, for a proper RSA (keys) authentication setup the "-i" switch is not necessary after using ssh-agent. Apart from those, this is cool. Thanks Aesir!
    I found that iterating through a large network with "for-do-done" loop makes for a slow execution especially if some hosts are unavailable (negotiation time lapses).

    SO, .... I'm using:
    Code:
    while read ip; do
     ...
    done < iplist

    Its fast for a network of 10-30 PCs

  10. #10
    Join Date
    Oct 2008
    Posts
    498

    Re: remote shutdown

    Hi,

    A different approach: install apcupsd and run it in slave mode on the desired servers. Set up an apcupsd in master mode on your workstation. Then use the apcupsd to shut down the remote servers.

    Or, more broadly speaking, any RPC client-server should do your job.

    WWell,

  11. #11
    Join Date
    Sep 2010
    Location
    China
    Posts
    75

    Re: remote shutdown

    Quote Originally Posted by aesir
    Setting up an RSA public key you can avoid typing the password (beware that this has security implications in case someone get access to your private key).

    Then on the remote machines you can set sudo to allow your non-root user tu run shutdown without password, with something like this in /etc/sudoers

    Code:
    user  ALL=NOPASSWD: /sbin/shutdown
    To shutdown all the machines you will just run
    Code:
    for ip in 192.168.1.1 192.168.1.2 192.168.1.3; do
     ssh user@$ip -i ~/.ssh/id_rsa "sudo shutdown -h now"
    done
    How interesting!^^^

Similar Threads

  1. Shutdown button through remote sessions: dangerous and stupid
    By cuban_cigar in forum Reviews, Rants & Things That Make You Scream
    Replies: 1
    Last Post: 26th May 2010, 08:06 PM
  2. Replies: 1
    Last Post: 18th March 2008, 06:00 PM
  3. Replies: 5
    Last Post: 19th November 2007, 12:00 PM
  4. Replies: 2
    Last Post: 12th February 2007, 07:07 PM
  5. How to hide FC6 shutdown messages or use shutdown spash
    By renjoyhenry in forum Using Fedora
    Replies: 0
    Last Post: 5th November 2006, 06:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •