Paramiko ssh与tor sock代理

时间:2014-05-18 17:41:51

标签: python ssh proxy paramiko tor

我尝试使用tor proxy代替ssh。

没有~/.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>

第二个~/.ssh/config

Host *
ProxyCommand connect -4 -S 127.0.0.1:9050 $(tor-resolve %h 127.0.0.1:9050) %p

并连接:

~$ 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>

现在使用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>

一切正常。但是当我尝试设置代理时:

>>> 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'))

我有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

对于python 2.7与其他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

因此与ssh客户端和tor的连接工作正常,但paramiko没有。

为什么它不起作用以及如何为python修复它?

0 个答案:

没有答案