0

I am trying to connect SSH through python. I have tried to remove errors using different methods, still struggling to get rid of the errors. This is my code:

1) import pxssh
   import getpass
   def get_ssh():
    s= pxssh.pxssh()
    hostname = raw_input('IP: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login (IP, username, password)
    s.logout()
    return True

2)   s= pxssh.pxssh(timeout='time_out', maxread=20000)
     s.SSH_OPTS += " -o StrictHostKeyChecking=no"
     s.SSH_OPTS += " -o UserKnownHostsFile=/dev/null"
     s.login ('IP', 'username', 'password')

In Both way I am getting same errors. Also I need some information about how to pass private key and public key for authentication:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mit_test.py", line 11, in get_ssh
    s.login (hostname, username, password)
  File "/usr/lib/python2.7/dist-packages/pxssh.py", line 196, 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/dist-packages/pexpect.py", line 1316, in expect
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1330, in expect_list
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
  File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1401, in expect_loop
    raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End Of File (EOF) in read_nonblocking(). Exception style platform.
<pxssh.pxssh object at 0x2840ad0>
version: 2.4 ($Revision: 516 $)
command: /usr/bin/ssh
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")

What exactly I have to do? I have searched some other comments but could't find any solution. Also, how can i set a path for private key in login argument? Can anyone help me? I have gone through different posts link [link] (EOF when using pexpect and pxssh) but couldn't get the solution. thank you for your time.

Community
  • 1
  • 1
M. T.
  • 33
  • 1
  • 7
  • Have you tried if SSH connection from the same host with same user as your program works otherwise? – marekful Feb 10 '17 at 09:36
  • Also, what it looks like is that when host key check finds a new host, as it seems in this case, you have to type 'yes' on the terminal first to proceed to authentication. You can disable this with [SSH options](http://linuxcommand.org/man_pages/ssh1.html) (`-O`) – marekful Feb 10 '17 at 09:39
  • yes I have configured the database with same host username and password and it works. Now I want to make SSH tunnel using python. – M. T. Feb 10 '17 at 09:42
  • Hello marekful, how i can modify my code to include SSH options? I have no clue. I searched from other answers: [link](http://stackoverflow.com/questions/17879585/eof-when-using-pexpect-and-pxssh) but wasn't helpful. – M. T. Feb 10 '17 at 11:25
  • See `StrictHostKeyChecking` [here](http://pexpect.readthedocs.io/en/stable/api/pxssh.html) – marekful Feb 10 '17 at 11:58

1 Answers1

0

The host your program tries to connect to is new the host it's connecting from. First the host's key must be accepted before proceeding to authentication. On a terminal this requires typing 'yes'. You can turn off host key checking with SSH options.

s = pxssh.pxssh(options={
    "StrictHostKeyChecking": "no"})
marekful
  • 14,986
  • 6
  • 37
  • 59
  • I have tried in both way to pass options 1) s= pxssh.pxssh(options={"StrictHostKeyChecking": "no"}) and by 2)s= pxssh.pxssh(timeout='time_out', maxread=2000000) s.SSH_OPTS += " -o StrictHostKeyChecking=no" s.SSH_OPTS += " -o UserKnownHostsFile=/dev/null" But still shows same errors. I don't know how to pass public and private key into login information. – M. T. Feb 10 '17 at 13:26
  • I have successfully done using paramiko: `def connect_ssh(): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect('151.252.41.105', port= 22, username='user', password= 'password',key_filename= '/home/ulw/.ssh/id_rsa') return c c=connect_ssh() stdin, stdout, stderr = c.exec_command(" ls -la") print stdout.readlines()`. – M. T. Feb 15 '17 at 10:19