ssh.py: add alloc_tty parameter to run_ssh_command(), add logging

This commit is contained in:
InsanePrawn 2022-08-11 06:23:06 +02:00
parent bd5415de47
commit 068009185c

23
ssh.py
View file

@ -19,7 +19,11 @@ def cmd_ssh(cmd: list[str], user: str, host: str, port: int):
run_ssh_command(list(cmd), user=user, host=host, port=port) run_ssh_command(list(cmd), user=user, host=host, port=port)
def run_ssh_command(cmd: list[str] = [], user: Optional[str] = None, host: str = SSH_DEFAULT_HOST, port: int = SSH_DEFAULT_PORT): def run_ssh_command(cmd: list[str] = [],
user: Optional[str] = None,
host: str = SSH_DEFAULT_HOST,
port: int = SSH_DEFAULT_PORT,
alloc_tty: bool = False):
if not user: if not user:
user = config.get_profile()['username'] user = config.get_profile()['username']
keys = find_ssh_keys() keys = find_ssh_keys()
@ -28,15 +32,19 @@ def run_ssh_command(cmd: list[str] = [], user: Optional[str] = None, host: str =
extra_args += ['-i', keys[0]] extra_args += ['-i', keys[0]]
if config.runtime['verbose']: if config.runtime['verbose']:
extra_args += ['-v'] extra_args += ['-v']
logging.info(f'Opening SSH connection to {host}') if alloc_tty:
return subprocess.run([ extra_args += ['-t']
logging.info(f'Opening SSH connection to {(user + "@") if user else ""}{host} ({port})')
full_cmd = [
'ssh', 'ssh',
] + extra_args + SSH_COMMON_OPTIONS + [ ] + extra_args + SSH_COMMON_OPTIONS + [
'-p', '-p',
str(port), str(port),
f'{user}@{host}', f'{user}@{host}',
'--', '--',
] + cmd) ] + cmd
logging.debug(f"running cmd: {full_cmd}")
return subprocess.run(full_cmd)
def scp_put_files(src: list[str], dst: str, user: str = None, host: str = SSH_DEFAULT_HOST, port: int = SSH_DEFAULT_PORT): def scp_put_files(src: list[str], dst: str, user: str = None, host: str = SSH_DEFAULT_HOST, port: int = SSH_DEFAULT_PORT):
@ -46,14 +54,17 @@ def scp_put_files(src: list[str], dst: str, user: str = None, host: str = SSH_DE
key_args = [] key_args = []
if len(keys) > 0: if len(keys) > 0:
key_args = ['-i', keys[0]] key_args = ['-i', keys[0]]
return subprocess.run([ cmd = [
'scp', 'scp',
] + key_args + SSH_COMMON_OPTIONS + [ ] + key_args + SSH_COMMON_OPTIONS + [
'-P', '-P',
str(port), str(port),
] + src + [ ] + src + [
f'{user}@{host}:{dst}', f'{user}@{host}:{dst}',
]) ]
logging.info(f"Copying files to {user}@{host}:{dst}:\n{src}")
logging.debug(f"running cmd: {cmd}")
return subprocess.run(cmd)
def find_ssh_keys(): def find_ssh_keys():