How can I expect the interactive mode to end?

I am writing some wait commands in bash.

Script:

#!/bin/bash  
set timeout -1  

expect -c "  

spawn telnet $IP $PORT1  
sleep 1  
send \"\r\"  
send \"\r\"  
expect Prompt1>  
interact timeout 20 {  
sleep 1  
}  

expect {  
Prompt2> {send \"dir\r\" }  
}    

"  

My intentions with the script are to connect telnet to the computer first, when it sees Prompt1, let it give me control, I will execute a command to download a specific image. Then wait for Prompt2 to appear (which indicates that the image has been downloaded). Then let him execute a further set of commands.

After running the script, I can enter interactive mode, upload the image. The problem is to exit interactive mode on the remote computer and return to it.

The error I received is:

expect: spawn id exp4 not open  
    while executing  
"expect -nobrace Prompt2 {send "dir\r" }"  
    invoked from within  
"expect {    
Prompt2 {send "dir\r" }  
}"  

How can i do this?

+5
source share
2 answers

Your problem is double ...

  • return , ... enter.

  • , , script , , , , \r. , , , ...

...

#!/bin/bash  
set timeout -1  

expect -c "  

spawn telnet $IP $PORT1  
sleep 1  
send \"\r\"  
send \"\r\"  
expect Prompt1>  
interact +++ return

send \"\r\"
expect {  
Prompt2> {send \"dir\r\" }  
}
"
+6

. .

:

expect Prompt1>  
interact timeout 10 return  
expect {  
timeout {exp_continue}  
Prompt2 {send \"dir\r\" }  
}  

- 10 set timeout -1, initally. , Prompt1, 10 , script .

, Prompt1, . 2 . set timeout -1 script Prompt2. - telnet, . exp_continue .

set timeout -1 , telnet spawn .

0

All Articles