From 99389f75ae0b9ab594f8b7e7bfc0fe255b512900 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Thu, 6 Jun 2013 18:17:47 +0100 Subject: Add tty= option to cliapp.ssh_runcmd() This option allows to request or disable pseudo-tty allocation when calling ssh and can be useful e.g. when running sudo at the remote end. --- cliapp/runcmd.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cliapp/runcmd.py b/cliapp/runcmd.py index 41878f5..7565bbd 100644 --- a/cliapp/runcmd.py +++ b/cliapp/runcmd.py @@ -242,6 +242,19 @@ def ssh_runcmd(target, argv, **kwargs): # pragma: no cover machine. The command is given as an argv array; elements in the array are automatically quoted so they get passed to the other side correctly. + + An optional ``tty=`` parameter can be passed to ``ssh_runcmd`` in + order to force or disable pseudo-tty allocation. This is often + required to run ``sudo`` on another machine and might be useful + in other situations as well. Supported values are ``tty=True`` for + forcing tty allocation, ``tty=False`` for disabling it and + ``tty=None`` for not passing anything tty related to ssh. + + With the ``tty`` option, + ``cliapp.runcmd(['ssh', '-tt', 'user@host', '--', 'sudo', 'ls'])`` + can be written as + ``cliapp.ssh_runcmd('user@host', ['sudo', 'ls'], tty=True)`` + which is more intuitive. The target is given as-is to ssh, and may use any syntax ssh accepts. @@ -255,6 +268,16 @@ def ssh_runcmd(target, argv, **kwargs): # pragma: no cover ''' - local_argv = ['ssh', target, '--'] + map(shell_quote, argv) + tty = kwargs.get('tty', None) + if tty: + ssh_cmd = ['ssh', '-tt', target, '--'] + elif tty is False: + ssh_cmd = ['ssh', '-T', target, '--'] + else: + ssh_cmd = ['ssh', target, '--'] + if 'tty' in kwargs: + del kwargs['tty'] + + local_argv = ssh_cmd + map(shell_quote, argv) return runcmd(local_argv, **kwargs) -- cgit v1.2.1