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.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • 6
    Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) – adiga Nov 09 '17 at 09:51

2 Answers2

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.

pynexj
  • 19,215
  • 5
  • 38
  • 56
  • 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 : –  Oct 30 '17 at 06:52
  • (stdin, stdout, stderr) = client.exec_command("ls") for line in stdout.readlines(): print(line) –  Oct 30 '17 at 06: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 '17 at 07: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 –  Oct 30 '17 at 07:34
  • i myself never configured sftp only servers. you need to check SSH conf on the server side. found a link: https://serverfault.com/questions/354615/allow-sftp-but-disallow-ssh – pynexj Oct 30 '17 at 07: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

Aziz Alto
  • 19,057
  • 5
  • 77
  • 60