【发布时间】:2017-02-15 21:08:12
【问题描述】:
我正在开发一个 python 程序来协助 apt-get 工具。我想使用 pexpect 下载选择的包。我相信我被困在 child.expect 线上。说到那条线,它似乎超时了。
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
更新:
密码发送得很好。它也期望下一行,但随后输入“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)
【问题讨论】:
-
仅供参考,运行
sudo apt install -y package将自动回答是。
标签: python linux ubuntu pexpect