From 0cefb6a2b4c40457997ae41d1712da58bfab56b5 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Mon, 4 May 2020 23:17:44 +0200 Subject: [PATCH] Allow default users --- markdownrunner.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/markdownrunner.py b/markdownrunner.py index 64f7c47..72adde5 100755 --- a/markdownrunner.py +++ b/markdownrunner.py @@ -64,9 +64,9 @@ def parse_snippet(s: Tuple[str, str]) -> Sequence[Command]: # We now check the separate lines and possibly tweak them (e.g. by removing sudo-s) lines = s[1].split('\n') directory = None # Don't cd by default - user = 'root' # Connect as root by default (it does not really matter, since the commands are run with sudo's) result = [] for line in lines: + user = None # This allows (in conjunction with the right order of arguments to 'ssh') to specify the default user in the hostname (as 'user@host.name') line = line.strip() if line.startswith('cd'): parts = line.split() @@ -90,7 +90,6 @@ def parse_snippet(s: Tuple[str, str]) -> Sequence[Command]: continue else: # No sudo or cd, just a command. - user = 'root' # Reset to default cmd = line result.append(Command(user=user,directory=directory,command=cmd,context=context)) # Let's have the previously processed commands in the context @@ -131,7 +130,7 @@ def user_action(hunk: Sequence[Command], context=None) -> Sequence[Command]: with tempfile.NamedTemporaryFile(mode='r+') as tmpfile: # Fill the file with data for cmd in hunk: - tmpfile.file.write(f"{cmd.user}\t{cmd.directory if cmd.directory else ''}\t{cmd.command}\n") + tmpfile.file.write(f"{cmd.user if cmd.user else ''}\t{cmd.directory if cmd.directory else ''}\t{cmd.command}\n") tmpfile.file.flush() # Should not be needed, but better safe than sorry # Let the user edit the file import os @@ -165,10 +164,11 @@ def user_action(hunk: Sequence[Command], context=None) -> Sequence[Command]: def run_command(cmd: Command): # Generate and run the SSH command. global host - local_command = ['ssh', host] + local_command = ['ssh'] remote_command = cmd.command if cmd.user is not None: local_command.extend(['-l', cmd.user]) + local_command.append(host) # SSH seems to honor the first specified parameter. This allows to have username as a part of the hostname, yet still be able to honor the sudo-s in the sinppets. if cmd.directory is not None: remote_command = f'cd {cmd.directory}; {remote_command}' local_command.append(remote_command)