|
|
|
@ -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): |
|
|
|
|