Skip to content

Instantly share code, notes, and snippets.

@cowlinator
Last active March 12, 2022 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowlinator/552ee27cd436e5f79c214107e4a779e6 to your computer and use it in GitHub Desktop.
Save cowlinator/552ee27cd436e5f79c214107e4a779e6 to your computer and use it in GitHub Desktop.
Paramiko SFTP Hello World
import paramiko
def get(host, user, passw, remote_path, local_path, public_host_key_filepath=None):
with paramiko.SSHClient() as ssh:
if public_host_key_filepath:
# secure
ssh.load_host_keys(public_host_key_filepath)
ssh.set_missing_host_key_policy(paramiko.RejectPolicy())
else:
# not secure
ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
# if False:
# especially insecure. Just don't.
# ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# Please note that opening a new connection for every `get` operation is
# inefficient and creates unnecessary network traffic. To improve this,
# leave the connection open while performing all needed operations
ssh.connect(host, username=user, password=passw)
with ssh.open_sftp() as sftp:
sftp.get(remote_path, local_path)
finally:
ssh.close()
if __name__ == "__main__":
get("fake.host.url", "fake_user", "12345", "remote_path", "local_path")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment