简体   繁体   English

将pxssh用于SSH时python代码出错

[英]Error in python code while using pxssh for SSH

I am trying to connect SSH through python. 我正在尝试通过python连接SSH。 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. 我经历了不同的帖子链接 [link]( 使用pexpect和pxssh时为EOF ),但无法获取解决方案。 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. 您可以使用SSH选项关闭主机密钥检查。

s = pxssh.pxssh(options={
    "StrictHostKeyChecking": "no"})

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

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