From b37799c8823a67174f6bd43f122fd5fdb05cc956 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 9 Apr 2019 13:10:11 -0500 Subject: [PATCH] Added implementations for all functions --- copy_cert.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/copy_cert.py b/copy_cert.py index f03f205..a729a98 100644 --- a/copy_cert.py +++ b/copy_cert.py @@ -1,20 +1,37 @@ #!/usr/bin/python +import paramiko import argparse +import os +from subprocess import list2cmdline def connection_from_config(name): """Create a connection from a Host entry in the .ssh/config file""" - pass - - -def scp(connection, src, dst): + config = paramiko.config.SSHConfig() + cp = os.path.expanduser('~/.ssh/config') + with open(cp) as file: + config.parse(file) + info = config.lookup(name) + info['username'] = info.pop('user') + info['key_filename'] = info.pop('identityfile') + info['port'] = int(info['port']) + client = paramiko.client.SSHClient() + client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) + client.connect(**info) + return client + + +def scp(connection: paramiko.sftp_client.SFTPClient, src, dst): """Function to copy files from src to dst using connection""" - pass + connection.put(src, dst) -def exec_remote(connection, command): +def exec_remote(connection: paramiko.SSHClient, command): """Function to execute specified command on conection""" - pass + if not isinstance(command, str): + command = list2cmdline(command) + stdin, stdout, stderr = connection.exec_command(command) + return stdout, stderr if __name__ == "__main__":