mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-01-31 10:11:17 +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:
parent
9e822c4b18
commit
c31d4ec8a3
@ -107,6 +107,7 @@ def check_keyfile(keyfile):
|
|||||||
class SshShell(object):
|
class SshShell(object):
|
||||||
|
|
||||||
default_password_prompt = '[sudo] password'
|
default_password_prompt = '[sudo] password'
|
||||||
|
max_cancel_attempts = 5
|
||||||
|
|
||||||
def __init__(self, password_prompt=None, timeout=10):
|
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.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))
|
logger.debug('Logging out {}@{}'.format(self.username, self.host))
|
||||||
self.conn.logout()
|
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):
|
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.
|
self.conn.prompt(0.1) # clear an existing prompt if there is one.
|
||||||
if as_root:
|
if as_root:
|
||||||
@ -179,6 +189,7 @@ class SshShell(object):
|
|||||||
command_index = output.find(command)
|
command_index = output.find(command)
|
||||||
output = output[command_index + len(command):].strip()
|
output = output[command_index + len(command):].strip()
|
||||||
if timed_out:
|
if timed_out:
|
||||||
|
self.cancel_running_command()
|
||||||
raise TimeoutError(command, output)
|
raise TimeoutError(command, output)
|
||||||
if strip_colors:
|
if strip_colors:
|
||||||
output = strip_bash_colors(output)
|
output = strip_bash_colors(output)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user