1
0
Fork 0

Allow default users

master
LEdoian 5 years ago
parent 8d97393257
commit 0cefb6a2b4

@ -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) # We now check the separate lines and possibly tweak them (e.g. by removing sudo-s)
lines = s[1].split('\n') lines = s[1].split('\n')
directory = None # Don't cd by default 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 = [] result = []
for line in lines: 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() line = line.strip()
if line.startswith('cd'): if line.startswith('cd'):
parts = line.split() parts = line.split()
@ -90,7 +90,6 @@ def parse_snippet(s: Tuple[str, str]) -> Sequence[Command]:
continue continue
else: else:
# No sudo or cd, just a command. # No sudo or cd, just a command.
user = 'root' # Reset to default
cmd = line cmd = line
result.append(Command(user=user,directory=directory,command=cmd,context=context)) result.append(Command(user=user,directory=directory,command=cmd,context=context))
# Let's have the previously processed commands in the 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: with tempfile.NamedTemporaryFile(mode='r+') as tmpfile:
# Fill the file with data # Fill the file with data
for cmd in hunk: 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 tmpfile.file.flush() # Should not be needed, but better safe than sorry
# Let the user edit the file # Let the user edit the file
import os import os
@ -165,10 +164,11 @@ def user_action(hunk: Sequence[Command], context=None) -> Sequence[Command]:
def run_command(cmd: Command): def run_command(cmd: Command):
# Generate and run the SSH command. # Generate and run the SSH command.
global host global host
local_command = ['ssh', host] local_command = ['ssh']
remote_command = cmd.command remote_command = cmd.command
if cmd.user is not None: if cmd.user is not None:
local_command.extend(['-l', cmd.user]) 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: if cmd.directory is not None:
remote_command = f'cd {cmd.directory}; {remote_command}' remote_command = f'cd {cmd.directory}; {remote_command}'
local_command.append(remote_command) local_command.append(remote_command)

Loading…
Cancel
Save