How to use the napalm.base.helpers.textfsm_extractor 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 / nxos_ssh / nxos_ssh.py View on Github external
Get environment facts.

        power and fan are currently not implemented
        cpu is using 1-minute average
        """

        environment = {}
        # sys_resources contains cpu and mem output
        sys_resources = self._send_command("show system resources")
        temp_cmd = "show environment temperature"

        # cpu
        environment.setdefault("cpu", {})
        environment["cpu"]["0"] = {}
        environment["cpu"]["0"]["%usage"] = -1.0
        system_resources_cpu = helpers.textfsm_extractor(
            self, "system_resources", sys_resources
        )
        for cpu in system_resources_cpu:
            cpu_dict = {
                cpu.get("cpu_id"): {
                    "%usage": round(100 - float(cpu.get("cpu_idle")), 2)
                }
            }
            environment["cpu"].update(cpu_dict)

        # memory
        environment.setdefault("memory", {})
        for line in sys_resources.splitlines():
            # Memory usage:   16401224K total,   4798280K used,   11602944K free
            if "Memory usage:" in line:
                proc_total_mem, proc_used_mem, _ = line.split(",")
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
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 = {
                "up": neigh["up"] != "never",
                "local_as": napalm.base.helpers.as_number(neigh["local_as"]),
                "remote_as": napalm.base.helpers.as_number(neigh["remote_as"]),
                "router_id": napalm.base.helpers.ip(bgp_neigh["router_id"])
                if bgp_neigh["router_id"]
                else "",
                "local_address": napalm.base.helpers.ip(bgp_neigh["local_address"])
                if bgp_neigh["local_address"]
                else "",
                "local_address_configured": False,
                "local_port": napalm.base.helpers.as_number(bgp_neigh["local_port"])
                if bgp_neigh["local_port"]
                else 0,
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
self, "show_lldp_neighbors_detail", lldp_entries
        )

        if len(lldp_entries) == 0:
            return {}

        # Older IOS versions don't have 'Local Intf' defined in LLDP detail.
        # We need to get them from the non-detailed command
        # which is in the same sequence as the detailed output
        if not lldp_entries[0]["local_interface"]:
            if interface:
                command = "show lldp neighbors {}".format(interface)
            else:
                command = "show lldp neighbors"
            lldp_brief = self._send_command(command)
            lldp_interfaces = textfsm_extractor(self, "show_lldp_neighbors", lldp_brief)
            lldp_interfaces = [x["local_interface"] for x in lldp_interfaces]
            if len(lldp_interfaces) != len(lldp_entries):
                raise ValueError(
                    "LLDP neighbor count has changed between commands. "
                    "Interface: {}\nEntries: {}".format(lldp_interfaces, lldp_entries)
                )

        for idx, lldp_entry in enumerate(lldp_entries):
            local_intf = lldp_entry.pop("local_interface") or lldp_interfaces[idx]
            # Convert any 'not advertised' to an empty string
            for field in lldp_entry:
                if "not advertised" in lldp_entry[field]:
                    lldp_entry[field] = ""
            # Add field missing on IOS
            lldp_entry["parent_interface"] = ""
            # Translate the capability fields
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 = {
                "up": neigh["up"] != "never",
                "local_as": napalm.base.helpers.as_number(neigh["local_as"]),
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
self, "show_lldp_neighbors_detail", lldp_entries
        )

        if len(lldp_entries) == 0:
            return {}

        # Older IOS versions don't have 'Local Intf' defined in LLDP detail.
        # We need to get them from the non-detailed command
        # which is in the same sequence as the detailed output
        if not lldp_entries[0]["local_interface"]:
            if interface:
                command = "show lldp neighbors {}".format(interface)
            else:
                command = "show lldp neighbors"
            lldp_brief = self._send_command(command)
            lldp_interfaces = textfsm_extractor(self, "show_lldp_neighbors", lldp_brief)
            lldp_interfaces = [x["local_interface"] for x in lldp_interfaces]
            if len(lldp_interfaces) != len(lldp_entries):
                raise ValueError(
                    "LLDP neighbor count has changed between commands. "
                    "Interface: {}\nEntries: {}".format(lldp_interfaces, lldp_entries)
                )

        for idx, lldp_entry in enumerate(lldp_entries):
            local_intf = lldp_entry.pop("local_interface") or lldp_interfaces[idx]
            # Convert any 'not advertised' to an empty string
            for field in lldp_entry:
                if "not advertised" in lldp_entry[field]:
                    lldp_entry[field] = ""
            # Add field missing on IOS
            lldp_entry["parent_interface"] = ""
            # Translate the capability fields
github napalm-automation / napalm / napalm / eos / eos.py View on Github external
def _show_vrf(self):
        commands = ["show vrf"]

        # This command has no JSON yet
        raw_output = self.device.run_commands(commands, encoding="text")[0].get(
            "output", ""
        )

        output = napalm.base.helpers.textfsm_extractor(self, "vrf", raw_output)

        return output
github napalm-automation / napalm / napalm / ios / ios.py View on Github external
if bgp_neigh["holdtime"]
                else 0,
                "configured_holdtime": 0,
                "keepalive": napalm.base.helpers.as_number(bgp_neigh["keepalive"])
                if bgp_neigh["keepalive"]
                else 0,
                "configured_keepalive": 0,
                "active_prefix_count": 0,
                "received_prefix_count": 0,
                "accepted_prefix_count": 0,
                "suppressed_prefix_count": 0,
                "advertised_prefix_count": 0,
                "flap_count": 0,
            }

            bgp_neigh_afi = napalm.base.helpers.textfsm_extractor(
                self, "ip_bgp_neigh_afi", raw_bgp_neigh
            )
            if len(bgp_neigh_afi) > 1:
                bgp_neigh_afi = bgp_neigh_afi[1]
                details.update(
                    {
                        "local_address_configured": bgp_neigh_afi["local_addr_conf"]
                        != "",
                        "multipath": bgp_neigh_afi["multipaths"] != "0",
                        "import_policy": bgp_neigh_afi["policy_in"],
                        "export_policy": bgp_neigh_afi["policy_out"],
                        "last_event": (
                            bgp_neigh_afi["last_event"]
                            if bgp_neigh_afi["last_event"] != "never"
                            else ""
                        ),
github napalm-automation / napalm / napalm / nxos / nxos.py View on Github external
def get_lldp_neighbors_detail(self, interface=""):
        lldp = {}
        lldp_interfaces = []

        if interface:
            command = "show lldp neighbors interface {} detail".format(interface)
        else:
            command = "show lldp neighbors detail"
        lldp_entries = self._send_command(command, raw_text=True)
        lldp_entries = str(lldp_entries)
        lldp_entries = napalm.base.helpers.textfsm_extractor(
            self, "show_lldp_neighbors_detail", lldp_entries
        )

        if len(lldp_entries) == 0:
            return {}

        for idx, lldp_entry in enumerate(lldp_entries):
            local_intf = lldp_entry.pop("local_interface") or lldp_interfaces[idx]
            # Convert any 'not advertised' to an empty string
            for field in lldp_entry:
                if "not advertised" in lldp_entry[field]:
                    lldp_entry[field] = ""
            # Add field missing on IOS
            lldp_entry["parent_interface"] = ""
            # Translate the capability fields
            lldp_entry[
github napalm-automation / napalm / napalm / nxos / nxos.py View on Github external
def get_snmp_information(self):
        snmp_information = {}
        snmp_command = "show running-config"
        snmp_raw_output = self.cli([snmp_command]).get(snmp_command, "")
        snmp_config = napalm.base.helpers.textfsm_extractor(
            self, "snmp_config", snmp_raw_output
        )

        if not snmp_config:
            return snmp_information

        snmp_information = {
            "contact": str(""),
            "location": str(""),
            "community": {},
            "chassis_id": str(""),
        }

        for snmp_entry in snmp_config:
            contact = str(snmp_entry.get("contact", ""))
            if contact:
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()
        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 = {
                "up": neigh["up"] != "never",
                "local_as": napalm.base.helpers.as_number(neigh["local_as"]),