From ef4cefcd6769efaef6079bd9331414e986e56238 Mon Sep 17 00:00:00 2001 From: Guillaume Chazarain Date: Mon, 17 Jan 2011 23:36:58 +0100 Subject: [PATCH] Added a --user option to specify the remote user to login as. --- NEWS | 5 +++++ gsh.1 | 3 +++ gsh/main.py | 2 ++ gsh/remote_dispatcher.py | 2 ++ tests/tests/command_line.py | 7 +++++++ 5 files changed, 19 insertions(+) diff --git a/NEWS b/NEWS index 385c964..65fd650 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +0.3.2 +~~~~~ +o Added a --user option to specify the remote user to login as. + + 0.3.1 ~~~~~ diff --git a/gsh.1 b/gsh.1 index 5a34c40..e5f17e7 100644 --- a/gsh.1 +++ b/gsh.1 @@ -32,6 +32,9 @@ Command to execute on the remote shells. This starts \fIgsh\fR in non\-interacti \fB\-\-ssh=SSH\fR Ssh command to use [exec ssh -oLogLevel=Quiet -t %(host)s exec bash --noprofile]. By default, \fIgsh\fR spawns lightweight remote shells using the ssh command, but another shell command can be specified here. For example, with \-\-ssh='usleep $((RANDOM*50)); exec ssh' a delay will be introduced to avoid all hosts accessing a NFS server at the same time. If the hostname should not be added at the end of the command, the macro %(host)s can be inserted where the hostname should be placed. Also, make sure the command you use launchs a pty, this may need the \-t option for ssh. .TP +\fB\-\-user=USER\fR +Remote user to log in as. When specified, \fIgsh\fR will ssh to USER@HOST instead of simply HOST. +.TP \fB\-\-password\-file=FILE\fR read a password from the specified file. - is the tty. This can be used when public key authentication is not available, either write the password in a file, or set FILE to - so that \fIgsh\fR will prompt for a password. .TP diff --git a/gsh/main.py b/gsh/main.py index b0b3d05..8e5d6f4 100644 --- a/gsh/main.py +++ b/gsh/main.py @@ -64,6 +64,8 @@ def parse_cmdline(): def_ssh = 'exec ssh -oLogLevel=Quiet -t %(host)s exec bash --noprofile' parser.add_option('--ssh', type='str', dest='ssh', default=def_ssh, metavar='SSH', help='ssh command to use [%s]' % def_ssh) + parser.add_option('--user', type='str', dest='user', default=None, + help='remote user to log in as', metavar='USER') parser.add_option('--password-file', type='str', dest='password_file', default=None, metavar='FILE', help='read a password from the specified file. - is ' + diff --git a/gsh/remote_dispatcher.py b/gsh/remote_dispatcher.py index c1ef595..2e927cf 100644 --- a/gsh/remote_dispatcher.py +++ b/gsh/remote_dispatcher.py @@ -94,6 +94,8 @@ class remote_dispatcher(buffered_dispatcher): def launch_ssh(self, name): """Launch the ssh command in the child process""" + if options.user: + name = '%s@%s' % (options.user, name) evaluated = options.ssh % {'host': name} if evaluated == options.ssh: evaluated = '%s %s' % (evaluated, name) diff --git a/tests/tests/command_line.py b/tests/tests/command_line.py index ac18a31..f83cd9a 100644 --- a/tests/tests/command_line.py +++ b/tests/tests/command_line.py @@ -82,3 +82,10 @@ class TestCommandLine(unittest.TestCase): child.expect('Error talking to unknown_host') child.expect(pexpect.EOF) + def testUser(self): + child = launch_gsh(['--ssh=echo', 'machine']) + child.expect('[^@]machine') + child.expect(pexpect.EOF) + child = launch_gsh(['--ssh=echo', '--user=login', 'machine']) + child.expect('login@machine') + child.expect(pexpect.EOF) -- 2.11.4.GIT