def show(self, commands): """Send configuration commands to a device. Args: commands (str, list): String with single command, or list with multiple commands. Raises: CommandError: Issue with the command provided. CommandListError: Issue with a command in the list provided. """ original_commands_is_str = isinstance(commands, str) if original_commands_is_str: commands = [commands] responses = [] for command in commands: if not command.startswith("show"): if original_commands_is_str: raise CommandError( command, 'Juniper "show" commands must begin with "show".') raise CommandListError( commands, command, 'Juniper "show" commands must begin with "show".') response = self.native.cli(command, warning=False) responses.append(response) if original_commands_is_str: return responses[0] return responses
def config(self, commands, format="set"): """Send configuration commands to a device. Args: commands (str, list): String with single command, or list with multiple commands. Raises: ConfigLoadError: Issue with loading the command. CommandError: Issue with the command provided, if its a single command, passed in as a string. CommandListError: Issue with a command in the list provided. """ if isinstance(commands, str): try: self.cu.load(commands, format=format) self.cu.commit() except ConfigLoadError as e: raise CommandError(commands, e.message) else: try: for command in commands: self.cu.load(command, format=format) self.cu.commit() except ConfigLoadError as e: raise CommandListError(commands, command, e.message)
def show(self, commands, raw_text=False): """Send configuration commands to a device. Args: commands (str, list): String with single command, or list with multiple commands. raw_text (bool, optional): False if encode should be json, True if encoding is text. Defaults to False. Raises: CommandError: Issue with the command provided. CommandListError: Issue with a command in the list provided. """ if not raw_text: encoding = "json" else: encoding = "text" original_commands_is_str = isinstance(commands, str) if original_commands_is_str: commands = [commands] try: response = self.native.enable(commands, encoding=encoding) response_list = self._parse_response(response, raw_text=raw_text) if original_commands_is_str: return response_list[0] return response_list except EOSCommandError as e: if original_commands_is_str: raise CommandError(e.commands, e.message) raise CommandListError(commands, e.commands[len(e.commands) - 1], e.message)
def show_list(self, commands): """ Send list of commands to device. Args: commands (list): Commands to be sent to device. Raises: CommandListError: Failure running command on device. Returns: list: Output from each command sent. """ self.enable() responses = [] entered_commands = [] for command in commands: entered_commands.append(command) try: responses.append(self._send_command(command)) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) return responses
def config_list(self, commands): """ Send multiple configuration commands to the device. Args: commands (list): The list of commands to send to the device. The commands should not include the "config" keyword. Example: >>> device = AIREOSDevice(**connection_args) >>> device.config_list(["interface hostname virtual wlc1.site.com", "config interface vlan airway 20"]) >>> Raises: COmmandListError: When the device's response indicates an error from sending one of the commands. """ self._enter_config() entered_commands = [] for command in commands: entered_commands.append(command) try: self._send_command(command) except CommandError as e: self.native.exit_config_mode() raise CommandListError(entered_commands, command, e.cli_error_msg) self.native.exit_config_mode()
def config_list(self, commands, format="set"): try: for command in commands: self.cu.load(command, format=format) self.cu.commit() except ConfigLoadError as e: raise CommandListError(commands, command, e.message)
def config_list(self, commands): self._enter_config() entered_commands = [] for command in commands: entered_commands.append(command) try: self._send_command(command) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) self.native.exit_config_mode()
def show_list(self, commands, raw_text=False): if raw_text: encoding = "text" else: encoding = "json" try: return strip_unicode( self._parse_response(self.native.enable(commands, encoding=encoding), raw_text=raw_text) ) except EOSCommandError as e: raise CommandListError(commands, e.commands[len(e.commands) - 1], e.message)
def show_list(self, commands): self.enable() responses = [] entered_commands = [] for command in commands: entered_commands.append(command) try: responses.append(self._send_command(command)) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) return responses
def config(self, commands): """Send configuration commands to a device. Args: commands (str, list): String with single command, or list with multiple commands. Raises: CommandError: Issue with the command provided. CommandListError: Issue with a command in the list provided. """ try: self.native.config(commands) except EOSCommandError as e: if isinstance(commands, str): raise CommandError(commands, e.message) raise CommandListError(commands, e.commands[len(e.commands) - 1], e.message)
def config_list(self, commands): """ Send list of commands to device. Args: commands (list): list of commands to be set to device. Raises: CommandListError: Message stating which command failed and the response from the device. """ self._enter_config() entered_commands = [] for command in commands: entered_commands.append(command) try: self._send_command(command) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) self.native.exit_config_mode()
def show_list(self, commands): """ Send an operational command to the device. Args: commands (list): The list of commands to send to the device. expect (bool): Whether to send a different expect string than normal prompt. expect_string (str): The expected prompt after running the command. Returns: list: The data returned from the device for all commands. Raises: CommandListError: When the returned data indicates one of the commands failed. Example: >>> device = AIREOSDevice(**connection_args) >>> command_data = device._send_command(["show sysinfo", "show boot"]) >>> print(command_data[0]) Product Version.....8.2.170.0 System Up Time......3 days 2 hrs 20 mins 30 sec ... >>> print(command_data[1]) Primary Boot Image............................... 8.2.170.0 (default) (active) Backup Boot Image................................ 8.5.110.0 >>> """ self.enable() responses = [] entered_commands = [] for command in commands: entered_commands.append(command) try: responses.append(self._send_command(command)) except CommandError as e: raise CommandListError(entered_commands, command, e.cli_error_msg) return responses
def config_list(self, commands, ignore_status=r''): """ ignore_status : Union(regex,callable) If the command matches the regex [or the function returns True], the non-zero exit status of certain commands will be ignored. """ import re, types self._enter_config() entered_commands = [] for command in commands: entered_commands.append(command) try: self._send_command(command) except CommandError as e: if (type(ignore_status) is str and re.match(ignore_status, command)) or ( type(ignore_status) == types.FunctionType and ignore_status(command)): print "Ignoring non-zero return status" print "\t" + str(e).replace("\n", "\n\t") else: raise CommandListError(entered_commands, command, e.cli_error_msg) self.native.exit_config_mode()
def config(self, command, **netmiko_args): """ Send config commands to device. By default, entering and exiting config mode is handled automatically. To disable entering and exiting config mode, pass `enter_config_mode` and `exit_config_mode` in ``**netmiko_args``. This supports all arguments supported by Netmiko's `send_config_set` method using ``netmiko_args``. This will send each command in ``command`` until either an Error is caught or all commands have been sent. Args: command (str|list): The command or commands to send to the device. **netmiko_args: Any argument supported by ``netmiko.ConnectHandler.send_config_set``. Returns: str: When ``command`` is a str, the config session input and ouput from sending ``command``. list: When ``command`` is a list, the config session input and ouput from sending ``command``. Raises: TypeError: When sending an argument in ``**netmiko_args`` that is not supported. CommandError: When ``command`` is a str and its results report an error. CommandListError: When ``command`` is a list and one of the commands reports an error. Example: >>> device = IOSDevice(**connection_args) >>> device.config("no service pad") 'configure terminal\nEnter configuration commands, one per line. End with CNTL/Z.\n' 'host(config)#no service pad\nhost(config)#end\nhost#' >>> device.config(["interface Gig0/1", "description x-connect"]) ['host(config)#interface Gig0/1\nhost(config-if)#, 'description x-connect\nhost(config-if)#'] >>> """ # TODO: Remove this when deprecating config_list method original_command_is_str = isinstance(command, str) if original_command_is_str: # TODO: switch to isinstance(command, str) when removing above command = [command] original_exit_config_setting = netmiko_args.get("exit_config_mode") netmiko_args["exit_config_mode"] = False # Ignore None or invalid args passed for enter_config_mode if netmiko_args.get("enter_config_mode") is not False: self._enter_config() netmiko_args["enter_config_mode"] = False entered_commands = [] command_responses = [] try: for cmd in command: entered_commands.append(cmd) command_response = self.native.send_config_set( cmd, **netmiko_args) command_responses.append(command_response) self._check_command_output_for_errors(cmd, command_response) except TypeError as err: raise TypeError(f"Netmiko Driver's {err.args[0]}") # TODO: Remove this when deprecating config_list method except CommandError as err: if not original_command_is_str: raise CommandListError(entered_commands, cmd, err.cli_error_msg) else: raise err # Don't let exception prevent exiting config mode finally: # Ignore None or invalid args passed for exit_config_mode if original_exit_config_setting is not False: self.native.exit_config_mode() # TODO: Remove this when deprecating config_list method if original_command_is_str: return command_responses[0] return command_responses
def config_list(self, commands): try: self.native.config(commands) except EOSCommandError as e: raise CommandListError(commands, e.commands[len(e.commands) - 1], e.message)
def show_list(self, commands, raw_text=False): try: return self.native.show_list(commands, raw_text=raw_text) except CLIError as e: raise CommandListError(commands, e.command, str(e))
def config_list(self, commands): try: self.native.config_list(commands) except CLIError as e: raise CommandListError(commands, e.command, str(e))