1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-01-18 20:11:20 +00:00

ssh: fixing timeout behavior

Since a command would still be running on time out, it would prevent
issuing subsequent commands in the same SSH shell, make it look like
the device has become unresponsive.

If a timeout condition is his, send ^C to kill the current foreground
process and make the shell available for subsequent commands.
This commit is contained in:
Sergei Trofimov 2015-05-27 09:13:03 +01:00
parent 9e822c4b18
commit c31d4ec8a3

View File

@ -107,6 +107,7 @@ def check_keyfile(keyfile):
class SshShell(object):
default_password_prompt = '[sudo] password'
max_cancel_attempts = 5
def __init__(self, password_prompt=None, timeout=10):
self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt
@ -156,6 +157,15 @@ class SshShell(object):
logger.debug('Logging out {}@{}'.format(self.username, self.host))
self.conn.logout()
def cancel_running_command(self):
# simulate impatiently hitting ^C until command prompt appears
logger.debug('Sending ^C')
for _ in xrange(self.max_cancel_attempts):
self.conn.sendline(chr(3))
if self.conn.prompt(0.1):
return True
return False
def _execute_and_wait_for_prompt(self, command, timeout=None, as_root=False, strip_colors=True, log=True):
self.conn.prompt(0.1) # clear an existing prompt if there is one.
if as_root:
@ -179,6 +189,7 @@ class SshShell(object):
command_index = output.find(command)
output = output[command_index + len(command):].strip()
if timed_out:
self.cancel_running_command()
raise TimeoutError(command, output)
if strip_colors:
output = strip_bash_colors(output)