I would use a simple script to loop through the list of servers, and an expect script to do the actual logon and run the commands.
The logic of the script would look something like this:
for every $entry in servers.list
docall the expect script with $entry as an argument, perhaps also a username and a password, redirect output to a logfile
done
Here's an implementation of the above as a bash script:
Code:
#!/bin/bash
set login=user
set password=MyPassword
set server_list=./servers.lst
set expect_script=./my_expect_script
set logfile=./data.log
cat $server_list | while read $servername ; do
expect $expect_script $servername $login $password >> $logfile
done
This is a rough draft and completely untested. At the very least, you would want to add some lines to the log file before and after calling the expect script to indicate which server the information is coming from.
As for the expect script, the logic of that could be something like this:
- grab server name and login details from the command line arguments
- suppress output
- connect to the server with ssh
- wait for the password prompt
- send the password
- wait for the shell prompt
- enable output
- send "sudo df -h"
- wait for the shell prompt
- send "sudo hostname"
- wait for the shell prompt
- suppress output
- send "logout"
Here's an implementation of the above (not guaranteed to be bug free):
Code:
set hostname [lindex $argv 0]
set login [lindex $argv 1]
set password [lindex $argv 2]
set timeout 20
log_user 0
spawn ssh $login@$hostname
expect "password:" {
send "$password\r"
}
expect "~$" {
log_user 1
send "sudo df -h\r"
}
expect "~$" {
send "sudo hostname\r"
}
log_user 0
send "logout\r"