Paramiko Authentication Failed? - python

I have tried to fix it up but got the same error every time. I have tried several times also tried in google but cannot give a suitable answer.
def ssh(hostname, command):
sshcon = paramiko.SSHClient()
logging.basicConfig()
sshcon.load_system_host_keys()
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshcon.connect(hostname, username='root', password='password')
stdin, stdout, stderr = sshcon.exec_command(command)
return stdout.read()
while True:
i = 1
while i <= 60:
print "|------- Webserver2 -----------|"
print time.ctime()
response_time(desired_response_time, url2, webserver2, bilal2, local)
list=ssh(bilal2, "xm list")
if "webserver1" in list:
horizontal('2')
print "|------- Webserver1 -----------|"
response_time(desired_response_time, url1, webserver1, bilal2, local)
else:
horizontal('1')
pass
time.sleep(5)
i += 1
The error
Error:
Traceback (most recent call last):
File "newcodepid.py", line 472, in <module>
list=ssh(bilal2, "xm list")
File "newcodepid.py", line 61, in ssh
sshcon.connect(hostname, username='root', password='password')
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 332, in connect
self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 493, in _auth
raise saved_exception
paramiko.AuthenticationException: Authentication failed.

Related

Is there a way to not print any traceback error message in Python

I have some piece of code like this
import paramiko
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP, username=myusername,password=mypassword,timeout=3)
except:
print ("[-] Wrong : "+ip+" : "+username+" : "+password)
And when I run it, it keeps giving tracebacks about SSH problem such as this:
Traceback (most recent call last):
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
I would like to know if it is possible to not print at all on the screen any Traceback messages?
Thanks
Here's the full error:
Traceback (most recent call last):
File "test123.py", line 50, in function1
client.connect(ip, username=myusername, password=mypassword,timeout=3)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/client.py", line 380, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/client.py", line 621, in _auth
raise saved_exception
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/client.py", line 608, in _auth
self._transport.auth_password(username, password)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/transport.py", line 1271, in auth_password
return self.auth_handler.wait_for_response(my_event)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/auth_handler.py", line 208, in wait_for_response
raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/work.py", line 920, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/work.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "test123.py", line 56, in slaveWork
except paramiko.ssh_exception:
TypeError: catching classes that do not inherit from BaseException is not allowed
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/transport.py", line 1888, in _check_banner
buf = self.packetizer.readline(timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/packet.py", line 331, in readline
buf += self._read_timeout(timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/packet.py", line 498, in _read_timeout
raise EOFError()
EOFError
You could just finish your try and just do a general catch and then do nothing with it.
import paramiko
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP, username=myusername,password=mypassword,timeout=3)
except Exception as e:
pass
Probably the best alternative would be to use the tracebacklimit module.
For Python 2.x you could do:
import sys
import paramiko
sys.tracebacklimit = 0
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP, username=myusername,password=mypassword,timeout=3)
except:
print ("[-] Wrong : "+ip+" : "+username+" : "+password)
For Python 3.5 (tracebacklimit) some people have reported that this should be set to None to work, i.e.:
sys.tracebacklimit = None
For multiple exception raised some people reported is not guaranteed to work (In Python, how do I print an error message without printing a traceback and close the program when a condition is not met?).

paramiko sftp can't delete remote folder, ioerror

This is my code, to delete a remote directory using paramiko sftp.
import paramiko
host = "192.168.1.13"
port = 22
transport = paramiko.Transport((host, port))
username = "root"
password = "abc123"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
filepath = '/root/test_folder'
sftp.rmdir(filepath)
Execute above code will output this error,
Traceback (most recent call last):
File "autom_test.py", line 36, in <module>
sftp.rmdir(filepath)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 390, in rmdir
self._request(CMD_RMDIR, path)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 729, in _request
return self._read_response(num)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 776, in _read_response
self._convert_status(msg)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 806, in _convert_status
raise IOError(text)
IOError: Failure
This is not the case when I'm using sftp.remove(path) for a single file. But sftp.rmdir causing IOError
The syntax is from the documentation.
The error is because the destination directory has got files inside it.
Try recurssive delete instead.. See below..
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host,username=username,password=password)
filepath="/root/test_folder"
cmd = "rm -rf " + filepath
stdin, stdout, stderr = ssh.exec_command(cmd)
while not stdout.channel.exit_status_ready():
time.sleep(5)

Paramiko ssh with tor sock proxy

I trying use tor proxy for ssh.
First simple connection without ~/.ssh/config
~$ ssh -p <SERVER_POST> <USER>#<SERVER_HOST>
<USER>#<SERVER_HOST>'s password:
~$ echo $SSH_CONNECTION
<MY_REAL_HOST> <MY_REAL_PORT> <SERVER_HOST> <SERVER_PORT>
Second with next ~/.ssh/config:
Host *
ProxyCommand connect -4 -S 127.0.0.1:9050 $(tor-resolve %h 127.0.0.1:9050) %p
and connect:
~$ ssh -p <SERVER_POST> <USER>#<SERVER_HOST>
<USER>#<SERVER_HOST>'s password:
~$ echo $SSH_CONNECTION
<NOT_MY_REAL_HOST> <NOT_MY_REAL_HOST> <SERVER_HOST> <SERVER_PORT>
Now with paramiko:
>>> import paramiko
>>> client = paramiko.SSHClient()
>>> proxy = None
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname=host, username=user, password=password,
... port=port, sock=proxy)
>>> stdin, stdout, stderr = client.exec_command('echo $SSH_CONNECTION')
>>> print((stdout.read() + stderr.read()).decode('utf8'))
<MY_REAL_HOST> <MY_REAL_PORT> <SERVER_HOST> <SERVER_PORT>
All work fine. But when I try set up proxy:
>>> import paramiko
>>> client = paramiko.SSHClient()
>>> proxy = paramiko.ProxyCommand(
... 'connect -4 -S 127.0.0.1:9050 $(tor-resolve %h 127.0.0.1:9050) %p')
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname=host, username=user, password=password,
... port=port, sock=proxy)
>>> stdin, stdout, stderr = client.exec_command('echo $SSH_CONNECTION')
>>> print((stdout.read() + stderr.read()).decode('utf8'))
I have next exception for python 3.4:
Exception: Error reading SSH protocol bannersequence item 0: expected str instance, bytes found
Traceback (most recent call last):
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 1535, in _check_banner
buf = self.packetizer.readline(timeout)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/packet.py", line 271, in readline
buf += self._read_timeout(timeout)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/packet.py", line 424, in _read_timeout
x = self.__socket.recv(128)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/proxy.py", line 93, in recv
result = ''.join(self.buffer)
TypeError: sequence item 0: expected str instance, bytes found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 1412, in run
self._check_banner()
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 1539, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol bannersequence item 0: expected str instance, bytes found
Traceback (most recent call last):
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 1535, in _check_banner
buf = self.packetizer.readline(timeout)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/packet.py", line 271, in readline
buf += self._read_timeout(timeout)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/packet.py", line 424, in _read_timeout
x = self.__socket.recv(128)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/proxy.py", line 93, in recv
result = ''.join(self.buffer)
TypeError: sequence item 0: expected str instance, bytes found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tbicr/Project/registrator/main.py", line 70, in <module>
client.connect(hostname=host, username=user, password=password, port=port, sock=proxy)
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/client.py", line 242, in connect
t.start_client()
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 346, in start_client
raise e
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 1412, in run
self._check_banner()
File "/home/tbicr/Project/env34/lib/python3.4/site-packages/paramiko/transport.py", line 1539, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.s
For python 2.7 same exception with other stacktrace:
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "/home/tbicr/Project/registrator/main.py", line 70, in <module>
client.connect(hostname=host, username=user, password=password, port=port, sock=proxy)
File "/home/tbicr/Project/env27/local/lib/python2.7/site-packages/paramiko/client.py", line 242, in connect
t.start_client()
File "/home/tbicr/Project/env27/local/lib/python2.7/site-packages/paramiko/transport.py", line 339, in start_client
raise e
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
So connection with ssh client and tor work fine, but with paramiko not.
Why it's not work and how I can fix it for python?

paramiko.SSHException: Channel closed

I try to use python 2.7.1 and paramiko 1.12.0:
connection = paramiko.SSHClient()
connection.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection.connect(hostIP, 22, 'd', 'd')
command = 'ls'
(stdin, stdout, stderr) = connection.exec_command(command)
response = stdout.readlines()
errormsg = stderr.read()
But I get this error message:
Traceback (most recent call last):<br>
File "./ssh.py", line 32, in <module><br>
(stdin, stdout, stderr) = connection.exec_command(command)<br>
File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/client.py", line 379, in exec_command<br>
chan.exec_command(command)<br>
File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/channel.py", line 218, in exec_command<br>
self._wait_for_event()<br>
File "/app/python/2.7.1/LMWP3/lib/python2.7/site-packages/paramiko/channel.py", line 1129, in _wait_for_event<br>
raise e<br>
paramiko.SSHException: Channel closed.
import paramiko
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn = s.connect(hostIP, username ='root', password='rootpassword', port=22)
command = 'pwd'
(stdin, stdout, stderr) = s.exec_command(command)
print stdout.read()
s.close()
This should work fine with root user on linux. If it's not, you are probably passing wrong values for hostIP (for ex: quotes in the value), username, password.

Paramiko SFTP problems when no password is used

I need to upload some files using SFTP, this works from the command line:
$sftp myuser#my_remote_host
Connected to my_remote_host
sftp>
This is my Paramiko script:
#!/usr/bin/env python
import paramiko
import sys
import os
host = "my_remote_host"
port = 22
transport = paramiko.Transport((host, port))
username = "myuser"
LOCAL_PATH = "/tmp/"
REMOTE_PATH = "/dcs/tmp/"
FILE = "myfile"
transport.connect(username = username)
sftp = paramiko.SFTPClient.from_transport(transport)
path = LOCAL_PATH + FILE
sftp.put(LOCAL_PATH + FILE, REMOTE_PATH + FILE)
sftp.close()
transport.close()
print 'Upload done.'
When executing I get this error:
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "./my_sftp.py", line 19, in ?
sftp = paramiko.SFTPClient.from_transport(transport)
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 102, in from_transport
chan = t.open_session()
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 655, in open_session
return self.open_channel('session')
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 745, in open_channel
raise e
EOFError
When adding a private key I get this error:
path = os.path.join(os.environ['HOME'], '.ssh', 'id_dsa')
key = paramiko.DSSKey.from_private_key_file(path)
transport.connect(username = username, pkey=key)
Traceback (most recent call last):
File "./my_sftp.py", line 24, in ?
transport.connect(username = username, pkey=key)
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 1007, in connect
self.auth_publickey(username, pkey)
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 1234, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "/usr/lib/python2.4/site-packages/paramiko/auth_handler.py", line 174, in wait_for_response
raise e
paramiko.AuthenticationException: Authentication failed.
In your first example, you can't authenticate with only a username, so the session can't be started.
I can't tell why your privatekey example isn't working without some more information. Is it possible that its the incorrect key for that server? SSH on the command line may be trying multiple keys, or getting it from an agent.
Anyway, it easier to start off with the SSHClient class. It will wrap up all the authentication, and host verification pieces in one package. It also has an open_sftp() convenience method to return an SFTPClient instance.

Categories

Resources