def exit_config_mode(self, *args, **kwargs): """Disable config edit-mode for Nokia SR OS""" output = self._exit_all() # Model-driven CLI if "@" in self.base_prompt and "(ex)[" in output: # Asterisk indicates changes were made. if "*(ex)[" in output: log.warning("Uncommitted changes! Discarding changes!") output += self._discard() cmd = "quit-config" self.write_channel(self.normalize_cmd(cmd)) output += self.read_until_pattern(pattern=re.escape(cmd)) if self.check_config_mode(): raise ValueError("Failed to exit configuration mode") return output
def run_ttp_template(connection, template, res_kwargs, **kwargs): """ Helper function to run TTP template parsing. :param connection: Netmiko connection object :type connection: obj :param template: TTP template :type template: str :param res_kwargs: ``**res_kwargs`` arguments for TTP result method :type res_kwargs: dict :param kwargs: ``**kwargs`` for TTP object instantiation :type kwargs: dict """ if not TTP_INSTALLED: msg = "\nTTP is not installed. Please PIP install ttp:\n" "pip install ttp\n" raise ValueError(msg) parser = ttp(template=template, **kwargs) # get inputs load for TTP template ttp_inputs_load = parser.get_input_load() log.debug("run_ttp_template: inputs load - {}".format(ttp_inputs_load)) # go over template's inputs and collect output from devices for template_name, inputs in ttp_inputs_load.items(): for input_name, input_params in inputs.items(): method = input_params.get("method", "send_command") method_kwargs = input_params.get("kwargs", {}) commands = input_params.get("commands", None) # run sanity checks if method not in dir(connection): log.warning( "run_ttp_template: '{}' input, unsupported method '{}', skipping" .format(input_name, method)) continue elif not commands: log.warning( "run_ttp_template: '{}' input no commands to collect, skipping" .format(input_name)) continue # collect commands output from device output = [ getattr(connection, method)(command_string=command, **method_kwargs) for command in commands ] output = "\n".join(output) # add collected output to TTP parser object parser.add_input(data=output, input_name=input_name, template_name=template_name) # run parsing in single process parser.parse(one=True) return parser.result(**res_kwargs)