I write the active.ksh script (based on expect) in order to login automatically to some Solaris machine and execute the hostname command (login to virtual IP in order to verify which hostname is the active machine – I have two cluster solaris machines )
The problem is with expect; expect sends the password string (pass123) and it misses the Password question, and it still waits for the password.
So actually the password (pass123) was entered after password question. On most cases the expect script works fine but sometimes it missed the password.
EXAMPLE OF THE PROBLEM
./active.ksh
spawn ssh 10.10.18.61
sh: /usr/local/bin/stty: not found
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is provided only for authorized uss
Password: * my remark - pass123 string was missed the Password Question pass123
Password:
THE SCRIPT
more active.ksh
#!/bin/ksh
VIP_ADDRESS=10.10.18.61
expect_for_verify_which_active_machine=`cat << EOF
set timeout -1
spawn ssh $VIP_ADDRESS
expect {
")?" { send "yes\r" ; exp_continue }
Password: {send "pass123\r"}
}
expect > {send "hostname\r"}
expect > {send exit\r}
expect eof
EOF`
expect -c "$expect_for_verify_which_active_machine"
EXAMPLE OF RIGHT RESULTS
./active.ksh
[Friday, February 24, 2012 2:32:06 PM IST] INFO Verify which is active SMU machine
spawn ssh 10.10.18.61
sh: /usr/local/bin/stty: not found
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is provided only for authorized uss
yes
Password:
Last login: Fri Feb 24 14:32:06 2012 from smu1a
This computer system, including all related equipment, networks and network devices (specifically including Internet access),is provided only for authorized uss
solaris1:/ ROOT > hostname
solaris1
solaris1:/ ROOT > exit
logout
Connection to 10.10.18.61 closed.
Maybe it will be easier to script this process using empty. It’s a small utility similar to expect but more convenient in some cases. You can use it to just “type text into an application”, and you can ignore the application’s prompts (though you don’t have to). Logging to a remote machine and executing
hostnamecould look like this:The first one starts your command in the background. The other one sends it data (your password) from stdin. The whole session (including passwords!) is logged to file
templogwhere you will find also the result ofhostname. There are also mechanisms for finer-grained control, but they may be a bit harder to set up.As some people already pointed out, ssh keys are the way to go, this is just an ugly and unsafe workaround (it’s risky due to shell history being preserved, users being able to see your password in
psresults, and in my example the password is additionally saved in thetemplog fileetc).