2012-02-24 65 views
0

您好我正在Python中使用pexpect来读取ssh设备信息。pexpect中的逻辑

expObject = pexpect.spawn('/usr/bin/ssh %[email protected]%s' % (username, device)) 
expObject.sendline(password) 

提供密码后,我已经显示了一些设备信息,并在命令提示符下,它会询问按任意键继续;一旦我按任何键信息消失。

我使用下面的逻辑来捕捉其他数据给出命令状show version

expObject.expect(CLI_PROMPT) 
    data = expObject.before 

那么,如何抓住这是给密码后,按下任意键使用“expObject”到comtinue之前所显示的数据之后到来。

回答

2

我有,我需要处理由行的文本输出线类似的问题。为了使这个工作,你必须知道,pexpect配置正则表达式,这样。*模式包括换行,所以而不是。*你必须使用[^ \ n] *。像这样的东西应该在你的情况下工作:

password propt text 
<start of data captured> - 1st line 
a second line 
a third line 
last line <end of data that will be captured> 
press any key to continue 

child = pexpect.spawn('ssh command goes here') 
child.expect('password prompt text\r\n') 
child.sendline(password) 
data = "" 
while True: 
    i = child.expect(['press any key to continue', '[^\n]*\r\n']) 
    if i == 0: 
     break 
    data += child.before 
print data 

这应该与输出以下命令工作