Sending commands via SSH with expect script
In a another question I was kindly provided with a solution to a dilemma I
was having with expect and if/else statements (basically, my lack of
knowledge in how they're written). The script I was given works with one
exception: If I cleanly connect to a remote host the exit command is not
sent which will close that connection and move onto the next host in the
list.
This is the functional part of the script:
while {[gets $file1 host] != -1} {
puts $host
spawn -noecho ssh $host
expect {
"continue connecting" {
send "yes\r"
expect {
"current" {
send -- $pw2\r
exp_continue
}
"New password" {
send -- $pw1\r
exp_continue
}
"Retype new password" {
send -- $pw1\r
exp_continue
}
msnyder
}
send "exit\r"
}
}
interact
}
When I'm connected to the host I expect to see the standard Linux prompt:
[username@host ~]$. At least, that's our standard prompt. The prompt is
preceded by a banner. Might that be throwing a kink into this? I wouldn't
think so since I'm only looking for "continue connecting" from the RSA
fingerprint prompt and that is within text spread across multiple lines. I
would think that expect is intelligent enough to ignore any scrolling text
and only look at text in prompts that are looking for input.
I considered passing the exit command as an argument for the ssh $host
command, but expect doesn't seem to like that.
Can someone now assist me in getting the exit command to be sent properly?
No comments:
Post a Comment