在寫一個shell腳本A,其中調(diào)用了另一個腳本,例如test.sh但是test.sh中有交互的內(nèi)容需要用戶依次輸入 a 回車 b 回車 回車 回車我想自動的實現(xiàn)它,請問應(yīng)該在A中怎么寫才會自動填入這些內(nèi)容,前提是tesh.sh執(zhí)行的過程還要讓用戶看到,不能把它重定向
2 回答
MM們
TA貢獻1886條經(jīng)驗 獲得超2個贊
用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>
動漫人物
TA貢獻1815條經(jīng)驗 獲得超10個贊
用管道輸入,然后echo出來,給用戶看
A.sh
echo -e a\nb\n\n\n | bash test.shecho aecho bechoechoecho
如果管道輸入滿足不了你的需求,就用expect,這個比較復雜了。
添加回答
舉報
0/150
提交
取消
