How to use the napalm.base.exceptions.CommandErrorException function in napalm

To help you get started, we’ve selected a few napalm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github napalm-automation / napalm / napalm / ios / ios.py View on Github external
self.device.send_config_set(["file prompt quiet"])
                    self.prompt_quiet_changed = True
                    self.prompt_quiet_configured = True
                else:
                    # check if the command is already in the running-config
                    cmd = "file prompt quiet"
                    show_cmd = "show running-config | inc {}".format(cmd)
                    output = self.device.send_command_expect(show_cmd)
                    if cmd in output:
                        self.prompt_quiet_configured = True
                    else:
                        msg = (
                            "on-device file operations require prompts to be disabled. "
                            "Configure 'file prompt quiet' or set 'auto_file_prompt=True'"
                        )
                        raise CommandErrorException(msg)

            # call wrapped function
            return f(self, *args, **kwargs)
github napalm-automation / napalm / napalm / eos / eos.py View on Github external
if type(commands) is not list:
            raise TypeError("Please enter a valid list of commands!")

        for command in commands:
            try:
                cli_output[str(command)] = self.device.run_commands(
                    [command], encoding="text"
                )[0].get("output")
                # not quite fair to not exploit rum_commands
                # but at least can have better control to point to wrong command in case of failure
            except pyeapi.eapilib.CommandError:
                # for sure this command failed
                cli_output[str(command)] = 'Invalid command: "{cmd}"'.format(
                    cmd=command
                )
                raise CommandErrorException(str(cli_output))
            except Exception as e:
                # something bad happened
                msg = 'Unable to execute command "{cmd}": {err}'.format(
                    cmd=command, err=e
                )
                cli_output[str(command)] = msg
                raise CommandErrorException(str(cli_output))

        return cli_output
github napalm-automation-community / napalm-ce / napalm_ce / ce.py View on Github external
def _save_config(self, filename=''):
        """Save the current running config to the given file."""
        command = 'save {}'.format(filename)
        save_log = self.device.send_command(command, max_loops=10, expect_string=r'Y/N')
        # Search pattern will not be detected when set a new hostname, so don't use auto_find_prompt=False
        save_log += self.device.send_command('y', expect_string=r'<.+>')
        search_result = re.search("successfully", save_log, re.M)
        if search_result is None:
            msg = "Failed to save config. Command output:{}".format(save_log)
            raise CommandErrorException(msg)
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
# Check if file already exists and has correct MD5
            if transfer.check_file_exists() and transfer.compare_md5():
                msg = "File already exists and has correct MD5: no SCP needed"
                return (True, msg)
            if not transfer.verify_space_available():
                msg = "Insufficient space available on remote device"
                return (False, msg)

            if use_scp:
                cmd = 'ip scp server enable'
                show_cmd = "show running-config | inc {}".format(cmd)
                output = self.device.send_command_expect(show_cmd)
                if cmd not in output:
                    msg = "SCP file transfers are not enabled. " \
                          "Configure 'ip scp server enable' on the device."
                    raise CommandErrorException(msg)

            # Transfer file
            transfer.transfer_file()

            # Compares MD5 between local-remote files
            if transfer.verify_file():
                msg = "File successfully transferred to remote device"
                return (True, msg)
            else:
                msg = "File transfer to remote device failed"
                return (False, msg)
            return (False, '')
github napalm-automation / napalm / napalm / nxos / nxos.py View on Github external
def _copy_run_start(self):
        results = self.device.save(filename="startup-config")
        if not results:
            msg = "Unable to save running-config to startup-config!"
            raise CommandErrorException(msg)
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
msg = "File already exists and has correct MD5: no SCP needed"
                return (True, msg)
            if not transfer.verify_space_available():
                msg = "Insufficient space available on remote device"
                return (False, msg)

            if use_scp:
                cmd = "ip scp server enable"
                show_cmd = "show running-config | inc {}".format(cmd)
                output = self.device.send_command_expect(show_cmd)
                if cmd not in output:
                    msg = (
                        "SCP file transfers are not enabled. "
                        "Configure 'ip scp server enable' on the device."
                    )
                    raise CommandErrorException(msg)

            # Transfer file
            transfer.transfer_file()

            # Compares MD5 between local-remote files
            if transfer.verify_file():
                msg = "File successfully transferred to remote device"
                return (True, msg)
            else:
                msg = "File transfer to remote device failed"
                return (False, msg)
            return (False, "")
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
def _discover_file_system(self):
        try:
            return self.device._autodetect_fs()
        except Exception:
            msg = (
                "Netmiko _autodetect_fs failed (to workaround specify "
                "dest_file_system in optional_args.)"
            )
            raise CommandErrorException(msg)
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
def get_bgp_neighbors_detail(self, neighbor_address=""):
        bgp_detail = defaultdict(lambda: defaultdict(lambda: []))

        raw_bgp_sum = self._send_command("show ip bgp all sum").strip()

        if "Invalid input detected" in raw_bgp_sum:
            raise CommandErrorException("BGP is not running on this device")

        bgp_sum = napalm.base.helpers.textfsm_extractor(
            self, "ip_bgp_all_sum", raw_bgp_sum
        )
        for neigh in bgp_sum:
            if neighbor_address and neighbor_address != neigh["neighbor"]:
                continue
            raw_bgp_neigh = self._send_command(
                "show ip bgp {} neigh {}".format(
                    AFI_COMMAND_MAP[neigh["addr_family"]], neigh["neighbor"]
                )
            )
            bgp_neigh = napalm.base.helpers.textfsm_extractor(
                self, "ip_bgp_neigh", raw_bgp_neigh
            )[0]
            details = {