首页 > 解决方案 > Python Pexpect 和 Minicom 无法按预期工作

问题描述

我需要对 Switch 执行一些只能通过串行端口执行的进程。为此,我使用 Pexpect 和 Minicom。我能够执行child.sendcontrol(“c”)打开菜单的命令。但是从菜单中我无法选择任何选项。

当我通过 Minicom 手动执行此操作时,它可以正常工作。可能是什么问题呢?

注意我child.after用来验证操作是否实际执行。

这是我的代码:

#!/usr/bin/python3

import pexpect

print("Waiting for connection....") # for check
child = pexpect.spawn("minicom")
child.expect(r".*Hit.*", timeout=120)
test = child.after
fileNameone = "/root/Desktop/fileone.txt"
file = open(fileNameone, "wb")
file.write(test)
file.close

child.sendcontrol("c")
print("menu") # for check

child.expect(".*selection.*", timeout=10)
testtwo = child.after
fileNametwo = "/root/Desktop/filetwo.txt"
file = open(fileNametwo, "wb")
file.write(testtwo)
file.close
child.sendline("1")
print("choose 1") # for check


child.expect(".*Gateway.*", timeout=40)
child.sendline("admin")
child.expect(".*Password.*", timeout=10)
child.sendline("admin")
print("connect!") # for check

这是按 ctrl + c 打开菜单的请求(日志是从 pexpect 通过 minicom 获取的):

[23;80H [24;1H************ Hit 'Ctrl + C' for boot menu ************
[23;80H [24;1H
[23;80H [24;1H 2 

这是菜单(日志是从 pexpect 通过 minicom 获取的):

[23;80H [24;1HWelcome to Embedded Boot Menu :
[23;80H [24;1H
[23;80H [24;1H[24;9H1. Start in normal Mode
[23;80H [24;1H[24;9H2. Start in debug Mode
[23;80H [24;1H[24;9H3. Start in maintenance Mode
[23;80H [24;1H[24;9H4. Restore to Factory Defaults (local)
[23;80H [24;1H[24;9H5. Install/Update Image from Network
[23;80H [24;1H[24;9H6. Restart Boot-Loader
[23;80H [24;1H[24;9H7. Run Hardware diagnostics
[23;80H [24;1H[24;9H8. Upload preset configuration file
[23;80H [24;1H[24;9H9. Delete preset configuration file
[23;80H [24;1H
[23;80H [24;1H[24;9HPlease enter your selection (pres

我正在尝试选择选项 1 进行测试。但它没有被执行。每个菜单选项也是如此。

child.expect(".*selection.*", timeout=10)
child.sendline("1")

这是错误:(启动时间不应超过 15 秒)

Traceback (most recent call last):
  File "./test.py", line 33, in <module>
    child.expect(".*Gateway.*", timeout=40)
  File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 344, in expect
    timeout, searchwindowsize, async_)
  File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 372, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py", line 181, in expect_loop
    return self.timeout(e)
  File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py", line 144, in timeout
    raise exc
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7fefa0934a90>
command: /usr/bin/minicom
args: ['/usr/bin/minicom']
buffer (last 100 chars): b's ENTER to finish) :1\r\n\x1b[23;80H \x1b[24;1H'
before (last 100 chars): b's ENTER to finish) :1\r\n\x1b[23;80H \x1b[24;1H'
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 10862
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
searcher: searcher_re:
    0: re.compile(b'.*Gateway.*')

标签: pythonpython-3.xserial-portpexpect

解决方案


我能够解决问题,minicom必须手动输入。即使这样也可以child.sendline使用 Enter 发送。由于某种原因,minicom 没有进入。 Child.send ("\r\n")也不行。

解决方案是:

线后child.sendline("1")

我添加了child.sendcontrol("m")按回车的行。

现在它可以正常工作了。


推荐阅读