Search code examples
pythonpxssh

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:

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!!


Solution

  • 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)