匹配旧输出中的字符串时,pexpect python模块的方法期望中断 [英] Method expect of pexpect python module breaks when matching a string in old output

查看:149
本文介绍了匹配旧输出中的字符串时,pexpect python模块的方法期望中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pexpect远程使用gdb.这是我当前的代码:

I'm trying to work with gdb remotely, using pexpect. This is my current code:

child = pexpect.spawn("ssh root@192.168.3.10 \"gdb\"")
child.logfile = sys.stdout
child.expect("password:")
child.sendline("xxxx")
child.expect("(gdb)")
child.sendline("attach 9813")
child.expect("(gdb)")
child.sendline("info registers")
child.expect("(gdb)")
child.sendcontrol('c')

这是我控制台输出的一部分:

And this is a part of my console output:

(...)
GNU gdb (GDB) 7.4.1-debian
(...)
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 9813
info registers
test@test-virtual-machine:~$

期望是这样的:

(...)
GNU gdb (GDB) 7.4.1-debian
(...)
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 9813
<Attaching...>
(gdb) info registers
<Registers info displayed...>
(gdb) <Ctrl+C is done>
test@test-virtual-machine:~$

因此,问题似乎出在匹配第一个(gdb)和发送第一个命令attach 9813之后,pexpect不希望第二个(gdb)行发送新的命令info registers.它再次看到第一个(gdb),将其匹配,并仅发送第二个命令而不等待,直到执行第一个命令为止(直到我们附加到所需的过程为止).

So, the problem seems to be that after matching first (gdb) and sending first command attach 9813, pexpect is not expecting a second (gdb) line to send a new command info registers. It sees the first (gdb) again, matches it and just sends a second command without waiting until the first one is executed (until we are attached to a needed proccess).

如何使其仅分析以下输出?没有匹配先前的输出两次? 我在ftp服务器上看到了这样的示例:

How can I make it analyse only the following output? Without matching the previous output twice? I saw a sample like this for ftp server:

child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
child.sendline('pexpect@sourceforge.net')
child.expect('ftp> ')
child.sendline('cd /pub/OpenBSD/3.7/packages/i386')
child.expect('ftp> ')
child.sendline('bin')
child.expect('ftp> ')
child.sendline('prompt')

据我了解,这里pexpect设法以必需的顺序对ftp>做出反应,并连续发送命令.我的代码似乎很相似.

As I understand, here pexpect manages to react to ftp> in required order and sends commands consecutively. My code seems to be similar.

我还尝试了冲洗标准输出,但没有用.

I also tried flushing stdout, it did not work.

推荐答案

GDB有时有时会在命令输出之前再次显示提示.您要匹配第一个提示,发送命令,然后立即匹配"premature"提示,然后再显示结果.这意味着在显示信息寄存器"的结果之前先发送Control-C,但是脚本的其余部分似乎可以正常工作.

GDB seems to sometimes show the prompt again before any output from commands. You are matching the first prompt, sending your command, then immediately matching the 'premature' prompt before the result is displayed. This means that the Control-C at the end is sent before the result from 'info registers' is displayed, but the rest of the script appears to work.

解决方案是在发送下一个命令之前匹配任何预期的输出.

The solution is to match any expected output before sending the next command.

这篇关于匹配旧输出中的字符串时,pexpect python模块的方法期望中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆