English 中文(简体)
我怎么能告诉大家,我已经完成了互动模式?
原标题:How do I tell expect that I have finished the interactive mode?
  • 时间:2011-04-09 01:35:46
  •  标签:
  • expect

I am writing some expect commands in bash.

原文:

#!/bin/bash  
set timeout -1  

expect -c "  

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

expect {  
Prompt2> {send "dir
" }  
}    

"  

我用文字写的用意是,首先,在看到普姆普伦普1时,让我能够控制时,我会指挥一个特定的形象。 然后等到Prompt2显示(这表明图像已经装上)。 之后 让我们执行更多的指挥系统。

在管理文字后,我可以进入互动模式,带我的形象。 这个问题正在脱离遥远机器的互动模式,背弃控制。

我的傲慢:

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

我如何能够这样做?

最佳回答

你的问题有两个方面......

  1. 你应当与明确的<代码>return互动,并在一定程度上了解你解除控制......在这种情况下,我使用三个加标和打入。

  2. 在你控制返回后,文字需要立即重新印发,这意味着你在返回控制之后所做的第一件事是发出另一个<编码><>>。 我为我认为你重新努力做的事情做了编辑。

Example follows...

#!/bin/bash  
set timeout -1  

expect -c "  

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

send "
"
expect {  
Prompt2> {send "dir
" }  
}
"
问题回答

return = fail

这对我来说不无所作为,因为就我而言,这并不是一件大事,只会使我再次面临同样的问题。 我无法说明如何使其与在我发言之前的打印文本相匹配。

expect_out (to fix above solution) = fail

The manual says:

Upon matching a pattern (or eof or full_buffer), any matching and previously unmatched output is saved in the variable expect_out(buffer).

但我看不到工作(除我下文使用外,加上<代码>-indices<>/code>,使工作在那里进行,也没有设想如何使以前的产出纳入新的<代码>expect{......>。)

expect_user

解决办法,使用expect_user 我没有做任何工作,要么是因为我没有解释,要么没有使用我所希望的方式,那么,我不知道如何在我的实际预期档案中应用这一有限的例子。

my solution

So what I did instead was avoid the interactive mode, and just have a way to provide input, one line at a time. It even works for arrow keys and alt+..., (in dpkg Dialog questions) but not for simply <enter> sometimes (hit alt+y for <Yes> or alt+o for <Ok> for those in dpkg Dialog). (anyone know how to send an enter? not , but the enter key like dpkg Dialog wants?)

<代码>i 用户_spawn_id 部分意思是,它不只检查你的间谍程序,而是检查用户类型。 这影响到以后的一切,因此,你使用<代码>expect_ subsequently。 或将其置于其余部分,而不是expect_ before .-indices,从而使其能够读到与正常表述相当的部分。 expect_out(1,string) 是I想要的部分(除殖民地外)。

expect_after {
    -i $user_spawn_id
    # single line custom input; prefix with : and the rest is sent to the application
    -indices -re ":(.*)" {
        send "$expect_out(1,string)"
    }
}

采用<代码>expect_ subsequently,即该代码将适用于所有下列条目:expect。 直至下一个<代码>expect_ subsequently。 因此,你可以在卷宗中将这一条放在你通常的<代码>expect上。

and my case/purpose

I wanted to automate do-release-upgrade which does not properly support the usual Debian non-interactive flags (see here)...it just hangs and ignores input instead of proceeding after a question. But the questions are unpredictable... and an aborted upgrade means you could mess up your system, so some fallback to interaction is required.

Thanks Mike for that suggestion. I tweaked it a bit and adapted it to my problem.

修改法典:

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

The timeout 10 value is not related to the set timeout -1 we set initally. Hence I can execute whatever commands I want on Prompt1 and once keyboard is idle for 10 seconds then script gains control back.

即便在我面临另一个问题之后,在<代码>Prompt1之后,我还是要指挥,装上某个特定的形象。 图像负荷大约为2分钟。 即便有<代码> 定时--1 该书正等待<代码>Prompt2。 甚至连经核实的电离网点都不会。 但是,这方面的解决办法是增加“在预想声明内出现停职时继续留任”。

若要生效,请在预设的电话网络指挥之前张贴。





相关问题
using python expect to run openvpn

i need a python script to run open vpn automaticaly but i use sudo for run the open vpn sudo openvpn --cd /etc/openvpn --config openvpn.conf & thats my terminal command. i have to give the ...

Regular Expression to find string in Expect buffer

I m trying to find a regex that works to match a string of escape characters (an Expect response, see this question) and a six digit number (with alpha-numeric first character). Here s the whole ...

Where can I find TTY and curses documentation for Unix?

I m working on automation tools for an ERP program running on SCO Unix. See my questions on Expect: (Tcl/Expect) clear screen after exit Expect - get variable from screen region based on row and ...

(Tcl/Expect) clear screen after exit

I want to clear the screen (on the local machine) after exiting from my (semi) interactive expect script. Can I do that from within the script? Here s what I tried, that failed. #!/usr/bin/expect -f ...

How can I flush the input buffer in an expect script?

I m writing an Expect script and am having trouble dealing with the shell prompt (on Linux). My Expect script spawns rlogin and the remote system is using ksh. The prompt on the remote system contains ...

default timeout handler for expect script

I have a expect script that need to fail when certain any of the expect condition is not meet. For example: expect "Hello World" If the expect script does not find "Hello World" in certain amount of ...

How can I debug a Perl CGI script?

I inherited a legacy Perl script from an old server which is being removed. The script needs to be implemented on a new server. I ve got it on the new server. The script is pretty simple; it ...

热门标签