Last active
March 12, 2022 01:00
-
-
Save cowlinator/552ee27cd436e5f79c214107e4a779e6 to your computer and use it in GitHub Desktop.
Paramiko SFTP Hello World
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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