python - 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.*')
解决方案
我能够解决问题,minicom必须手动输入。即使这样也可以child.sendline
使用 Enter 发送。由于某种原因,minicom 没有进入。
Child.send ("\r\n")
也不行。
解决方案是:
线后child.sendline("1")
我添加了child.sendcontrol("m")
按回车的行。
现在它可以正常工作了。
推荐阅读
- javascript - 在迭代时更改我的数组(Javascript)
- java - Flink:左加入带有静态列表的流
- php - 对 phpmail 例程的 Ajax 调用在 ASYNC:FALSE 中有明显的延迟,并且在涉及到新页面的标头时在 ASYNC:TUE 中失败
- javascript - DynamoDB API 时间点备份启用
- javascript - 如果我们在新页面上加载 React 应用程序,如何停止历史记录?
- python - Python 2.7 已弃用,我应该删除它吗?
- pine-script - 最大策略利润的变量值迭代 - Pine 脚本
- reactjs - React reload call api函数不起作用
- mysql - MySQL连接客户端和服务器无法通信,因为它们没有共同的算法
- php - 奇怪的 php sub() 和 add() 函数改变了我的变量