Skip to content

Instantly share code, notes, and snippets.

@solebox
Created August 9, 2017 08:37
Show Gist options
  • Save solebox/bc96905108633e15a60cf8b236e04523 to your computer and use it in GitHub Desktop.
Save solebox/bc96905108633e15a60cf8b236e04523 to your computer and use it in GitHub Desktop.
import paramiko
import contextlib
@contextlib.contextmanager
def open_sftp(hostname, port, username, password):
# open ssh/sftp connection:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=hostname, port=port, username=username, password=password)
sftp = client.open_sftp()
yield sftp
finally:
sftp.close()
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment