简体   繁体   English

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

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

I have this script: 我有这个脚本:

#!/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)

My device has enable SSH and uses the 2222 port, so when I use this port, all goes ok, but when I use the default port, I receive this message: 我的设备已启用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

What is wrong? 怎么了? Thank you for your support!! 谢谢您的支持!!

Finally, I solved it: 最后,我解决了它:

#!/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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM