2012-02-24 60 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 

這應該與輸出以下命令工作