2017-02-10 119 views
0

我試圖通過python連接SSH。我試圖用不同的方法去除錯誤,仍努力擺脫錯誤。這是我的代碼:在使用pxssh進行SSH時使用python代碼時出錯

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 [鏈接](EOF when using pexpect and pxssh),但無法獲得解決方案。感謝您的時間。

+0

您是否嘗試過,如果SSH連接與您的程序具有相同用戶的同一主機工作,否則會起作用? – marekful

+0

此外,它看起來像是當主機密鑰檢查發現一個新的主機,就像在這種情況下,你必須先在終端上鍵入'yes'才能繼續進行認證。你可以用[SSH選項](http://linuxcommand.org/man_pages/ssh1.html)('-O')來禁用這個。 – marekful

+0

是的,我已經使用相同的主機用戶名和密碼配置數據庫,它可以工作。現在我想用python製作SSH隧道。 –

回答

0

程序試圖連接的主機是新連接的主機。首先在進行認證之前必須接受主人的密鑰。在終端上,這需要輸入「是」。您可以使用SSH選項關閉主機密鑰檢查。

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

我已經嘗試了兩種方式來傳遞選項1)s = pxssh.pxssh(options = { 「StrictHostKeyChecking」:「no」})和2)s = pxssh.pxssh(timeout ='time_out',maxread = 2000000)s.SSH_OPTS + =「-o StrictHostKeyChecking = no」s.SSH_OPTS + =「-o UserKnownHostsFile =/dev/null「但仍顯示相同的錯誤。我不知道如何將公鑰和私鑰傳遞給登錄信息。 –

+0

我已經成功完成了使用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()'。 –

相關問題