From 9ee7b8248f5f3016af3068ac373ba34d68c605c0 Mon Sep 17 00:00:00 2001 From: Christopher Hunt Date: Wed, 27 Apr 2016 11:16:23 -0500 Subject: [PATCH] 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 --- .../networks/common/proxy_mgr/ssh_util.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py b/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py index 71104f14..c8ea2885 100644 --- a/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py +++ b/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py @@ -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: