From e5dadce20fa33fa9ef2f2162bf51518951458c58 Mon Sep 17 00:00:00 2001 From: Christopher Hunt Date: Thu, 14 Apr 2016 11:14:25 -0500 Subject: [PATCH] Added 'Connection Refused' support + better error checking * Connection refused condition not handled, until now. * Better error checking (connection refused was accepted as pass) * Small typo in docstring Change-Id: Ibb297560c080b22ca05eeca908e178bbc0f7363b --- .../networks/common/proxy_mgr/proxy_mgr.py | 2 +- .../networks/common/proxy_mgr/ssh_util.py | 40 +++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/cloudcafe/networking/networks/common/proxy_mgr/proxy_mgr.py b/cloudcafe/networking/networks/common/proxy_mgr/proxy_mgr.py index 002fdc57..4127aee1 100755 --- a/cloudcafe/networking/networks/common/proxy_mgr/proxy_mgr.py +++ b/cloudcafe/networking/networks/common/proxy_mgr/proxy_mgr.py @@ -43,7 +43,7 @@ class NetworkProxyMgr(PingMixin, SshMixin): @param ip_version: Version to use by default, if utilities differ across IP versions. @param logger: Logging functionality. - @param debug: (Boolean) Used for debugging system and mixin utiliies + @param debug: (Boolean) Used for debugging system and mixin utilities @return: None """ diff --git a/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py b/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py index aadec13c..c4183d31 100644 --- a/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py +++ b/cloudcafe/networking/networks/common/proxy_mgr/ssh_util.py @@ -70,6 +70,7 @@ class SshMixin(object): PSWD_PROMPT_REGEX = r'ssword\:\s*' LINUX_PROMPT_REGEX = r'[$#>]\s*$' SSH_KEY_REGEX = r'connecting\s+\(yes\/no\)\?\s*' + REFUSED = r'connection\s+refused' DEFAULT_CMD = 'ls -alF' @@ -124,7 +125,7 @@ class SshMixin(object): output = self.ssh_to_target( target_ip=target_ip, user=user, password=password, cmds=[cmd]) self.last_response = output - return cmd in output.output + return cmd in output.output and output.errors == '' def ssh_to_target( self, target_ip=None, user=None, password=None, cmds=None, @@ -270,6 +271,7 @@ class SshMixin(object): (pexpect.TIMEOUT, None), (self.PSWD_PROMPT_REGEX, password), (self.SSH_KEY_REGEX, 'yes'), + (self.REFUSED, None), (self.LINUX_PROMPT_REGEX, None)]) # Set ssh process from the open connection @@ -308,6 +310,7 @@ class SshMixin(object): err_msg = err.format( ssh_process.before, ssh_process.after) self.logger.error(err_msg) + response_obj.errors += '{0}\n'.format(err_msg) # Record IO and remove IP from the tracking list response_obj.add_to_stdout( @@ -332,8 +335,22 @@ class SshMixin(object): # FAILURE... if response == 0: err = "SSH'ing target timed out. {0} --> {1}" - self.logger.error(err.format( - ssh_process.before, ssh_process.after)) + err_msg = err.format(ssh_process.before, ssh_process.after) + self.logger.error(err_msg) + response_obj.errors += '{0}\n'.format(err_msg) + response_obj.add_to_stdout( + str(ssh_process.before) + str(ssh_process.after)) + self._conn_path.pop() + if not self._conn_path: + response_obj.connection = None + break + + # Connection refused + if response == 3: + err = "Connection Refused. {0} --> {1}" + err_msg = err.format(ssh_process.before, ssh_process.after) + self.logger.error(err_msg) + response_obj.errors += '{0}\n'.format(err_msg) response_obj.add_to_stdout( str(ssh_process.before) + str(ssh_process.after)) self._conn_path.pop() @@ -344,6 +361,9 @@ class SshMixin(object): # Connection established, the expected prompt was found # (last element in the expectation ordered dict) if response == len(expectations.keys()) - 1: + if 'connection refused' in ssh_process.before.lower(): + response_obj.errors += 'Connection Refused: {0}\n'.format( + ssh_process.before) response_obj.add_to_stdout( str(ssh_process.before) + str(ssh_process.match.group())) break @@ -406,9 +426,12 @@ class SshMixin(object): # TIMEOUT, break out of loop and indicate FAILURE except pexpect.TIMEOUT: err = "CMD '{cmd}' timed out. {before} --> {after}" - self.logger.error(err.format( + err_msg = err.format( before=ssh_process.before, after=ssh_process.after, - cmd=cmd)) + cmd=cmd) + self.logger.error(err_msg) + response_obj.errors += '{0}\n'.format(err_msg) + self.logger.debug('Connection Hop Path: {0}'.format( self._conn_path)) @@ -435,10 +458,11 @@ class SshMixin(object): # indicate FAILURE... if response == 0: err = "CMD '{cmd}' timed out. {before} --> {after}" - self.logger.error(err.format( + err_msg = err.format( before=ssh_process.before, after=ssh_process.after, - cmd=cmd)) - + cmd=cmd) + self.logger.error(err_msg) + response_obj.errors += '{0}\n'.format(err_msg) response_obj.add_to_stdout( str(ssh_process.before) + str(ssh_process.after)) self.logger.debug('Connection Hop Path: {0}'.format(