def on_session_start(self, session, logger): """ :param session: :param logger: :return: """ # open automation authorization if needed if self._open_automation_auth: self._logger.info("Open Automation Authorization") test_password = self._api.DecryptPassword( self.resource_config.test_user_password).Value if test_password: cli_service = CliServiceImpl(session=session, command_mode=self.default_mode, logger=logger) command = CommandTemplateExecutor( cli_service=cli_service, command_template=ctrl_command_templates. OPEN_AUTOMATION_AUTHORIZATION, action_map={ "[Pp]assword for user": lambda session, logger: session.send_line( test_password, logger) }) command.execute_command( test_user=self.resource_config.test_user)
def load(self, source_file, vrf=None, action_map=None, error_map=None): load_cmd = CommandTemplateExecutor(self._cli_service, ios_xr_cmd_templates.LOAD, action_map=action_map, error_map=error_map) if vrf: load_result = load_cmd.execute_command(source_file=source_file, vrf=vrf) else: load_result = load_cmd.execute_command(source_file=source_file) match_success = re.search( r"[\[\(][1-9][0-9]*[\)\]].*bytes|^([1-9][0-9]*)+\s*bytes\s*(parsed|process+ed)", load_result, re.IGNORECASE | re.MULTILINE) if not match_success: error_str = "Failed to restore configuration, please check logs" match_error = re.search( r" Can't assign requested address|[Ee]rror:.*\n|%.*$", load_result, re.IGNORECASE | re.MULTILINE) if match_error: error_str = 'load error: ' + match_error.group() raise Exception('validate_load_success', error_str) return load_result
def configure_snmp_v3(self, snmp_access, user, auth_type, auth_protocol, auth_password, priv_protocol, priv_password): """ Configure SNMP v3 """ CommandTemplateExecutor(self._cli_service, enable_disable_snmp.CONFIGURE_V3_VIEW).execute_command(view_name=self.VIEW) CommandTemplateExecutor(self._cli_service, enable_disable_snmp.CONFIGURE_V3_GROUP).execute_command(group_name=self.GROUP, auth_type=auth_type, snmp_access=snmp_access, view_name=self.VIEW) CommandTemplateExecutor(self._cli_service, enable_disable_snmp.CONFIGURE_V3_USER).execute_command(user_name=user) command = CommandTemplateExecutor(self._cli_service, enable_disable_snmp.CONFIGURE_V3_AUTH) command.update_action_map({r"Password:"******"Password:": lambda session, logger: session.send_line(priv_password, logger)}) command.execute_command(user_name=user, priv_protocol=priv_protocol)
def _modify_monitor_ports(self, association_name, monitor_ports): association_attributes = self._associations_table.get(association_name) if association_attributes.get(self.MONITOR_PORTS) != monitor_ports: command_executor = CommandTemplateExecutor( self._cli_service, command_template.MODIFY_MONITOR_PORTS) command_executor.execute_command(name=association_name, ports=','.join(monitor_ports)) association_attributes[self.MONITOR_PORTS] = monitor_ports
def commit(self): """ :return: """ command = CommandTemplateExecutor( cli_service=self._cli_service, command_template=command_templates.COMMIT) command.execute_command()
def execute_flow(self, license_server_ip): """ :param str license_server_ip: :return: """ with self._cli_handler.get_cli_service( self._cli_handler.cli_mode) as cli_session: command = CommandTemplateExecutor( cli_service=cli_session, command_template=ctrl_command_templates. CONFIGURE_LICENSE_SERVER) command.execute_command(license_server_ip=license_server_ip)
def override_running( self, path, vrf=None, action_map=None, error_map=None, timeout=300, reconnect_timeout=1600, ): """Override running-config. :param path: relative path to the file on the remote host tftp://server/sourcefile :param action_map: actions will be taken during executing commands, i.e. handles yes/no prompts :param error_map: errors will be raised during executing commands, i.e. handles Invalid Commands errors :raise Exception: """ command_template = CommandTemplateExecutor( self._cli_service, configuration.CONFIGURE_REPLACE, action_map=action_map, error_map=error_map, timeout=timeout, check_action_loop_detector=False, ) try: if vrf: with self._cli_service.enter_mode(AristaVrfCommandMode(vrf)): output = command_template.execute_command(path=path) else: output = command_template.execute_command(path=path) match_error = re.search(r"[Ee]rror.*", output, flags=re.DOTALL) if match_error: error_str = match_error.group() raise CommandExecutionException( "Override_Running", "Configure replace completed with error: " + error_str, ) except ExpectedSessionException as e: self._logger.warning(e.args) if isinstance(e, CommandExecutionException): raise self._cli_service.reconnect(reconnect_timeout)
def map_clear(self, slot_id, port): """ Clear bidirectional mapping :param ports: :return: """ # connection = " ".join(ports) executor = CommandTemplateExecutor(self._cli_service, command_template.DEL_CONN) output = executor.execute_command(slot_id=slot_id, connection=port) return output
def map_clear(self, ports): """ Clear bidirectional mapping :param ports: :return: """ output = "" executor = CommandTemplateExecutor(self._cli_service, command_template.MAP_CLEAR) for port in ports: output += executor.execute_command(port=port) return output
def map_clear_to(self, src_port, dst_ports): """ Clear unidirectional mapping :param src_port: :param dst_ports :return: """ executor = CommandTemplateExecutor(self._cli_service, command_template.MAP_CLEAR_TO) output = '' for dst_port in dst_ports: output += executor.execute_command(src_port=src_port, dst_port=dst_port) return output
def map_uni(self, master_port, slave_ports): logical_master_id = self._get_logical(master_port) self._validate_port(logical_master_id) command_executor = CommandTemplateExecutor(self._cli_service, command_template.MAP_UNI) exception_messages = [] for slave_port in slave_ports: try: logical_slave_id = self._get_logical(slave_port) self._validate_port(logical_slave_id) command_executor.execute_command( master_ports=logical_master_id, slave_ports=logical_slave_id, name='{0}-uni-{1}'.format(logical_master_id, logical_slave_id)) except Exception as e: if len(e.args) > 1: exception_messages.append(e.args[1]) elif len(e.args) == 1: exception_messages.append(e.args[0]) if exception_messages: raise Exception(self.__class__.__name__, ', '.join(exception_messages))
def map_bidi(self, slot_id, src_port, dst_port): """ Unidirectional mapping :param src_port: :param dst_ports: :return: """ try: connection = "{}:{}".format(dst_port, int(src_port)) except: connection = "{}:{}".format(src_port, int(dst_port)) executor = CommandTemplateExecutor(self._cli_service, command_template.SET_CONN) output = executor.execute_command(slot_id=slot_id, connection=connection) return output
def _remove_association(self, association_name): if association_name: command_executor = CommandTemplateExecutor( self._cli_service, command_template.MAP_CLEAR) command_executor.execute_command(name=association_name) del self._associations_table[association_name]