play9091 发表于 2014-04-27 22:06

問題:等待輸入

請教各位先進,我想要用 Python 去批量的執行另外一個腳本。

想問問看,在 Python 裡面有沒有功能是可以在,當糸統等待輸入的時候才丟出用串的功能?

例如:
當腳本A等待你輸入Y才往下執行,Python就輸入 Y

Hadron74 发表于 2014-04-27 22:10

用input或raw_input

play9091 发表于 2014-04-28 09:42

回复 2# Hadron74


    不好意思,是我表達不明確……

我想用 Python 去執行 另外一個腳本 Perl。在 Perl 裡面要使用者輸入 確認(Y) 。
我的問題是,要如何在 Perl 出現要使用者輸入 確認(Y) 時才讓 Python 輸出 確認(Y) 給 Perl 。

sosolitude 发表于 2014-04-30 23:08

回复 1# play9091

pexpect 行不行?

网上找的import pexpect

if __name__ == '__main__':
    user = 'forever'
    ip = '192.168.0.200'
    mypassword = 'forever'
      
    print user
    child = pexpect.spawn('ssh %s@%s' % (user,ip))
    child.expect ('password:')
    child.sendline (mypassword)
      
    child.expect(')
    child.sendline('sudo -s')
    child.expect (':')
    child.sendline (mypassword)
    child.expect('#')
    child.sendline('ls -la')
    child.expect('#')
    print child.before   # Print the result of the ls command.
    child.sendline("echo '112' >> /home/forever/1.txt ")
    child.interact()   # Give control of the child to the user.

yjphhw 发表于 2014-05-01 12:10

subprocess 里的 call里边有个 comunicate可以。

play9091 发表于 2014-05-20 13:27

謝謝大家,我通過修改源碼內的變數解決了!
页: [1]
查看完整版本: 問題:等待輸入