简体   繁体   中英

Error in python code while using pxssh for SSH

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.

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"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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