Skip to content

Instantly share code, notes, and snippets.

@dcragusa
Created September 19, 2018 11:45
Show Gist options
  • Save dcragusa/c321806cb632e17ce0f7c682b4a5ed5e to your computer and use it in GitHub Desktop.
Save dcragusa/c321806cb632e17ce0f7c682b4a5ed5e to your computer and use it in GitHub Desktop.
An SFTP client based on Paramiko, with utility function to download a whole folder tree
import os
import stat
from paramiko import SSHClient, SFTPClient, AutoAddPolicy
from tools.file_tools import make_775_dir, file_exists
join = os.path.join
def get_sftp_client(host, username=None, password=None):
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(host, username=username, password=password)
sftp = SFTPClient.from_transport(ssh.get_transport())
return sftp
def download_all_files(sftp, remote_dir, local_dir):
for f in sftp.listdir_attr(remote_dir):
file = f.filename
if stat.S_ISDIR(f.st_mode):
# print('Going into %s' % join(remote_dir, file))
download_all_files(sftp, join(remote_dir, file), join(local_dir, file))
else:
if not file_exists(join(local_dir, file)):
if not os.path.isdir(local_dir):
make_775_dir(local_dir)
# print('Downloading %s' % join(remote_dir, file))
sftp.get(join(remote_dir, file), join(local_dir, file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment