我怎样才能知道我已经完成了交互模式? [英] How do I tell expect that I have finished the interactive mode?

查看:51
本文介绍了我怎样才能知道我已经完成了交互模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在bash中编写一些期望命令.

脚本:

 #!/bin/bash设置超时-1期望-c生成telnet $ IP $ PORT1睡1发送\"\ r \"发送\"\ r \"期待Prompt1>互动超时20 {睡1}预计 {提示2>{发送\"dir \ r \"}}" 

我对脚本的意图是,首先让它远程登录到计算机,当它看到Prompt1时,让它提供控制权,我将执行命令以加载特定的图像.然后等待,直到Prompt2出现(表明图像已加载).然后让它执行其他命令集.

运行脚本后,我可以进入交互模式,加载我的图像.问题是退出远程计算机上的交互模式,并将控制权交还给它.

我得到的错误:

  expect:派生ID exp4未打开在执行时期望-nobrace Prompt2 {发送"dir \ r"}从内部调用预计 {提示2 {发送"dir \ r"}}" 

我该怎么做?

解决方案

您的问题有两个...

  1. 您应该与明确的 return 进行交互,并提供某种方式来知道您已释放控制权...在这种情况下,我使用三个加号并按Enter键./p>

  2. 返回控件后,脚本将需要再次获得提示,这意味着返回控件后期望执行的第一件事是发送另一个 \ r .我编辑了我认为您要尝试执行的操作...

示例如下...

 #!/bin/bash设置超时-1期望-c生成telnet $ IP $ PORT1睡1发送\"\ r \"发送\"\ r \"期待Prompt1>互动+++返回发送\"\ r \"预计 {提示2>{发送\"dir \ r \"}}" 

I am writing some expect 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, first let it telnet into a machine, when it sees Prompt1, let it give control to me, I will execute a command to load a specific image. Then wait until Prompt2 shows up (which indicates image has been loaded). Then Let it execute the further set of commands.

After running the script, I could get into the interactive mode, load my image. The problem is getting out of interactive mode on the remote machine and giving back control to it.

The Error which I got:

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?

解决方案

Your problem is two-fold...

  1. You should interact with an explicit return, and give it some way to know you've released control... in this case, I use three plus signs and hit enter.

  2. After you return control, the script will need to get the prompt again, which means the first thing you do after returning control to expect is send another \r. I edited for what I think you're trying to do...

Example follows...

#!/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\" }  
}
"

这篇关于我怎样才能知道我已经完成了交互模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆