使用 python pexpect 的 QEMU 来宾自动化
QEMU guest automation using python pexpect
我正在寻找 运行 qemu 访客,登录并执行一些任务。我正在使用以下内容
- Ubuntu 16.04 作为主持人
- QEMU 2.10.0-rc4
- python3
- 期待 4.0.1
我已经尝试了
中提到的代码
但是,我总是遇到超时异常。任何帮助将不胜感激。请在下面找到我的代码片段和异常。
代码:
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\nroot@qemuarm:~# '
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\nroot@qemuarm:~# '
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
这是不正确的:
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))
我正在寻找 运行 qemu 访客,登录并执行一些任务。我正在使用以下内容
- Ubuntu 16.04 作为主持人
- QEMU 2.10.0-rc4
- python3
- 期待 4.0.1
我已经尝试了
但是,我总是遇到超时异常。任何帮助将不胜感激。请在下面找到我的代码片段和异常。
代码:
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\nroot@qemuarm:~# '
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\nroot@qemuarm:~# '
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
这是不正确的:
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))