私はapt-getツールを手助けするために、Pythonプログラムに取り組んでいます。私は選択したパッケージをダウンロードするためにpexpectを使用したい。私はchild.expectの行に固執していると信じています。それは、その行になるとタイムアウトしているようです。Pexpectでルートコマンドを実行するには?
butt = "vlc"
child = pexpect.spawn('sudo apt-get install ' + butt)
child.logfile = sys.stdout
child.expect('[sudo] password for user1: ')
child.sendline('mypassword')
これはログファイルです。
TIMEOUT: Timeout exceeded.
<pexpect.spawn object at 0xb5ec558c>
version: 3.2
command: /usr/bin/sudo
args: ['/usr/bin/sudo', 'apt-get', 'install', 'vlc']
searcher: <pexpect.searcher_re object at 0xb565710c>
buffer (last 100 chars): '[sudo] password for user1: '
before (last 100 chars): '[sudo] password for user1: '
after: <class 'pexpect.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 27641
child_fd: 4
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: <open file '<stdout>', mode 'w' at 0xb74d8078>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
UPDATE:
パスワードがうまく送信されます。また、次の行も期待しますが、「Y」を入力して何もしません。
child = pexpect.spawn('sudo apt-get install ' + butt)
child.logfile = sys.stdout
child.expect_exact('[sudo] password for user1: ')
child.sendline('mypass')
child.expect_exact('Do you want to continue? [Y/n] ')
child.sendline('Y')
解決しよう:
私は最後に次の行を追加する必要がありました。
child.expect(pexpect.EOF, timeout=None)
これはうまくいきました。今、私はその行と私はパスワードを送ることができると期待しています。ありがとう。 – user2631279
問題ありません。喜んで助けてください。 –