无法使用python的Paramiko库将远程计算机作为根用户进行SSH

无法使用python的Paramiko库将远程计算机作为根用户进行SSH,python,authentication,ssh,root,paramiko,Python,Authentication,Ssh,Root,Paramiko,我正在尝试以根用户身份建立到远程计算机的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 con

我正在尝试以根用户身份建立到远程计算机的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')

适用于我的示例脚本:

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)

适用于我的示例脚本:

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)

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