2017-08-25 48 views
0

我期待运行qemu客人,登录并执行一些任务。我现在用的是以下QEMU客人自动化使用python pexpect

  • 的Ubuntu 16.04作为主机
  • QEMU 2.10.0-RC4
  • python3
  • Pexpect的4.0.1

我已经尝试了代码作为automation of processes by python简称

但是,我总是得到超时异常。任何帮助将不胜感激。请在下面找到我的代码片段和例外。

代码:

import pexpect 
QEMU_RUN_CMD='qemu-system-arm -nographic -kernel kernel.bin -M versatilepb -drive file=core-image-minimal-qemuarm.rootfs.ext4,if=virtio,format=raw,cache=writeback -m 128 -append "root=/dev/vda rw console=ttyAMA0,115200 console=tty mem=128M highres=off rootfstype=ext4"' 

child = pexpect.spawn(QEMU_RUN_CMD) 
child.expect('login: ') 
child.sendline('root') 
child.expect('# ') 
child.sendline('ls -l') 
child.expect(pexpect.EOF) 
output = child.before 
print ("{}".format(output)) 

异常

Traceback (most recent call last): 
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 97, in expect_loop 
incoming = spawn.read_nonblocking(spawn.maxread, timeout) 
File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 452, in read_nonblocking 
raise TIMEOUT('Timeout exceeded.') 
pexpect.exceptions.TIMEOUT: Timeout exceeded. 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
File "ref.py", line 11, in <module> 
child.expect(pexpect.EOF) 
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 315, in expect 
timeout, searchwindowsize, async) 
File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 339, in expect_list 
return exp.expect_loop(timeout) 
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 104, in expect_loop 
return self.timeout(e) 
File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 68, in timeout 
raise TIMEOUT(msg) 
pexpect.exceptions.TIMEOUT: Timeout exceeded. 
<pexpect.pty_spawn.spawn object at 0x7ff6843fc898> 
command: /usr/local/bin/qemu-system-arm 
args: ['/usr/local/bin/qemu-system-arm', '-nographic', '-kernel', 'zImage--4.4.26+git0+3030330b06_187bcc13f3-r0-qemuarm-20170810070254.bin', '-M', 'versatilepb', '-drive', 'file=core-image-minimal-qemuarm-20170810070254.rootfs.ext4,if=virtio,format=raw,cache=writeback', '-m', '128', '-append', 'root=/dev/vda rw console=ttyAMA0,115200 console=tty mem=128M highres=off rootfstype=ext4'] 
searcher: None 
buffer (last 100 chars): b'0:38 foo.txt\r\r\ndrwxr-xr-x 4 root  root   1024 Aug 24 09:14 sect_test\r\r\[email protected]:~# ' 
before (last 100 chars): b'0:38 foo.txt\r\r\ndrwxr-xr-x 4 root  root   1024 Aug 24 09:14 sect_test\r\r\[email protected]:~# ' 
after: <class 'pexpect.exceptions.TIMEOUT'> 
match: None 
match_index: None 
exitstatus: None 
flag_eof: False 
pid: 10707 
child_fd: 5 
closed: False 
timeout: 30 
delimiter: <class 'pexpect.exceptions.EOF'> 
logfile: None 
logfile_read: None 
logfile_send: None 
maxread: 2000 
ignorecase: False 
searchwindowsize: None 
delaybeforesend: 0.05 
delayafterclose: 0.1 
delayafterterminate: 0.1 

回答

0

这是不正确的:

child.sendline('ls -l') 
child.expect(pexpect.EOF) 

,因为有ls -l后没有EOF权。你应该再次弹出expect() shell提示:

child.sendline('ls -l') 
child.expect('# ') 
output = child.before 
print ("{}".format(output)) 
+0

谢谢。我犯了一个严重的错误。 – user8514760