Shutdown multiple computers via ssh, try different passwords
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Shutdown multiple computers via ssh, try different passwords
I'm trying to write a mass reboot script in the event of a power outage (servers go to UPS for some 15 minutes or so and shutdown uncleanly).
What I have is a set of 6 common passwords which will be prompted for per ssh connection, is there any way I can cycle through this password list
Code:
#below is an example of how this is constructed, IP's are used instead of hostnames due to the possibility of a DNS server being offline.
SERVERSRM1="1 2 3"
SERVERSRM2="110 120 130"
for i in $SERVERSRM1; do ssh -n root@192.168.0.$i "hostname && shutdown -y -i5 -g0"; done;'
for i in $SERVERSRM2; do ssh -n root@192.168.0.$i "hostname && shutdown -y -i5 -g0"; done;'
Each time the code is executed I'm prompted for a password (it's always one of 6 passwords). It would be quicker to have the passwords be cycled through.
Can anyone help. I realise this is basic code at the moment but it will eventually allow for a specific set of computers to be shut down based on what power supply they're using.
authorized keys not really an option given the scale of computers (some 300 or so).
Last edited by genderbender; 10-01-2010 at 06:24 AM.
Right, seems like I need to use except which I've got working quite well...
I just have one problem now concerning expect, some of my ssh logins have a motd banner, is there anyway I can make expect ignore these banners and continue typing text? You'll have to forgive how crap I am with except, it's a new tool for me so I'm not great with it (I don't intend to use it again due to difficulties I've had running it).
Just out of curiosity what type of UPS are you using and are they connected via serial or USB to the servers? The two UPS tools that I am familiar with i.e. apcupsd and NUT have a network capability so one computer can shutdown all the rest without having to manually send commands.
Just out of curiosity what type of UPS are you using and are they connected via serial or USB to the servers? The two UPS tools that I am familiar with i.e. apcupsd and NUT have a network capability so one computer can shutdown all the rest without having to manually send commands.
APC smartups, servers are connected by iec cables, however there's snmp on the smartups's, so the second they switch on I've got some time to shut everything down with my rather crap script, lol. This is uninterruptable power supply, i.e in the event of power loss switch to battery power, server's aren't informed of the power loss as such, they just continue working unaware.
You should be able to set up the server so that it is aware that the UPS is on. The last time I did this the APC UPS came with a serial cable for this very task.
BTW it is possible to use the same public/private key on all of the servers.
I assume that there is a RS-232 or USB port in addition to ethernet so you could run apcupsd on one server to control all of the others via the network.
I have not played with expect much so can't help you there.
Wrote an except script which takes three variables, username, password and server.
Made a bash script which cycles through all the addresses in the whole building.
Any time it failed to login I wrote down the IP's and assumed they were using a different password and created a seperate function for the next set of IP's.
Continued doing this until i had 6 functions with different passwords in.
Made a script which runs each of the functions.
Still can't get past the banners though, if theres' a banner I cant execute any commands and have to wait until the connection times out
on each server create a new user without shell, and add it to the sudoer file like:
shutdowner ALL=(ALL) NOPASSWD: /sbin/shutdown
then the script to shutdown all your server at once is:
Code:
for IP in $SERVER_IP; do
sshpass -p $PASSWD ssh -t shutdowner@$IP "sudo /sbin/shutdown -h now"
done
I'd suggest checking out somethign like func. no ssh, no passwords, no login shells, just commands like "func server*.domain.com call command run shutdown" to blanket shut down all servers matching a hostname pattern. doddle.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.