Python Exception.pxssh 在 SSH 连接失败时不起作用

Python Exception.pxssh doesn't work when SSH conection fails

我有这个脚本:

#!/usr/bin/python

from pexpect import pxssh
import sys

dispo = sys.argv[1]

print dispo

def ingcom(mostrar):
    print mostrar
    s.PROMPT='#'
    s.prompt()
    s.sendline('show ver | i time')
    s.PROMPT='#'
    s.prompt()
    print s.before     # print everything before the prompt.
    s.logout()
    return

try:
    s = pxssh.pxssh()
    s.login (dispo, 'user', 'pass', port=22, auto_prompt_reset=False,login_timeout=30)
    mensaje = "SSH OK via port 22"
    ingcom(mensaje)

except pxssh.ExceptionPxssh, e:
    print "SSH failed on login."
    print str(e)

我的设备启用了 SSH 并使用 2222 端口,所以当我使用这个端口时,一切正常,但是当我使用默认端口时,我收到此消息:

Traceback (most recent call last):
  File "./ssh_ok.py", line 32, in <module>
    s.login (dispo, 'user', 'pass', port=22, auto_prompt_reset=False,login_timeout=30)
  File "/usr/lib/python2.7/site-packages/pexpect/pxssh.py", line 206, in login
    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", TIMEOUT, "(?i)connection closed by remote host"], timeout=login_timeout)
  File "/usr/lib/python2.7/site-packages/pexpect/__init__.py", line 1354, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/lib/python2.7/site-packages/pexpect/__init__.py", line 1368, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/lib/python2.7/site-packages/pexpect/__init__.py", line 1439, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style platform.
<pexpect.pxssh.pxssh object at 0x6ffffe01c90>
version: 2.5.1
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-q', '-p', '22', '-l', 'prtg', '10.27.7.29']
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: 12504
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

怎么了? 感谢您的支持!!

终于解决了:

#!/usr/bin/python

from pexpect import pxssh
import sys

dispo = sys.argv[1]

print dispo

def ingcom(mostrar):
    print mostrar
    s.PROMPT='#'
    s.prompt()
    s.sendline('show ver | i time')
    s.PROMPT='#'
    s.prompt()
    print s.before     # print everything before the prompt.
    s.logout()
    return

try:
    s = pxssh.pxssh()
    s.login (dispo, 'user', 'pass', port=22, auto_prompt_reset=False,login_timeout=30)
    mensaje = "SSH OK via port 22"
    ingcom(mensaje)

except Exception, e:
    print "SSH failed on login."
    print str(e)