2016-12-13 77 views
1

给定一个交互式的Python脚本Expect脚本与一个Python的命令行应用程序

#!/usr/bin/python 
import sys 

name = raw_input("Please enter your name: ") 
age = raw_input("Please enter your age: ") 
print("Happy %s.th birthday %s!" % (age, name)) 

while 1: 
    r = raw_input("q for quit: ") 
    if r == "q": 
     sys.exit() 

我想从与它交互的expect脚本

#!/usr/bin/expect -f 

set timeout 3 
puts "example to interact" 
spawn python app.py 

expect { 
    "name: " { send "jani\r"; } 
    "age: " { send "12\r"; } 
    "quit: " { send "q\r"; } 
} 

puts "bye" 

的期望脚本似乎没有互动交互与Python应用程序刚刚运行。

是python的问题还是与期望的代码?

回答

0

你需要的是3种不同的期望呼叫:

#!/usr/bin/expect -f 

set timeout 3 
puts "example to interact" 
spawn python app.py 

expect "name: " { send "jani\r" } 
expect "age: " { send "12\r" } 
expect "quit: " { send "q\r" } 

原因是expect命令并不像一个循环工作。一旦它处理了输入/输出,它就会继续。

0

为什么你需要预期?

python app.py <<END_INPUT 
jani 
12 
q 
END_INPUT 
+1

我真正的问题是关于一个ssh会话,其中登录包括很多额外的行,也有机器的具体信息。我必须在90台服务器上运行一些'yum remove cvs'。这只是我在Expect上的学习曲线。 – jshepherd

+0

该要求与您的问题无关。你研究了[标签:期待]有关自动化ssh的问题吗?已经有几十个类似的问题。 –

相关问题