From 0384c7462ff94ed5b0b752a07b24be916f868cf9 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 25 May 2021 17:19:04 -0500 Subject: [PATCH] Removed scp_[get|put] and fixed connection_from_config --- .gitignore | 2 ++ openwrt_backup/utils.py | 51 ++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index d494556..e834ebe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /.dir-locals.el +*.egg-info +*.pyc diff --git a/openwrt_backup/utils.py b/openwrt_backup/utils.py index b42378b..5a227e8 100755 --- a/openwrt_backup/utils.py +++ b/openwrt_backup/utils.py @@ -1,34 +1,49 @@ import paramiko -import os +from pathlib import Path from subprocess import list2cmdline -def connection_from_config(name): +def connection_from_config(name: str, ssh_config_root: Path): """Create a connection from a Host entry in the .ssh/config file""" config = paramiko.config.SSHConfig() - cp = os.path.expanduser("~/.ssh/config") + cp = ssh_config_root / "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"]) + info["username"] = info.pop("user", None) + info["key_filename"] = info.pop("identityfile", None) + info["port"] = int(info.pop("port", 22)) + params = dict( + hostname=None, + port=22, + username=None, + password=None, + pkey=None, + key_filename=None, + timeout=None, + allow_agent=True, + look_for_keys=True, + compress=False, + sock=None, + gss_auth=False, + gss_kex=False, + gss_deleg_creds=True, + gss_host=None, + banner_timeout=None, + auth_timeout=None, + gss_trust_dns=True, + passphrase=None, + disabled_algorithms=None, + ) + for key, value in info.items(): + if key in params.keys(): + params[key] = value client = paramiko.client.SSHClient() - client.load_host_keys(os.path.expanduser("~/.ssh/known_hosts")) - client.connect(**info) + client.load_host_keys(ssh_config_root / "known_hosts") + client.connect(**params) return client -def scp_put(connection: paramiko.sftp_client.SFTPClient, src, dst): - """Function to copy files from src to dst using connection (local to remote)""" - connection.put(src, dst) - - -def scp_get(connection: paramiko.sftp_client.SFTPClient, src, dst): - """Function to copy files from src to dst using connection (remote to local)""" - connection.get(src, dst) - - def exec_remote(connection: paramiko.SSHClient, command): """Function to execute specified command on conection""" if not isinstance(command, str):