Created
August 9, 2017 08:37
-
-
Save solebox/bc96905108633e15a60cf8b236e04523 to your computer and use it in GitHub Desktop.
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 | |
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