2

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.

1

2 Answers 2

2

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.

5
  • Hi pynexj, Thanks for your reply. its connecting now but when i try to run ls command i am getting the message " This service allows sftp connections only." the execution command is :
    – user4941703
    Oct 30, 2017 at 6:52
  • (stdin, stdout, stderr) = client.exec_command("ls") for line in stdout.readlines(): print(line)
    – user4941703
    Oct 30, 2017 at 6:53
  • based on the error message the remote SSH server only allow SFTP so you cannot call exec_command(). you can verify this by manually ssh'ing to it.
    – pynexj
    Oct 30, 2017 at 7:26
  • I have crossed the question limit so i am unable to raise new one. can you please guide me for this problem This service allows sftp connections only
    – user4941703
    Oct 30, 2017 at 7:34
  • i myself never configured sftp only servers. you need to check SSH conf on the server side. found a link: serverfault.com/questions/354615/allow-sftp-but-disallow-ssh
    – pynexj
    Oct 30, 2017 at 7:36
0

Just do not forget to add hostname and port to the proxy command

target_host = 'foo'
target_port = 22
proxy = paramiko.proxy.ProxyCommand('/usr/bin/nc --proxy proxy.bar.com:8080 %s %d' % (target_hostname, target_port) )

more info here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.