I realize a proxy ssh connection using a Python script with Paramiko module with this simple code:
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)
Now I would perform different attempts to the same iptarget with different username. I add to the previous code a for in order to do ssh connection for a list of user, i.e. :
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)
but I get into the following exception:
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')
I think because I launch many connect, located in proxy command, but I do not know how to fix the problem.
I update the code, and it is work:
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)
But it still does not work in a for loop:
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)
In this case I obtain these errors:
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')