Reputation: 6120
I am quite new to Expect and am looking for some suggestions on this one:
I am trying to ssh to the servers and execute some commands.
If the server is one that I am logging into for the first time, it prompts me whether I want to trust it. I select "yes"; this is why I have the first expect
. The second expect
is for the password.
When I do the following, it accepts either the first prompt or the second prompt, so this does not work for me.
expect {
"yes/no" {
send "yes\n"
}
"assword: " {
send "$mypass\n"
}
}
When I do the following, it works, but it waits for a very long time before it enters the password. I suspect it waits for the "yes/no" prompt for a certain amount of time, does not receive it and then moves on
expect "yes/no" {
send "yes\n"
}
expect "assword: " {
send "$mypass\n"
}
What is the right way to set this up?
Upvotes: 1
Views: 508
Reputation: 246774
When you split it into two separate commands, you force the script to first look for the "yes/no" prompt. It will wait up to $timeout seconds (default 10 seconds). Only then will it look for the password prompt.
Your first idea is the right approach, but you're missing one key command:
expect {
"yes/no" {
send "yes\r"
exp_continue ;# <== this
}
"assword: " {
send "$mypass\r"
}
}
The exp_continue
command essentially forces the flow of execution to remain in that Expect command so you can still match the password prompt.
A minor point: idiomatically, use \r
(carriage return) as the character for "hitting Enter".
For further learning, look at the expect tag for more information and links to other questions and answers.
Upvotes: 1