Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paramiko hangs on get after ownloading 20 MB of file

I am in need of python sftp client to download files from a sftp server. I started to use Paramiko. Small files in KB works well but however when I try to download 600 MB of file, it hangs indefinitely after downloading 20 MB of file. Unable to figure out what the issue is. Increasing the window size did not solve either. Any help would be much appreciated!

host = config.getsafe(section, "host")
username = config.getsafe(section, "username")
port = config.getsafe(section, "port")
remote_dir = config.getsafe(section, "remote_dir")
download_dir = config.getsafe(section, "download_dir")
archive_dir = config.getsafe(section, "archive_dir") if config.has_option(section, "archive_dir") else \
    None
password = config.getsafe(section, "password") if config.has_option(section, "password") else None
file_pattern = config.getsafe(section, "file_pattern") if config.has_option(section, "file_pattern") \
    else "*"
passphrase = config.getsafe(section, "passphrase") if config.has_option(section, "passphrase") else None
gnupg_home = config.getsafe(section, "gnupg_home") if config.has_option(section, "gnupg_home") else None

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, port=int(port), username=username, password=password)

sftp = ssh.open_sftp()
sftp.sshclient = ssh

sftp.get("/SFTP/PL_DEV/test.dat", "C:/import/download/test.dat")
like image 540
Ram Avatar asked Mar 08 '23 05:03

Ram


2 Answers

I did two things to solve a similar problem:

  1. increase window size – you say you tried that too; for me, this helped to get from a few ten MBs to half a GB but no further

  2. effectively disable rekeying – this might have security implications, but helped me to get files over a GB from a weird windows sftp server

    with paramiko.Transport((_SFTP['host'], 22)) as transport:
        # SFTP FIXES
        transport.default_window_size=paramiko.common.MAX_WINDOW_SIZE
        transport.packetizer.REKEY_BYTES = pow(2, 40)  # 1TB max, this is a security degradation!
        transport.packetizer.REKEY_PACKETS = pow(2, 40)  # 1TB max, this is a security degradation!
        # / SFTP FIXES
    
        transport.connect(username=_SFTP['user'], password=_SFTP['password'])
            with paramiko.SFTPClient.from_transport(transport) as sftp:
                listdir = sftp.listdir()
                # ...
                sftp.get(remotepath=filename, localpath=localpath)
    
like image 58
törzsmókus Avatar answered Mar 24 '23 02:03

törzsmókus


Increasing default_max_packet_size and default_window_size as follows worked for me:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()

client.connect(hostname, username=username, password=password, port=port)
tr = client.get_transport()
tr.default_max_packet_size = 100000000
tr.default_window_size = 100000000
sftp = client.open_sftp()
sftp.get(remote_file, local_filepath)

client.close()
like image 26
gustavengstrom Avatar answered Mar 24 '23 03:03

gustavengstrom