Wait for prompts pending script
I need the expect
script to wait for the issued command to complete and then exit 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
Part I don't know as a phrase ???
. I just need to wait for it to appear prompt
and as soon as it appears the script should end. I guess it should be something like [gets line != "prompt>" ]
.
source to share
expect
the team expect to be appropriate here. Something like expect "prompt\n"
, followed by sending the output file.
As a side note, if this is a regular telnet system, you usually have to wait to receive a prompt for a username and password before simply submitting it. See how to automate a telnet session while waiting or waiting for a script to automate telnet login
source to share
You really have to expect something before submitting 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
source to share
I tried the command expect, but it didn't work, after some research, trial and error, I figured out the following:
- Use
expect "prompt>\r"
insteadexpect "prompt>"
- Curly curly braces must be on the same line as the wait command, for example
expect "prompt>\r" {
- Use
set timeout -1
to wait for a prompt indefinitely instead of 10 seconds
So 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
}
source to share