【问题标题】:Unable to SSH a remote machine as a root using Paramiko library of python无法使用 python 的 Paramiko 库以 root 身份对远程计算机进行 SSH
【发布时间】:2015-10-27 07:58:55
【问题描述】:

我正在尝试以 root 身份建立与远程计算机的 SSH 连接。执行 SSH 命令时出现一系列错误:

ssh.connect(host, port=22, username=username, password=password)  

错误:

ssh.connect(host, username=username, password=password) File  
"C:\Python34\lib\site-packages\paramiko\client.py", line 307, in  
connect look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)     

File "C:\Python34\lib\site-packages\paramiko\client.py",
line 519, in _auth   raise saved_exception      

File "C:\Python34\lib\site-packages\paramiko\client.py", line 510, in _auth
self._transport.auth_password(username, password) 

File "C:\Python34\lib\site-packages\paramiko\transport.py", line 1168, in
auth _password  return self.auth_handler.wait_for_response(my_event)  

File "C:\Python34\lib\site-packages\paramiko\auth_handler.py", line
208, in wa it_for_response      raise e    

paramiko.ssh_exception.AuthenticationException: Authentication failed.

我可以像普通用户一样 SSH 同一台远程机器。仅当我尝试以 root 身份登录时才会出现此错误。请提供解决方案。这是我写的代码。

ssh = paramiko.SSHClient()    
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
    try:        
        ssh.connect(host, port=22, username=username, password=password)  
        print ('connecting to wag.... '+host)      
    except:  
        print ('Cannot SSH WAG : '+host+'Check the connection or parameters supplied')

【问题讨论】:

  • 如果远程机器上/etc/ssh/sshd_config 中的PermitRootLogin 设置为no,您将无法通过ssh 以root 身份登录。你能先检查一下吗?
  • 你可以使用独立的 SSH 客户端(例如 OpenSSH ssh 命令行客户端)以 root 身份连接吗?
  • 我检查了机器的 sshd_config 并且 PermitRootLogin 设置为“YES”。
  • 嗨 Martin,我使用 putty 连接到远程机器并以普通用户身份登录,使用特殊命令进入开发人员模式并执行命令“enable-root”。然后我关闭连接,再次SSH同一台机器,这次我以root身份登录,它可以工作!与我编程的方式相同,但我的程序无法以 root 身份登录。正常登录发生。
  • 从安全角度来看,我强烈建议不要实施这种后门。无论如何,您专有的“启用根”脚本一定有问题。尝试启用 paramiko 调试日志记录。将腻子的 pcaps 与 paramiko 进行比较。它在哪里挂起/引发异常?你仍然得到身份验证失败的异常吗?检查你的 ssh 配置。

标签: python authentication ssh root paramiko


【解决方案1】:

对我有用的示例脚本:

import paramiko, socket
def check_hostname(host_name, pw_r):

        print ("Checking hostname :"+str(host_name))
        file_output = open('output_file','a')
        file_success = open('success_file','a')
        file_failed = open('failed_file','a')
        file_error = open('error_file','a')
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
          ssh.connect(host_name, username='root', password=pw_r, timeout=5)
          print ("Success")
          file_success.write(str(host_name+"\n"))
          file_success.close()
          file_output.write("success: "+str(host_name+"\n"))
          file_output.close()

          # printing output if required from remote machine
          #stdin,stdout,stderr = ssh.exec_command("hostname&&uptime")
          #for line in stdout.readlines():
           # print (line.strip())

        except paramiko.SSHException:
                print ("error")
                file_failed.write(str(host_name+"\n"))
                file_failed.close()
                file_output.write("failed: "+str(host_name+"\n"))
                file_output.close()
                #quit()
        except paramiko.ssh_exception.NoValidConnectionsError:
                print ("might be windows------------")
                file_output.write("failed: " + str(host_name + "\n"))
                file_output.close()
                file_failed.write(str(host_name+"\n"))
                file_failed.close()
                #quit()
        except socket.gaierror:
          print ("wrong hostname/dns************")
          file_output.write("error: "+str(host_name+"\n"))
          file_output.close()
          file_error.write(str(host_name + "\n"))
          file_error.close()

        except socket.timeout:
           print ("No Ping %%%%%%%%%%%%")
           file_output.write("error: "+str(host_name+"\n"))
           file_output.close()
           file_error.write(str(host_name + "\n"))
           file_error.close()

        ssh.close()

host_name = input("Enter Hostname: ")
pw_r = input("Enter password: ")
check_hostname(host_name,pw_r)

【讨论】: