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?
Related
Can you please assist. I am doing some channeling from my windows machine via python. I could not get into the remote desktop. I am using ssh, but I know this seems to be wrong. This is working to jumpbox which is linux based.
My machine(windows) > remote desktop(VM machine) > network device via ssh
CODE:
for device in devices:
device = device.rstrip()
#for jump in jumphost_devices:
print("Connecting to Device %s..." % (device),)
jhost = paramiko.SSHClient()
jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jhost.connect('10.10.10.11', username=username, password=password, timeout=10)
vmtransport = jhost.get_transport()
dest_addr = (device, 22)
local_addr = ('10.10.10.11', 22)
vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr)
#
host = paramiko.SSHClient()
host.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host.connect(device, username=username, password=password, sock=vmchannel, timeout=10)
remote_conn = host.invoke_shell()
#output = remote_conn.recv(1000)
ERROR:
C:\Users\xxxx\Documents\PYTHON>python push2.py device.txt command.txt logs
Enter Username: EV24049584
Password:
Retype your password:
Connecting to Device 10.120.120.138...
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\packet.py", line 622, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 2039, in run
self._check_banner()
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 2215, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\packet.py", line 622, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "push2.py", line 129, in <module>
main()
File "push2.py", line 125, in main
ssh_access_command(sys.argv[1], sys.argv[2], sys.argv[3])
File "push2.py", line 89, in ssh_access_command
jhost.connect('10.138.113.11', username=username, password=password, timeout=10)
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\client.py", line 406, in connect
t.start_client(timeout=timeout)
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 660, in start_client
raise e
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 2039, in run
self._check_banner()
File "C:\Users\xbsds\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paramiko\transport.py", line 2215, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
thanks,
I am attempting to connect to one of our SFTP servers in order to automate a report. We pull a file from this server once a week and I am not able to get this connections to work. I am not finding any useful references to the below traceback error. Anyone have an idea of what could be causing this problem?
I get a socket.timeout error after 2 or 3 seconds of executing the code.
import paramiko
cli = paramiko.SSHClient()
cli.set_missing_host_key_policy(paramiko.AutoAddPolicy())
cli.connect(hostname='AHOST', port=21, username="USER", password="PASS")
stdin_, stdout_, stderr_ = cli.exec_command("ls -l ~")
print(stdout_.readlines())
cli.close()
Traceback:
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2138, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 367, in readline
buf += self._read_timeout(timeout)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 576, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 1966, in run
self._check_banner()
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2143, in _check_banner
"Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2138, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 367, in readline
buf += self._read_timeout(timeout)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\packet.py", line 576, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/user_name/PycharmProjects/PDD_Report/MAIN/MAIN.py", line 12, in <module>
cli.connect(hostname='A40T', port=21, username="user_name", password="gate001")
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\client.py", line 397, in connect
t.start_client(timeout=timeout)
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 587, in start_client
raise e
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 1966, in run
self._check_banner()
File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\paramiko\transport.py", line 2143, in _check_banner
"Error reading SSH protocol banner" + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
You are connecting to FTP port 21.
SSH/SFTP uses port 22 (what is the default value of the port parameter, which you override).
Side note 1: Your question is about SFTP. Yet, instead of using SFTPClient, you use SSHClient and execute shell commands – That has nothing to do with SFTP.
Side note 2: Do not use AutoAddPolicy like this. You
lose security by doing so.
See Paramiko "Unknown Server".
I use Paramiko Python module to write a simple multiple ssh connections throw:
import paramiko
import time
i=0
while i<20 :
ssh = paramiko.SSHClient()
starttime=time.clock()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('127.0.0.1', username="user", password="password")
except:
endtime=time.clock()
except OSError:
print("Os error")
except paramiko.SSHException:
print("Ssh error")
total=endtime-starttime
print(total)
i=i+1
The problem is that the code does not capture all the raised exceptions, i.e. :
Exception: Error reading SSH protocol banner[Errno 104] Connection reset by peer
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2000, in _check_banner
buf = self.packetizer.readline(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 353, in readline
buf += self._read_timeout(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 531, in _read_timeout
x = self.__socket.recv(128)
ConnectionResetError: [Errno 104] Connection reset by peer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1854, in run
self._check_banner()
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2005, in _check_banner
'Error reading SSH protocol banner' + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 104] Connection reset by peer
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2000, in _check_banner
buf = self.packetizer.readline(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 353, in readline
buf += self._read_timeout(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 533, in _read_timeout
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1854, in run
self._check_banner()
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2005, in _check_banner
'Error reading SSH protocol banner' + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
I would capture all the raised exceptions in order to total control my std-output.
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.
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?).