-1

I'm trying to do a Python script (with a loop) to connect in SSH,it works if everything is good ( password and route) but stop when it's not working(wrong password or no routes to host ).Here is the important part of the script, how can I control if everything is working ?

connexion = pexpect.spawn("ssh -o StrictHostKeyChecking=no "+user+"@" + ip )
index=connexion.expect(':')
connexion.sendline(password + "\r")
connexion.expect('>')
connexion.sendline('show clock \r')
connexion.expect('>')
connexion.sendline('exit')
connexion.close()

I get the error :

Traceback (most recent call last):
File "script.py", line 21, in <module>
connexion.expect('>')
 File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in    expect
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list
timeout, searchwindowsize)
File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1521, in expect_loop
raise EOF(str(err) + '\n' + str(self))
pexpect.EOF: End Of File (EOF). Exception style platform.
<pexpect.spawn object at 0x7fcfeecee750>
version: 3.1
command: /usr/bin/ssh
args: ['/usr/bin/ssh', '-o', 'StrictHostKeyChecking=no',   'username@10.9.128.5']
 searcher: <pexpect.searcher_re object at 0x7fcfeecee850>
 buffer (last 100 chars): ''
 before (last 100 chars): ' connect to host 10.9.128.5 port 22: No route to  host\r\r\npassword\r\n\r\n'

Thanks

77140B
  • 41
  • 6
  • What are the errors that you get? Cause you could possibly put your code in a try block and catch the error if the password is incorrect and prompt the user for the password again. Can you post some examples of your code where it fails, as well as the full error traceback? – Harrison Aug 07 '16 at 22:32
  • Traceback (most recent call last): File "script.py", line 21, in connexion.expect('>') File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1418, in expect timeout, searchwindowsize) File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1433, in expect_list timeout, searchwindowsize) File "/usr/lib/python2.7/dist-packages/pexpect/__init__.py", line 1521, in expect_loop raise EOF(str(err) + '\n' + str(self)) pexpect.EOF: End Of File (EOF). Exception style platform. – 77140B Aug 07 '16 at 22:34
  • Can you add that error to your original post and format it please so it's easily readable? – Harrison Aug 07 '16 at 22:36
  • Ok i edit the post. – 77140B Aug 07 '16 at 22:41
  • Looks like it possibly did a eof exception when it could not get to the host. you probably need to have a try block to catch the exception should it occur and handle the fact the connection died or was not available. – LhasaDad Aug 08 '16 at 01:15

2 Answers2

0

The problem is that the host 10.9.128.5 was not reachable at the moment. ssh has returned this message:

connect to host 10.9.128.5 port 22: No route to  host

And not that, what was expected.

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
0

You are getting an exception which needs to be handled correctly. Below is some code that you can use as an example to pass the exception and log it.

def ignore_exception_out(conn, text, timeout=10):
        try:
            conn.expect(text, timeout)
        except Exception as e:
            logging.log("Exception reached {0}".format(e))
            pass
cmidi
  • 1,880
  • 3
  • 20
  • 35