2013-07-26 49 views
6

當我試圖運行在與SSH交互通過Pexpect的蠻力從Violent Python第2章強制SSH密碼與Pxssh部分的代碼。使用child.expect()pxssh我都會得到類似的EOF錯誤。EOF使用Pexpect的和pxssh

從Python控制檯運行這些命令:

import pexpect 
connStr = "ssh [email protected]" 
child = pexpect.spawn(connStr) 
ret = child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"]) 

我得到這樣的輸出:

Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li 
ne 1316, in expect 
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li 
ne 1330, in expect_list 
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li 
ne 1401, in expect_loop 
    raise EOF (str(e) + '\n' + str(self)) 
EOF: End Of File (EOF) in read_nonblocking(). Empty string style platform. 
<pexpect.spawn object at 0x10180c550> 
version: 2.4 ($Revision: 516 $) 
command: /usr/bin/ssh 
args: ['/usr/bin/ssh', '[email protected]'] 
searcher: searcher_re: 
    0: TIMEOUT 
    1: re.compile("Are you sure you want to continue connecting") 
    2: re.compile("[P|p]assword:") 
buffer (last 100 chars): 
before (last 100 chars): 
after: <class 'pexpect.EOF'> 
match: None 
match_index: None 
exitstatus: 255 
flag_eof: True 
pid: 12122 
child_fd: 4 
closed: False 
timeout: 30 
delimiter: <class 'pexpect.EOF'> 
logfile: None 
logfile_read: None 
logfile_send: None 
maxread: 2000 
ignorecase: False 
searchwindowsize: None 
delaybeforesend: 0.05 
delayafterclose: 0.1 
delayafterterminate: 0.1 

並運行這些命令,使用pxssh

import pxssh 
s = pxssh.pxssh() 
s.login("127.0.0.1", "root", "1234") 

我得到這個輸出:

Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pxssh.py", line 
196, in login 
    i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:pas 
sword)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", TIMEOUT, "(?i)connectio 
n closed by remote host"], timeout=login_timeout) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li 
ne 1316, in expect 
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li 
ne 1330, in expect_list 
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pexpect.py", li 
ne 1401, in expect_loop 
    raise EOF (str(e) + '\n' + str(self)) 
EOF: End Of File (EOF) in read_nonblocking(). Empty string style platform. 
<pxssh.pxssh object at 0x1016bff90> 
version: 2.4 ($Revision: 516 $) 
command: /usr/bin/ssh 
args: ['/usr/bin/ssh', '-q', '-l', 'root', '127.0.0.1'] 
searcher: searcher_re: 
    0: re.compile("(?i)are you sure you want to continue connecting") 
    1: re.compile("[#$]") 
    2: re.compile("(?i)(?:password)|(?:passphrase for key)") 
    3: re.compile("(?i)permission denied") 
    4: re.compile("(?i)terminal type") 
    5: TIMEOUT 
    6: re.compile("(?i)connection closed by remote host") 
buffer (last 100 chars): 
before (last 100 chars): 
after: <class 'pexpect.EOF'> 
match: None 
match_index: None 
exitstatus: None 
flag_eof: True 
pid: 12136 
child_fd: 3 
closed: False 
timeout: 30 
delimiter: <class 'pexpect.EOF'> 
logfile: None 
logfile_read: None 
logfile_send: None 
maxread: 2000 
ignorecase: False 
searchwindowsize: None 
delaybeforesend: 0.05 
delayafterclose: 0.1 
delayafterterminate: 0.1 

當我將127.0.0.1替換爲其他主機並嘗試使用不同的用戶名/密碼組合時,我會得到類似的結果。

pexpect documentation建議使用expect(pexpect.EOF)以避免生成EOF異常。事實上,當我做到以下幾點:

connStr = "ssh [email protected]" 
child = pexpect.spawn(connStr) 
print child.expect(pexpect.EOF) 

結果是0

但接下來的問題是:

  1. 我對書的語法困惑:child.expect([pexpect.TIMEOUT, ssh_newkey, "[P|p]assword:"])。爲什麼我們將一個列表傳遞給expect()?這個列表應該包含什麼?
  2. 如何使用expect(pexpect.EOF),正如文檔指示的那樣,使用pxssh時?
  3. 爲什麼書中的代碼無法正常工作?自該書出版以來,在書店庫中有什麼變化?是因爲我在OS X上嗎?

我有Python 2.7和pexpect 2.4在Mac OS X 10.8.4上運行。

+0

您可能已經添加了嘗試連接到主機的計算機的ssh公鑰。你確定當你試圖連接你的密碼提示嗎? – orezvani

+0

我也面臨同樣的問題。有沒有解決方案? – Nikole

回答

5

關於#2:期待EOF在這裏是紅鯡魚。您不希望在登錄時輸入EOF,您希望在登錄時輸入密碼。 pxssh在從ssh登錄時返回EOF時發生錯誤,但未收到密碼提示。這可能會發生,因爲它使用ssh -q不會收到警告,並且您從ssh收到警告。就拿它使用ssh選項和自己運行它們,而不問:

在/ usr/bin中/ SSH -l根127.0.0.1

在我的情況,我可以得到SSH時蹬出此錯誤信息由於我連接的計算機的身份發生變化,因此已知的主機違規行爲。

1

我試圖在pxssh中使用self.expect()時發生同樣的錯誤。通過刪除'-q'來更改ssh選項對我來說不起作用。

我按照本網站中的說明添加了pexpect.EOF作爲self.expect()的輸入之一。這將使該腳本等到整個字符串被接收時,接收到EOF退出前:

http://pexpect.readthedocs.org/en/stable/overview.html

i = self.expect(["(?i)are you sure you want to continue connecting", original_prompt, "(?i)(?:password.*)|(?:passphrase for key)", "(?i)permission denied", "(?i)terminal type", pexpect.EOF, TIMEOUT, "(?i)connection closed by remote host"], timeout=login_timeout) 

有了這個,它工作正常!