Added parameter to override defauilt cmd timeout in pexpect

* Added cmd_timeout/timeout parameter to allow ability to overwrite
  pexpect's default value of 30s.

Change-Id: I6c532bd05ea3f10d70708198bdcb330450b87c2c
This commit is contained in:
Christopher Hunt 2016-04-27 11:16:23 -05:00
parent 78ad9acb01
commit 9ee7b8248f
1 changed files with 16 additions and 5 deletions

View File

@ -136,7 +136,7 @@ class SshMixin(object):
def ssh_to_target(
self, target_ip=None, user=None, password=None, cmds=None,
proxy_user=None, proxy_pswd=None, proxy_ip=None,
close_when_done=True, response_obj=None):
close_when_done=True, response_obj=None, cmd_timeout=None):
"""
SSH to the target host from the specified host. If target_ip is not
specified, the response_obj with an open connection must be provided.
@ -168,6 +168,7 @@ class SshMixin(object):
object.
:param response_obj: Provided SshResponse Object a for open
connection to pass cmds to...
:param cmd_timeout: Timeout per command. Default = 30s (as per pexpect)
:return: SSH Response object
"""
@ -234,7 +235,10 @@ class SshMixin(object):
# If there are commands to execute
if cmds is not None:
response_obj = self._cmds_via_open_connection(response_obj, cmds)
args = {'response_obj': response_obj, 'cmds': cmds}
if cmd_timeout is not None:
args['timeout'] = cmd_timeout
response_obj = self._cmds_via_open_connection(**args)
self.last_response = response_obj
@ -399,7 +403,8 @@ class SshMixin(object):
return response_obj
def execute_cmds_via_open_connection(
self, connection, cmds, response_obj=None, close_conn=False):
self, connection, cmds, response_obj=None, close_conn=False,
timeout=None):
"""
Execute the list of commands on the open connection
@ -407,6 +412,7 @@ class SshMixin(object):
@param cmds: list of commands to execute
@param response_obj: The SSH Response object; instantiated if !provided
@param close_conn: (Boolean), Close the connection when done?
@param timeout: (int) Max number of seconds to wait per command
@return: Populated response object
"""
@ -418,19 +424,23 @@ class SshMixin(object):
response_obj.connection = connection
args = {'response_obj': response_obj, 'cmds': cmds}
if timeout is not None:
args['timeout'] = timeout
response_obj = self._cmds_via_open_connection(**args)
if close_conn:
self.close_connections(response_obj)
return response_obj
def _cmds_via_open_connection(self, response_obj, cmds):
def _cmds_via_open_connection(self, response_obj, cmds, timeout=30):
"""
SSH from the local host using pexpect.
@param response_obj: Populated SshResponse Obj
@param cmds: Ordered Dict of commands to execute on the host to
validate connection
@param timeout: Amount of time allowed per cmd (default: 30s, which
is the default for pexpect)
@return: SshResponse Obj
@ -453,7 +463,8 @@ class SshMixin(object):
# Watch connection for potential and expected output.
try:
response = ssh_process.expect(expectations.keys())
response = ssh_process.expect(
expectations.keys(), timeout=timeout)
# TIMEOUT, break out of loop and indicate FAILURE
except pexpect.TIMEOUT: