Reputation: 1141
I need an expect
script to wait for an issued command to finish, and then log out from telnet
. Here is the script:
spawn telnet host
send "username\r"
send "password\r"
sleep 3
send "custom_command\r"
while {???}
{
sleep 1
}
send "logout\r"
expect eof
The part that I don't know how to phrase is ???
. I basically just need to wait for the prompt
to show up, and as soon as it shows up, the script should end. I'm guessing it should be something like [gets line != "prompt>" ]
.
Upvotes: 6
Views: 45780
Reputation: 246817
You should really expect something before you send something, to get the timing right. Something like:
exp_internal 1 ;# expect internal debugging. remove when not needed
spawn telnet host
expect "login: "
send "username\r"
expect "Password: "
send "password\r"
set prompt {\$ $} ;# this is a regular expression to match the *end* of
# your shell prompt. Adjust as required.
expect -re $prompt
send "custom_command\r"
expect -re $prompt
send "logout\r"
expect eof
Upvotes: 7
Reputation: 1141
I tried the expect command, but it didn't work, after some research, trial and error I figured out the following:
expect "prompt>\r"
instead of expect "prompt>"
expect "prompt>\r" {
set timeout -1
to wait for the prompt infinitely, instead of 10 secondsSo, the answer is:
spawn telnet host
send "username\r"
send "password\r"
sleep 3
set timeout -1
send "custom_command\r"
expect "prompt>\r" {
send "logout\r"
expect eof
}
Upvotes: 10
Reputation: 1526
The expect
command of expect seems appropriate here.
Something like expect "prompt\n"
followed by the sending of the logout.
As a note, if this is a normal telnet system you should generally wait to be prompted for username and password before just sending it over. See how to automate telnet session using expect or expect script to automate telnet login
Upvotes: 2