Running commands on a gob of Linux servers? No problem
(originally written around May 15th or so)
Today I am sitting in Phoenix at one of my employer’s datacenter locations. One of the first tasks that I got when I arrived was to log in via SSH to just over 500 customer servers and run a few commands to gather inventory information, including CPU speed, # of CPU’s, and OS version.
Now, when you look at that task with the mindset of “I have to log in to all of those?”, you will probably not be too happy about the work ahead of you. However, if you get a little creative and start looking for ways to avoid such massive repetition, you might quickly come to a solution similar to what I came up with.
I ended up using the expect command for SSH, since I did not have any SSH keys installed, and I simply could not sit and paste in passwords for hours. I got the local sysadmin to install the ‘expect’ package on our central shell server, and whipped up a quick little script to do my work for me. Keep in mind that this script will likely not apply to something you are doing, because this particular case was a customer running FreeSSH on Windows servers, but it will give you the general idea of how much you can do with so little code:
#!/usr/local/bin/expect
# =============================================================
# Filename: GetInfo.exp
# Description: Echo's the output of systeminfo Windows command
# =============================================================
# Get variables from argv
set remote_server [lindex $argv 0]
# Spawn SSH session
spawn /usr/bin/ssh -o "StrictHostKeyChecking=no" user@$remote_server "systeminfo"
expect {
"assword:" {
set timeout 60
send -- "root_password_here\r"
send -- "\r"
}
}
expect eof
# EOF