def _wait_string(self, wait_str_ok_regex='', wait_str_invalid_regex=None, wait_str_failed_regex=None): """As equipment goes returning a string, makes a regex and verifies if string wished was returned.""" if wait_str_invalid_regex is None: wait_str_invalid_regex = self.INVALID_REGEX if wait_str_failed_regex is None: wait_str_failed_regex = self.ERROR_REGEX string_ok = 0 recv_string = '' while not string_ok: while not self.channel.recv_ready(): sleep(self.WAIT_FOR_CLI_RETURN) recv_string = self.channel.recv(9999) file_name_string = self.removeDisallowedChars(recv_string) for output_line in recv_string.splitlines(): if re.search(self.CURRENTLY_BUSY_WAIT, output_line): log.warning('Need to wait - Switch busy: %s' % output_line) raise plugin_exc.CurrentlyBusyErrorException() elif re.search(self.WARNING_REGEX, output_line): log.warning('Equipment warning: %s' % output_line) elif re.search(wait_str_invalid_regex, output_line): log.error('Equipment raised INVALID error: %s' % output_line) raise deploy_exc.InvalidCommandException(file_name_string) elif re.search(wait_str_failed_regex, output_line): log.error('Equipment raised FAILED error: %s' % output_line) raise deploy_exc.CommandErrorException(file_name_string) elif re.search(wait_str_ok_regex, output_line): log.debug('Equipment output: %s' % output_line) # test bug switch copying 0 bytes if output_line == '0 bytes successfully copied': log.debug('Switch copied 0 bytes, need to try again.') raise plugin_exc.CurrentlyBusyErrorException() string_ok = 1 elif re.search(self.VALID_TFTP_PUT_MESSAGE_OS10, output_line): log.debug('Equipment output: %s' % output_line) # test bug switch copying 0 bytes if output_line == 'Copy failed': log.debug('Switch copied 0 bytes, need to try again.') raise plugin_exc.CurrentlyBusyErrorException() string_ok = 1 return recv_string
def _copy_script_file_to_config(self, filename, destination='running-config'): """ Copy file from TFTP server to destination By default, plugin should apply file in running configuration (active) """ command = 'copy tftp://{}/{} {}\n\n'.format( self.tftpserver, filename, destination) file_copied = 0 retries = 0 while(not file_copied and retries < self.MAX_TRIES): if retries is not 0: sleep(self.RETRY_WAIT_TIME) try: log.info('try: %s - sending command: %s' % (retries, command)) self.channel.send('%s\n' % command) recv = self._wait_string(self.VALID_TFTP_PUT_MESSAGE) file_copied = 1 except plugin_exc.CurrentlyBusyErrorException: retries += 1 # not capable of configuring after max retries if retries is self.MAX_TRIES: raise plugin_exc.CurrentlyBusyErrorException() return recv
(python_command, filepath, e)) clean_command = 'delete %s' % (filename[-1]) confirm_command = 'Y' try: self.channel.send('%s\n' % clean_command) self.waitString(wait_str_ok_regex="[Y/N]") self.channel.send('%s\n' % confirm_command) self.waitString(wait_str_ok_regex="Done") except Exception, e: raise Exception("Error sending command %s to equipment %s: %s" % (python_command, filepath, e)) # not capable of configuring after max retries if retries is self.MAX_TRIES: raise exceptions.CurrentlyBusyErrorException() return recv @mock_return('') def ensure_privilege_level(self, privilege_level=None): self.channel.send('\n') self.waitString('>|#') def waitString(self, wait_str_ok_regex='>', wait_str_invalid_regex=None, wait_str_failed_regex=None): if wait_str_invalid_regex is None: