亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我想自動的實現它,請問應該在A中怎么寫才會自動填入這些內容?

我想自動的實現它,請問應該在A中怎么寫才會自動填入這些內容?

UYOU 2023-04-15 22:18:35
在寫一個shell腳本A,其中調用了另一個腳本,例如test.sh但是test.sh中有交互的內容需要用戶依次輸入 a 回車 b 回車 回車 回車我想自動的實現它,請問應該在A中怎么寫才會自動填入這些內容,前提是tesh.sh執行的過程還要讓用戶看到,不能把它重定向
查看完整描述

2 回答

?
MM們

TA貢獻1886條經驗 獲得超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>


查看完整回答
反對 回復 2023-04-18
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

用管道輸入,然后echo出來,給用戶看
A.sh

echo -e a\nb\n\n\n | bash test.shecho aecho bechoechoecho

如果管道輸入滿足不了你的需求,就用expect,這個比較復雜了。


查看完整回答
反對 回復 2023-04-18
  • 2 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號