简体   繁体   English

Python Pexpect 和 Minicom 无法按预期工作

[英]Python Pexpect and Minicom not working as expected

I need to perform a number of processes to Switch that I can only execute through serial port.我需要对 Switch 执行一些只能通过串行端口执行的进程。 To do this I use Pexpect and Minicom.为此,我使用 Pexpect 和 Minicom。 I am able to execute the command child.sendcontrol(“c”) to open the menu.我可以执行命令child.sendcontrol(“c”)打开菜单。 But from the menu I can't choose any of the options.但是从菜单中我无法选择任何选项。

When I do this manually through the Minicom it works without a problem.当我通过 Minicom 手动执行此操作时,它可以正常工作。 what could be the problem?可能是什么问题呢?

Note I use child.after to verify that the actions were actually performed.请注意,我使用child.after来验证操作是否已实际执行。

This is my code:这是我的代码:

#!/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

This is the request to press ctrl + c to open the menu (The log was taken from pexpect through minicom):这是按 ctrl + c 打开菜单的请求(日志是从 pexpect 通过 minicom 获取的):

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

This is the menu (The log was taken from pexpect through minicom):这是菜单(日志是从 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

I'm trying to choose option 1, for the test.我正在尝试选择选项 1 进行测试。 But it is not performed.但它没有被执行。 The same goes for each menu option.每个菜单选项也是如此。

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

This is the error: (Should not take more than 15 seconds to boot)这是错误:(启动时间不应超过 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.*')

I was able to solve the problem, minicom must get manual enter.我能够解决问题,minicom必须手动输入。 Even that child.sendline sends with Enter.甚至那个child.sendline也使用 Enter 发送。 For some reason the minicom doesn't get the enter.由于某种原因,minicom 没有进入。 Child.send ("\r\n") doesn't work either. Child.send ("\r\n")也不起作用。

The solution is:解决方案是:

After the line child.sendline("1")child.sendline("1")行之后

I added the line child.sendcontrol("m") for press enter.我为按下回车添加了child.sendcontrol("m")行。

Now it works properly.现在它可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM