簡體   English   中英

將pxssh用於SSH時python代碼出錯

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

我正在嘗試通過python連接SSH。 我嘗試使用不同的方法消除錯誤,但仍在努力擺脫錯誤。 這是我的代碼:

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

在兩種方式下,我都會遇到相同的錯誤。 另外,我還需要一些有關如何通過私鑰和公鑰進行身份驗證的信息:

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

我到底該怎么辦? 我搜索了其他一些評論,但找不到任何解決方案。 另外,如何在登錄參數中設置私鑰的路徑? 誰能幫我? 我經歷了不同的帖子鏈接 [link]( 使用pexpect和pxssh時為EOF ),但無法獲取解決方案。 感謝您的時間。

您的程序嘗試連接的主機是它所連接的主機的新主機。 首先,必須先接受主機的密鑰,然后才能進行身份驗證。 在終端上,這需要輸入“是”。 您可以使用SSH選項關閉主機密鑰檢查。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM