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.

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.

Have used Parallel SSH before.
A tutorial here:
Execute commands simultaneously on multiple servers Using PSSH/Cluster SSH/Multixterm*|*Ubuntu Geek

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!
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.
for remote shutdown, try
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.Code:for f in ip1 ip2 ip3 ... ; do ssh $f "shutdown -h now&exit;" &done
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.
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...).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
Instead of the shutdown, you can distribute the keys this way (imaging saves typing the root password repeatedly too).
This can also work for thin clients. Just copy the SSH key into the chroot in the NFS filesystem of the clients.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
e.g.SSH is the Swiss army knife of GNU/Linux system administration.Code:cat /root/.ssh/id_rsa.pub >> /opt/ltsp/i386/root/.ssh/authorized_keys //append to key file in chroot.
Last edited by pogson; 4th December 2011 at 03:41 PM.

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
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
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.
morganw (7th December 2011)
There are currently 1 users browsing this thread. (0 members and 1 guests)