You can make use of exp_continue
to handle this situation. The command exp_continue
allows expect itself to continue executing rather than returning as it normally would. This is useful for avoiding explicit loops or repeated expect statements. By default, exp_continue
resets the timeout
timer. The timer is not restarted, if exp_continue
is called with the -continue_timer
flag.
In expect
, the default timeout is 10 seconds. i.e. the time till which expect
will wait for the expected string to appear.
we used to give the expected string in expect
as something like
expect "name"
which will wait for the string 'name' and proceed to next statement if timeout happened. To handle the timeout scenario, we are using the keyword timeout
in expect
itself.
expect {
"name" { # Some code here }
timeout { # timeout_hanlder_code_here }
}
You can change timeout
value by using set
command as shown below.
set timeout 60; # Timeout will happen after 60 seconds.
So, combining all together in one shot,
expect {
# If the phrase 'Enter Password' seen, then it will send the password
"Enter Password" {send "yourpassword\r"}
# If 'timeout' happened, then it will send some keys &
# 'expect' will be looped again.
timeout {send -- {^[-}; exp_continue}
}
Note : I am seeing a problem in your code. You have mentioned that you have to send escape + hyphen key together. But, you are sending only literal square bracket ([
) and hyphen (-
) symbol. If it is working then fine and you don't need to read this 'Note' section.Skip it. Else, proceed to read below.
You should send the actual Escape character to the program. It can be done as
send -- \033-; # Sending Escape + hyphen together
What is this \033
? It is the octal code for Escape key. Then along with that we are just combining the hyphen with it's symbol as -
which results in \033-
. So our final code will be,
expect {
# If the phrase 'Enter Password' seen, then it will send the password
"Enter Password" {send "yourpassword\r"}
# If 'timeout' happened, then it will send some keys &
# 'expect' will be looped again.
timeout {send -- \033-; exp_continue}
}
Reference : Tcl's wiki & ASCII Char Table