+ Post New Thread
Results 1 to 9 of 9
*nix Thread, Remote Shutdown in Technical; Does anybody have a script which will send a remote shutdown signal to a list of machines ip's? Just configuring ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,817
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350

    Remote Shutdown

    Does anybody have a script which will send a remote shutdown signal to a list of machines ip's? Just configuring the APC powerchute to shutdown remote machines.

  2. #2


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339

  3. #3

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,817
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350
    I'm not going to be manually running the script so not sure if it will work, i think its going to be easier to install the apc software on each machine and say when the card says shutdown shutdown!

  4. #4

    Join Date
    Jan 2008
    Location
    Cheshire, UK
    Posts
    340
    Thank Post
    68
    Thanked 60 Times in 42 Posts
    Rep Power
    48
    You could write a script in Expect (Expect), it lets you automate things like ssh, telnet, etc, which would otherwise be interactive. I will see if I can find a script I was playing with to shut our iMacs down. I never actually used it in anger as I realised that by the time I would be running the script they would have gone to sleep and would not respond.

    PM me it I don't get back to you in a day or so.

  5. #5

    Join Date
    Dec 2011
    Location
    Canada
    Posts
    6
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0
    for remote shutdown, try

    Code:
    for f in ip1 ip2 ip3 ... ; do ssh $f "shutdown -h now&exit;" &done
    You need to set up passwordless logins by doing ssh-keygen as root on the system somewhere and copying /root/.ssh/id_rsa.pub into /root/.ssh/authorized_keys on each client you wish to control.

    One way to make the list of IPs is to create a directory in /root/ with the IP addresses as directories or filename, e.g.
    Code:
    cd /root/
    mkdir IPs
    cd IPs
    touch 192.168.0.34
    for ((f=4;f=f-1;));do ((g=f+13));echo $f $g;done //generates a sequence of values
    for ((f=4;f=f-1;));do ((g=f+13));echo  $g;touch 192.168.0.$g; done //generates a sequence of empty files 192.168.0.14-16
    for f in *;do ssh $f "shutdown -h now&exit;" & done // executes shutdown on the list of IPs
    The process is very fast, a fraction of a second per PC. The shutdowns run in parallel once started. It's a great way to get students' attention at the end of class or whatever. It can be scripted with crontab, for example to occur at end of day/class (dangerous to shutdown visiting teachers PC, however...).

    Instead of the shutdown, you can distribute the keys this way (imaging saves typing the root password repeatedly too).

    Code:
    for f in *;  do cat /root/.ssh/id_rsa.pub | ssh $f "cat >> /root/.ssh/authorized_keys";done //Type root password once for each client visited
    This can also work for thin clients. Just copy the SSH key into the chroot in the NFS filesystem of the clients.

    e.g.
    Code:
    cat /root/.ssh/id_rsa.pub >> /opt/ltsp/i386/root/.ssh/authorized_keys //append to key file in chroot.
    SSH is the Swiss army knife of GNU/Linux system administration.
    Last edited by pogson; 4th December 2011 at 03:41 PM.

  6. #6

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,817
    Thank Post
    272
    Thanked 1,138 Times in 1,034 Posts
    Rep Power
    350
    But will this not ask for auth to run it? I'm looking for a script to be run for a one off when the UPS tells one server its on battery then run a script to shutdown others

  7. #7


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    Quote Originally Posted by glennda View Post
    But will this not ask for auth to run it? I'm looking for a script to be run for a one off when the UPS tells one server its on battery then run a script to shutdown others
    use ssh without a password
    SSH login without password

    same with parallel ssh

  8. #8

    Join Date
    Dec 2011
    Location
    Canada
    Posts
    6
    Thank Post
    0
    Thanked 1 Time in 1 Post
    Rep Power
    0
    The clients accept the key offered by the requesting machine if it matches what's in /root/.ssh/authorized_keys. If it's the UPS sending the command, that command will be executed by some user on some computer. Let it be root or someone with a key that the clients accept as from root. Typically the UPS will send a "shutdown" command to execute on a particular host. Just replace it with the command you need to run ssh to each other client. You may also want to shut machines down in a certain order, so instead of a loop, use a sequence of commands with delays between:

    ssh able "shutdown -h now&exit;"// The web applicaton
    sleep 15
    ssh baker "shutdown -h now&exit;" //The database
    sleep 15
    shutdown -h now //the machine local to the UPS

    You can put the sequence of commands in some file, say, /root/shutdown_servers
    Have as the first line #!/bin/bash
    commands
    ...

    Then instead of shutdown -h now, have the UPS run /root/shutdown_servers
    Make the script executable and runnable by root
    chown root:root /root/shutdown_servers
    chmod 550 /root/shutdown_servers

  9. #9

    Join Date
    May 2009
    Posts
    9
    Thank Post
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    0
    We use dsh (distributed ssh) for remote shutdown of all clients in our lab.
    You just need an ssh public key on every Host. You can create groups and run every command on many hosts.
    like: "dsh -c -g clients apt-get upgrade -y" to upgrade all hosts in the clients group.

    for ups remote shutdown you could use apcupsd, it works like powerchute from apc, but it is included in ubuntu.

  10. Thanks to lownoize from:

    morganw (7th December 2011)

SHARE:
+ Post New Thread

Similar Threads

  1. Terminal Client Remote shutdown
    By galetony01 in forum Thin Client and Virtual Machines
    Replies: 2
    Last Post: 3rd July 2008, 09:11 AM
  2. Disabling the remote shutdown command.
    By IT_Man_Dan in forum Windows
    Replies: 15
    Last Post: 15th November 2007, 01:31 PM
  3. Remote shutdown
    By Kained in forum Windows
    Replies: 9
    Last Post: 28th August 2007, 06:44 PM
  4. Remote Shutdown Query
    By Kained in forum How do you do....it?
    Replies: 10
    Last Post: 27th April 2007, 08:57 AM
  5. Remotely Shutdown/Restart PC's at night
    By flyinghaggis in forum How do you do....it?
    Replies: 20
    Last Post: 2nd February 2006, 09:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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