i am trying to write an except script to ssh into a list of devices and run some commands, and i came across this problem, not every device is alive, which breaks the script, my script looks like this
#!/usr/bin/expect
# set defaults
set device [lindex $argv 0]
set user "admin"
set password "test123"
set list [open $device r]
set resultfile [open setlog.log w]
set timeout 10
trap exit SIGINT
log_file -a -noappend setlog_logfile.log
# ssh to each IP on the list
while {[gets $list line] != -1} {
set pid [spawn ssh -o "ConnectTimeout 3" admin@$line]
expect "password:"
send "$password\r"
expect eof
expect "#"
send "shell\r"
****some commands here****
send "exit\r"
expect eof
puts $resultfile "$line, Worked!"
log_user 1
wait
}
following is the error message i got, apparently expect runs across a dead ip and it times out and *continue* to run the next command in the script, how can i fix this? thanks
$ ./testrun /tmp/testip
spawn ssh -o ConnectTimeout 3 admin@192.168.1.2
ssh: connect to host 192.168.1.2 port 22: Connection timed out
send: spawn id exp9 not open
while executing
"send "$password\r" "
("while" body line 4)
invoked from within