2

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.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • 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 Oct 30 '17 at 08:10
  • 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 . –  Oct 30 '17 at 08:17
  • A Google search on *paramiko sftp* gave me this other [SO post](https://stackoverflow.com/a/3635163/3545273). – Serge Ballesta Oct 30 '17 at 08:22
  • Possible duplicate of [paramiko's sshclient with sftp](https://stackoverflow.com/questions/3635131/paramikos-sshclient-with-sftp) – Serge Ballesta Oct 30 '17 at 08:22
  • 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. –  Oct 30 '17 at 08:30

1 Answers1

0

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)
Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846