怎么自动填写有交互的shell脚本
在写一个shell脚本A,其中调用了另一个脚本,例如test.sh
但是test.sh中有交互的内容
需要用户依次输入 a 回车 b 回车 回车 回车
我想自动的实现它,请问应该在A中怎么写才会自动填入这些内容,前提是tesh.sh执行的过程还要让用户看到,不能把它重定向
Answers
用pexpect,以下代码供参考:
#!/usr/bin/python
import sys
import pexpect
password = 'password'
expect_list = ['(yes/no)', 'password:']
p = pexpect.spawn('ssh username@localhost ls')
try:
while True:
idx = p.expect(expect_list)
print p.before + expect_list[idx],
if idx == 0:
print "yes"
p.sendline('yes')
elif idx == 1:
print password
p.sendline(password)
except pexpect.TIMEOUT:
print >>sys.stderr, 'timeout'
except pexpect.EOF:
print p.before
print >>sys.stderr, '<the end>'
输出:
username@localhost's password: password
Permission denied, please try again.
username@localhost's password: password
Permission denied, please try again.
username@localhost's password: password
Permission denied (publickey,gssapi-with-mic,password).
<the end>