From fea32de3d38659d8b7ea91e34a56eff3c560e10b Mon Sep 17 00:00:00 2001 From: Sergei Trofimov Date: Fri, 10 Apr 2015 09:03:48 +0100 Subject: [PATCH] Parameterizing password prompt for sudo in Ssh interface. On some devices, sudo presents a different prompt when asking for a password. All the prompt to be specified in device configruation to handle such cases. --- wlauto/common/linux/device.py | 6 ++++-- wlauto/utils/ssh.py | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/wlauto/common/linux/device.py b/wlauto/common/linux/device.py index 6b651095..7ddaba24 100644 --- a/wlauto/common/linux/device.py +++ b/wlauto/common/linux/device.py @@ -744,6 +744,8 @@ class LinuxDevice(BaseLinuxDevice): Parameter('password', description='Password for the account on the device (for password-based auth).'), Parameter('keyfile', description='Keyfile to be used for key-based authentication.'), Parameter('port', kind=int, description='SSH port number on the device.'), + Parameter('password_prompt', default='[sudo] password', + description='Prompt presented by sudo when requesting the password.'), Parameter('use_telnet', kind=boolean, default=False, description='Optionally, telnet may be used instead of ssh, though this is discouraged.'), @@ -793,7 +795,7 @@ class LinuxDevice(BaseLinuxDevice): self._is_rooted = None def validate(self): - if not self.password and not self.keyfile: + if self.password is None and not self.keyfile: raise ConfigError('Either a password or a keyfile must be provided.') if self.working_directory is None: # pylint: disable=access-member-before-definition if self.username == 'root': @@ -823,7 +825,7 @@ class LinuxDevice(BaseLinuxDevice): self.reset() def connect(self): # NOQA pylint: disable=R0912 - self.shell = SshShell(timeout=self.default_timeout) + self.shell = SshShell(password_prompt=self.password_prompt, timeout=self.default_timeout) self.shell.login(self.host, self.username, self.password, self.keyfile, self.port, telnet=self.use_telnet) self._is_ready = True diff --git a/wlauto/utils/ssh.py b/wlauto/utils/ssh.py index d0e89f13..dfa2beb3 100644 --- a/wlauto/utils/ssh.py +++ b/wlauto/utils/ssh.py @@ -83,7 +83,10 @@ class TelnetConnection(pxssh.pxssh): class SshShell(object): - def __init__(self, timeout=10): + default_password_prompt = '[sudo] password' + + def __init__(self, password_prompt=None, timeout=10): + self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt self.timeout = timeout self.conn = None @@ -135,7 +138,7 @@ class SshShell(object): if log: logger.debug(command) self.conn.sendline(command) - index = self.conn.expect_exact(['[sudo] password', TIMEOUT], timeout=0.5) + index = self.conn.expect_exact([self.password_prompt, TIMEOUT], timeout=0.5) if index == 0: self.conn.sendline(self.password) timed_out = not self.conn.prompt(timeout)