Python Connect over HTTP proxy with pysftp

后端 未结 3 1894
太阳男子
太阳男子 2020-12-18 07:12

Currently, I am doing SFTP transfer using Python subprocess.POPEN and PuTTY psftp.exe.

It is working, but not really clean nor transportabl

相关标签:
3条回答
  • 2020-12-18 07:34

    try this:

    import paramiko
    
    use_proxy = True
    
    # PROXY
    host_proxy = ''
    port_proxy = ''
    
    # SFTP
    host = ''
    port = 22
    username = ''
    password = ''
    
    try:
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        
        if host_proxy and use_proxy:
        # find the path to nc on the command line: "which nc"
            proxy = paramiko.ProxyCommand(f"/bin/nc --proxy {host_proxy}:{port_proxy} {host} {port}") 
            trans = paramiko.Transport(proxy)
        else:
            trans = paramiko.Transport((host, port))
        trans.connect(username=username, password=password)
        sftp = paramiko.SFTPClient.from_transport(trans)
    except Exception as e:
        print(e)
    
    
    sftp.listdir('.')
    
    sftp.close()
    trans.close()
    

    Remember to install ncat if you run in docker, in debian. Not install netcat, it's the older version and does not work well.

    apt-get -y update && apt-get -y install ncat
    
    0 讨论(0)
  • 2020-12-18 07:35

    I do not think that the pysftp supports proxies. Though note that the pysftp is just a wrapper around Paramiko library, which does support proxies.

    So I suggest you to use Paramiko directly.

    For a start see How to ssh over HTTP proxy in Python Paramiko?, particularly the answer by @tintin.


    To authenticate to the proxy, after the CONNECT command, add a Proxy-Authorization header like:

    Proxy-Authorization: Basic <credentials>
    

    where the <credentials> is base-64 encoded string username:password.

    auth = 'Basic ' + base64.encodebytes("username:password".encode()).decode()
    args = ("123.123.123.255", 23, auth)
    cmd_connect = "CONNECT {}:{} HTTP/1.1\r\nProxy-Authorization: {}\r\n\r\n".format(*args)
    
    0 讨论(0)
  • 2020-12-18 07:47

    In my case, I do this:

    import pysftp
    import paramiko
    
    hostname, prot = 'some.host.name', 22
    proxy = paramiko.proxy.ProxyCommand('/usr/bin/nc --proxy proxy.foobar:8080 %s %d' % (hostname, port))
    t = paramiko.Transport(sock=proxy)
    t.connect(username='abc', password='123')
    
    sftp = paramiko.SFTPClient.from_transport(t) # back to pysftp wrapper
    sftp.listdir('.')
    
    0 讨论(0)
提交回复
热议问题