I am trying to establish an SSH connection to a remote machine as a root. I get a series of errors when I execute the SSH command:
ssh.connect(host, port=22, username=username, password=password)
Errors:
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.
I can SSH the same remote machine as a normal user. This error comes only when I try to login as a root. Please provide a solution. Here is my code I have written.
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')
Sample script that worked for me :
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.