Jump to content

Recommended Posts

Posted
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.
Posted
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!
Posted

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.

Posted (edited)

for remote shutdown, try

 

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.

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).

 

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.

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.

Edited by pogson
Posted
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
Posted

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

  • Thanks 1
Posted

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.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...