Need Proxy?

BotProxy: Rotating Proxies Made for professionals. Really fast connection. Built-in IP rotation. Fresh IPs every day.

Find out more


how to give sftp proxy command in python script using paramiko?

Question

I want to use proxy command in python using paramiko.

proxy command:

sftp -o "ProxyCommand /usr/bin/nc --proxy proxy.somegateway.com:8080 %h %p"

but when I use this directly, I am able to connect sftp server.

but if I want to use this proxy command in a Python script I get the issue below:

My script:

>>> import paramiko
>>> cmd = '/usr/bin/ssh proxy.somegateway.com:8080'
>>> proxy = paramiko.ProxyCommand(cmd)
>>> proxy
<paramiko.proxy.ProxyCommand object at 0x23b3bd0>
>>> client = paramiko.SSHClient()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname='some.host.com', username='myuser', password='secretpassword', sock=proxy)
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 366, in connect
    t.start_client(timeout=timeout)
  File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 510, in start_client
    raise e
paramiko.ssh_exception.ProxyCommandFailure: ('/usr/bin/ssh proxy.somegateway.com:8080', 'Broken pipe')

I am getting ProxyCommandFailure issue.

Answer

It should be like this:

target_host = 'sftp.foo.com'
target_port = 22
proxy = paramiko.proxy.ProxyCommand(
    '/usr/bin/nc --proxy proxy.bar.com:8080 %s %d' \
    % (target_host, target_port) )
client.connect(hostname=target_host, port=target_port, password='...', sock=proxy)
#                                                                      ^^^^^^^^^^

See paramiko's [doc] for details.

cc by-sa 3.0