Hi, all
I have just found what cause these strange things. It’s the command: send “wait”
I wrote some scripts to test it.
proc Login {pass} {
# send the password
}
proc Wait {} {
expect "*]$*"
sleep 1
}
proc sendl {message} {
send "$message\r"
}
spawn ssh xxxxx.xxx.xxx
Login xxxxxxxx
Wait
sendl "cd /somepath/"
Wait
sendl "expect infiniteLoop.exp >/dev/null &"
Wait
***sendl "wait"***
Wait
sendl "TESTTEST"
Wait
sendl "HAHAHA"
Wait
sendl "DONEDONEDONE"
Wait
It should be stuck after the line: sendl “wait”
But the result was:
[xxx@xxxxx.xxx ~]$ cd /somepath/
[xxx@xxxxx.xxx folder]$ expect infiniteLoop.exp >/dev/null &
[1] 27260
[xxx@xxxxx.xxx folder]$ wait
TESTTEST
HAHAHA
DONEDONEDONE
[xxx@xxxxx.xxx folder]$
The shell is still waiting, but the follow-up commands has been sent out one by one without any response. All the expect command after that line seems to be invalid. And then the script finished.
I don’t know what happened here. What cause expect commands invalid? The command “wait” seems not to be a program( I can’t use “whereis wait” find it).
This kind of output is probably caused by expect timeout.
I’m not sure what infiniteLoop.exp is doing. If it’s actually an infinite loop(or something taking a long time), the following
waitshell command will always wait because the process(infiniteLoop.exp) never terminates.Therefore, the following
Wait()proc invokes will eventually time out after each 10s (10s is Expect default timeout) and the script just continues like:When the Expect script doesn’t tun as expected, I always do two things:
The first one is to run the procedure manually to check if there is something wrong.
The second one is adding -d option to turn on debugging message(ex:
expect -d hello.exp).If you see something like “expect: timed out”, then the previous pattern is probably failed to match.
BTW, you can add a little check to indicate the timeout case