2
votes

I need to connect to SFTP server using proxy command.

I know how to connect to SFTP server directly:
paramiko's sshclient with sftp

I can open an SSH connection via proxy command using this code:

import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
target_host = 'sftp.XXXXX.co'
target_port = 22
proxy = paramiko.proxy.ProxyCommand('/usr/bin/nc --proxy proxy_host:8080 %s %d' % (target_host, target_port) )
client.connect(hostname=target_host,username='username', port=target_port, password='XXXXXXXX', sock=proxy)

But I need to create SFTPClient, not SSHClient. But I do not know how to pass the ProxyCommand to the SFTPClient.

1
Well the error is pretty self explanatory. You try to pass a command to the remote ssh server (ls), that means that you want to use the ssh interface while the server is configured to only allow sftp accesses. - Serge Ballesta
Thanks for your reply sergeBallesta. can you elaborate you answer with example. means to where i should use ls.Actually i want to read one of my sftp file using python . - user8854981
A Google search on paramiko sftp gave me this other SO post. - Serge Ballesta
I am connecting via proxycomamnd instaed of transport. and transport option is fine when we tried with locally. but when we tried with linux we have to use proxycomamnd option only. - user8854981

1 Answers

0
votes

To connect to SFTP server using a "custom socket", do:

proxy = paramiko.proxy.ProxyCommand(...)
transport = paramiko.Transport(proxy)
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)