I have a expect script that fires commands on remote devices, some of the devices can have credentials issues for which I need to skip is the password prompt repeats after entering the password.
#!/usr/bin/expect -f
set x [list 192.168.1.1 192.168.1.2]
foreach hosts $x {
# spawn ssh sy-nep-oss@$x
sleep 5
spawn ssh -o StrictHostKeyChecking=no sy-nep-oss@$hosts
sleep 5
**expect {"password: "
send -- "abcd\r"
expect "*>"
If the password is incorrect, need to skip this loop iteration and move on to the next item in the list
send -- "cd cfcard:/ \r"
sleep 2
expect "*>"
send -- "cd cfcard2:/ \r"
sleep 2
expect "*>"
sleep 2
send -- "dir log* \r"
expect "*>"
sleep 2
send "cd log \r"
expect {enter code here
"Error: Wrong path *" {
send "cd logfile \r"
}
"logfile*" {
send "cd log \r"
}
}
expect "*>"
sleep 2
send -- "dir log.log \r"
expect "*>"
sleep 2
send -- "system-view \r"
expect "Enter system view*"
sleep 2
send -- "sftp 212.72.10.182 \r"
expect "*username*"
sleep 2
send -- "test\r"
expect "*password:"
sleep 2
send -- "Y\r"
sleep 3
send -- "N \r"
sleep 4
expect "Please select public key type for user authentication*"
send -- "R \r"
sleep 2
expect "*password"
sleep 2
expect "sftp-client>"
sleep 2
send -- "put log.log $hosts.log \r "
sleep 2
expect "sftp-client>"
sleep 2
send -- "quit\r"
sleep 2
send -- "quit\r"
sleep 2
send -- "quit\r"
}
expect eof