Tor代理ssh的Python Paramiko脚本

时间:2017-10-16 10:13:41

标签: python ssh proxy paramiko tor

我使用带有Paramiko模块的Python脚本实现了代理ssh连接,并使用了这个简单的代码:

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
proxy_cmd = "connect -R remote -5 -S 127.0.0.1:9050 %h %p"
proxy = paramiko.ProxyCommand(proxy_cmd)
ssh.connect(iptarget, username=user, password=p, sock=proxy)

现在我将使用不同的用户名对同一个iptarget执行不同的尝试。我添加到前面的代码a for以便为用户列表进行ssh连接,即:

listUser=["bob", "user", "1234"]
for user in listUser:
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  proxy_cmd = "connect -R remote -5 -S 127.0.0.1:9050 %h %p"
  proxy = paramiko.ProxyCommand(proxy_cmd)
  ssh.connect(iptarget, username=user, password=p, sock=proxy)
  time.sleep(50)

但是我遇到了以下异常:

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 546, in _read_timeout
raise socket.timeout()
socket.timeout

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

Exception: ('connect -R remote -5 -S 127.0.0.1:9050 %h %p', 'Broken pipe')
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/proxy.py", line 66, in send
self.process.stdin.write(content)
BrokenPipeError: [Errno 32] Broken pipe

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 1852, in run
self.packetizer.write_all(b(self.local_version + '\r\n'))
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 309, in write_all
n = self.__socket.send(out)
File "/usr/local/lib/python3.5/dist-packages/paramiko/proxy.py", line 72, in send
raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
paramiko.ssh_exception.ProxyCommandFailure: ('connect -R remote -5 -S 127.0.0.1:9050 %h %p', 'Broken pipe')

我认为因为我发起了许多连接,位于代理命令中,但我不知道如何解决问题。

我更新了代码,这是有效的:

  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  proxy_cmd = "connect -R remote -5 -S 127.0.0.1:9050 'targetip' 22"
  proxy = paramiko.ProxyCommand(proxy_cmd)
  ssh.connect(iptarget, username=user, password=p, sock=proxy)

但它仍然无法在for循环中运行:

listUser=["bob", "user", "1234"]
for user in listUser:
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  proxy_cmd = "connect -R remote -5 -S 127.0.0.1:9050 192.1.1.1 22"
  proxy = paramiko.ProxyCommand(proxy_cmd)
  ssh.connect(iptarget, username=user, password=p, sock=proxy)
  time.sleep(50)

在这种情况下,我得到了这些错误:

Exception: ('connect -R remote -4 -S 127.0.0.1:9150 192.1.1.1 22', 'Broken pipe')
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/proxy.py", line 66, in send
self.process.stdin.write(content)
BrokenPipeError: [Errno 32] Broken pipe

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 1852, in run
self.packetizer.write_all(b(self.local_version + '\r\n'))
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 309, in write_all
n = self.__socket.send(out)
File "/usr/local/lib/python3.5/dist-packages/paramiko/proxy.py", line 72, in send
raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
paramiko.ssh_exception.ProxyCommandFailure: ('connect -R remote -4 -S 127.0.0.1:9150 192.1.1.1 22', 'Broken pipe')

1 个答案:

答案 0 :(得分:0)

您可以使用 banner_timeout 来避免此问题。

ssh.connect(iptarget, username=user, password=p, sock=proxy, banner_timeout=120)