Skip to content

Instantly share code, notes, and snippets.

@rubanm
Created June 19, 2013 21:27
Show Gist options
  • Save rubanm/5818236 to your computer and use it in GitHub Desktop.
Save rubanm/5818236 to your computer and use it in GitHub Desktop.
Paramiko SFTP username/password authentication via SOCKS proxy
import paramiko
import socket
import socks
# set up SOCKS proxy
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_details['host'],
proxy_details['port'], True, proxy_details['username'],
proxy_details['password'])
socket.socket = socks.socksocket
# initialize paramiko ssh client
transport = paramiko.SSHClient()
# ignore missing host key as we rely on username/password auth
transport.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect() automatically uses the SOCKS proxy socket
transport.connect(auth_credentials['host'], username = auth_credentials['user_name'],
password = auth_credentials['password'])
# start SFTP Client from SSH transport
sftp = paramiko.SFTPClient.from_transport(transport.get_transport())
# do stuff
# cleanup
sftp.close()
transport.close()
@contang0
Copy link

Does it still work for you? I use python 3 and I get the following error:
GeneralProxyError: Socket error: SOCKS5 proxy server sent invalid data

@goinnn
Copy link

goinnn commented Mar 30, 2020

Currently, at least, with Python 3.6 works

@jonanem
Copy link

jonanem commented Sep 14, 2021

When i am using the same code i am getting error as hostname not known, Any idea on how to fix this issue

@jonanem
Copy link

jonanem commented Sep 14, 2021

Do we need to have some code as, paramiko.client.socket.socket = socks.socksocket which is mentioned in http://vozis.blogspot.com/2015/01/python-sftp-with-paramiko-via-socks.html

And also our proxy server allows HTTP only not SOCKS can you please let me know if i need to make any changes in the above code apart from adding socks.PROXY_TYPE_HTTP in setdefaultproxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment