Example #1
0
    def execute(self):
        if self.snmp and self.access_profile.snmp_ro:
            try:
                # Get interface physAddress
                # IF-MIB::ifPhysAddress
                for i, m in self.join_four_tables(self.snmp,
                                                  "1.3.6.1.2.1.2.2.1.6",
                                                  bulk=True):
                    if i == 1:
                        first_mac = MACAddressParameter().clean(m)
                    last_mac = MACAddressParameter().clean(m)

                return {
                    "first_chassis_mac": first_mac,
                    "last_chassis_mac": last_mac
                }

            except self.snmp.TimeOutError:
                pass

        # Fallback to CLI
        v = self.cli("show system")
        match = self.re_search(self.rx_mac, v)
        mac = MACAddressParameter().clean(match.group("id"))
        return {"first_chassis_mac": mac, "last_chassis_mac": mac}
Example #2
0
 def execute_35(self):
     ifs = []
     r = []
     # EdgeCore ES3526 advertises MAC address(3) port sub-type, so local_interface_id parameter required
     # Collect data
     local_port_ids = {}  # name -> id
     for port, local_id in self.rx_localport.findall(
             self.cli("show lldp info local-device")):
         local_port_ids["Eth " +
                        port] = MACAddressParameter().clean(local_id)
     v = self.cli("show lldp info remote-device")
     for match in self.rx_neigh.finditer(v):
         ifs += [{
             "local_interface": match.group("local_if"),
             "neighbors": [],
         }]
     for i in ifs:
         if i["local_interface"] in local_port_ids:
             i["local_interface_id"] = local_port_ids[i["local_interface"]]
         v = self.cli("show lldp info remote detail %s" %
                      i["local_interface"])
         match = self.re_search(self.rx_detail, v)
         n = {"remote_chassis_id_subtype": 4}
         if match:
             n["remote_port_subtype"] = {
                 "MAC Address": 3,
                 "Interface name": 5,
                 "Inerface alias": 5,
                 "Local": 7
             }[match.group("p_type")]
             if n["remote_port_subtype"] == 3:
                 remote_port = MACAddressParameter().clean(
                     match.group("p_id"))
             elif n["remote_port_subtype"] == 5:
                 remote_port = match.group("p_id").strip()
             else:
                 # Removing bug
                 remote_port = binascii.unhexlify(''.join(
                     match.group("p_id").split('-')))
                 remote_port = remote_port.rstrip('\x00')
             n["remote_chassis_id"] = match.group("id")
             n["remote_system_name"] = match.group("name")
             n["remote_port"] = remote_port
             # Get capability
             cap = 0
             for c in match.group("capability").strip().split(", "):
                 cap |= {
                     "Other": 1,
                     "Repeater": 2,
                     "Bridge": 4,
                     "WLAN": 8,
                     "Router": 16,
                     "Telephone": 32,
                     "Cable": 64,
                     "Station": 128
                 }[c]
             n["remote_capabilities"] = cap
         i["neighbors"] += [n]
         r += [i]
     return r
Example #3
0
def parse_neighbor(text):
    rx_ngh_line = re.compile(r"\s+Neighbor[^\n]+\n\s+Update[^\n]+\n(?P<neighbor>.*\n\n)", re.MULTILINE | re.DOTALL | re.IGNORECASE)
    rx_neigh = re.compile(r"\s+Chassis\s*ID\s*:\s*(?P<id>\S+).*?Port\s*ID\s*(sub)*type\s*:\s*(?P<p_type>[\w\s]+)\n\s+Port\s*ID\s*:\s*(?P<p_id>.+?)\n.+?Sys.*?name\s*:\s*(?P<name>[^\n]+)\n.*?Sys.*?cap.*?enabled\s*:\s*(?P<capability>[^\n]+)", re.MULTILINE | re.IGNORECASE | re.DOTALL)
    n = []
    for match_n in rx_ngh_line.finditer(text):
        for match_data in rx_neigh.finditer(match_n.group("neighbor")):
            n = {"remote_chassis_id_subtype": 4}
            if match_data:
                n["remote_port_subtype"] = {
                    "macAddress": 3,
                    "Interface name": 5,
                    "Locally assigned": 7
                }[match_data.group("p_type")]
                if n["remote_port_subtype"] == 3:
                    n["remote_port"] = MACAddressParameter().clean(match_data.group("p_id"))
                else:
                    n["remote_port"] = match_data.group("p_id")
                n["remote_chassis_id"] = match_data.group("id")
                n["remote_system_name"] = match_data.group("name")
                # Get capability
                cap = 0
                for c in match_data.group("capability").strip().split(","):
                    cap |= {
                        "NA": 0, "Other": 1, "Repeater": 2, "Bridge": 4,
                        "WLAN": 8, "Router": 16, "Telephone": 32,
                        "Cable": 64, "StationOnly": 128
                    }[c]
                n["remote_capabilities"] = cap
    return n
Example #4
0
 def execute_ex(self):
     r = []
     # Collect data
     local_port_ids = {}  # name -> id
     v = self.cli("show lldp local-information")
     for port, local_id in self.rx_localport.findall(v):
         local_port_ids[port] = IntParameter().clean(local_id)
     v = self.cli("show lldp neighbors")
     ifs = [{
         "local_interface": match.group("local_if"),
         "neighbors": [],
     } for match in self.rx_neigh.finditer(v)]
     for i in ifs:
         if i["local_interface"] in local_port_ids:
             i["local_interface_id"] = local_port_ids[i["local_interface"]]
         v = self.cli("show lldp neighbors interface %s" %
                      i["local_interface"])
         match = self.match_lines(self.rx_detail, v)
         n = {"remote_chassis_id_subtype": 4}
         if match:
             n["remote_port_subtype"] = {
                 "Mac address": 3,
                 "Interface alias": 1,
                 "Interface name": 5,
                 "Locally assigned": 7
             }[match.get("p_type")]
             if n["remote_port_subtype"] == 3:
                 remote_port = MACAddressParameter().clean(
                     match.get("p_id"))
             elif n["remote_port_subtype"] == 7:
                 p_id = match.get("p_id")
                 try:
                     remote_port = IntParameter().clean(p_id)
                 except InterfaceTypeError:
                     remote_port = p_id
             else:
                 remote_port = match.get("p_id")
             n["remote_chassis_id"] = match.get("id")
             n["remote_system_name"] = match.get("name")
             n["remote_port"] = remote_port
             # Get capability
             cap = 0
             if match.get("capability"):
                 for c in match.get("capability").strip().split(" "):
                     cap |= {
                         "Other": 1,
                         "Repeater": 2,
                         "Bridge": 4,
                         "WLAN": 8,
                         "Router": 16,
                         "Telephone": 32,
                         "Cable": 64,
                         "Station": 128
                     }[c]
             n["remote_capabilities"] = cap
         i["neighbors"] += [n]
         r += [i]
     return r
Example #5
0
 def execute(self, interface=None, vlan=None, mac=None):
     
     cmd = "sh port mac-learning"
     if interface is not None:
         cmd += " %s" % int(interface.replace("Port", ""))
         macs = self.cli(cmd)
         r = []
         for l in macs.splitlines():
             match = self.rx_macs.match(l)
             if match:
                 mac = MACAddressParameter().clean(match.group("mac"))
                 type = match.group("type")
                 vlan = "1"
                 intf = [interface]
                 r += [{
                     "vlan_id": vlan,
                     "mac": mac,
                     "interfaces": intf,
                     "type": self.types[type.lower()]
                 }]
         return r
     else:
         r = []
         for i in self.scripts.get_interface_status():
             if i["status"] == False:
                continue
             else:
                 cmd = "sh port mac-learning"
                 port = i["interface"]
                 cmd += " %s" % int(port.replace("Port", ""))
                 macs = self.cli(cmd)
                 for l in macs.splitlines():
                     match = self.rx_macs.match(l)
                     if match:
                         mac = MACAddressParameter().clean(match.group("mac"))
                         type = match.group("type")
                         vlan = "1"
                         intf = [port]
                         r += [{
                             "vlan_id": vlan,
                             "mac": mac,
                             "interfaces": intf,
                             "type": self.types[type.lower()]
                         }]
         return r
Example #6
0
    def execute(self, interface=None):
        if self.snmp and self.access_profile.snmp_ro:
            try:
                # Get interface status
                r = []
                # IF-MIB::ifName, IF-MIB::ifOperStatus, IF-MIB::ifAdminStatus, IF-MIB::ifPhysAddress
                for i, n, s, d, m in self.join_four_tables(
                        self.snmp,
                        "1.3.6.1.2.1.2.2.1.2",
                        "1.3.6.1.2.1.2.2.1.8",
                        "1.3.6.1.2.1.2.2.1.7",
                        "1.3.6.1.2.1.2.2.1.6",
                        bulk=True):
                    match = self.rx_snmp_name_eth.search(n)
                    if match:
                        n = "Port " + match.group("port")
                    if n.startswith("CpuPort"):
                        continue
                    r += [{
                        "snmp_ifindex": i,
                        "interface": n,
                        "status": int(s) == 1,
                        "mac": MACAddressParameter().clean(m)
                    }]  # ifOperStatus up(1)
                return r
            except self.snmp.TimeOutError:
                pass
            # Fallback to CLI
        r = []
        s = []

        cmd = "sh port state"
        buf = self.cli(cmd).lstrip("\n\n")
        for l in buf.split("\n"):
            match = self.rx_interface.search(l)
            if match:
                descr = ""
                interface = match.group("interface")
                linestatus = match.group("status")
                r += [{
                    "interface": interface,
                    "status": linestatus.lower() == "up",
                }]
        return r
Example #7
0
 def execute(self, interface=None):
     if self.snmp and self.access_profile.snmp_ro:
         try:
             # Get interface status
             r = []
             # IF-MIB::ifName, IF-MIB::ifOperStatus
             for i, a, n, s, m in self.join_four_tables(
                     self.snmp,
                     "1.3.6.1.2.1.2.2.1.7",
                     "1.3.6.1.2.1.31.1.1.1.1",
                     "1.3.6.1.2.1.2.2.1.8",
                     "1.3.6.1.2.1.2.2.1.6",
                     bulk=True):
                 # ifOperStatus up(1)
                 mac = MACAddressParameter().clean(m) if m else None
                 r += [{
                     "snmp_ifindex": i,
                     "interface": n,
                     "status": int(s) == 1,
                     "mac": mac
                 }]
             return r
         except self.snmp.TimeOutError:
             pass
Example #8
0
    def execute(self, interface=None):
        if self.snmp and self.access_profile.snmp_ro:
            try:
                # Get interface status
                r = []
                # IF-MIB::ifName, IF-MIB::ifOperStatus, IF-MIB::ifAlias, IF-MIB::ifPhysAddress
                for i, n, s, d, m in self.join_four_tables(
                        self.snmp,
                        "1.3.6.1.2.1.2.2.1.2",
                        "1.3.6.1.2.1.2.2.1.8",
                        "1.3.6.1.2.1.31.1.1.1.18",
                        "1.3.6.1.2.1.2.2.1.6",
                        bulk=True):
                    match = self.rx_snmp_name_eth.search(n)
                    if (i >= 1000000):
                        continue
                    if match:
                        n = match.group("port")
                        #print " !!! PORT --   %s " % n
                    macaddr = ""
                    if m:
                        macaddr = MACAddressParameter().clean(m)
                    r += [{
                        "snmp_ifindex": i,
                        "interface": n,
                        "status": int(s) == 1,
                        "description": d,
                        "mac": macaddr
                    }]  # ifOperStatus up(1)
                return r
            except self.snmp.TimeOutError:
                pass
            # Fallback to CLI

        r = []
        return r
Example #9
0
 def clean_mac(self):
     return MACAddressParameter(required=False).form_clean(self.cleaned_data["mac"])
Example #10
0
    def execute(self, interface=None):
        if self.snmp and self.access_profile.snmp_ro:
            try:
                # Get interface status
                r = []
                # IF-MIB::ifName, IF-MIB::ifOperStatus, IF-MIB::ifAlias, IF-MIB::ifPhysAddress
                for i, n, s, d, m in self.join_four_tables(
                        self.snmp,
                        "1.3.6.1.2.1.2.2.1.2",
                        "1.3.6.1.2.1.2.2.1.8",
                        "1.3.6.1.2.1.31.1.1.1.18",
                        "1.3.6.1.2.1.2.2.1.6",
                        bulk=True):
                    match = self.rx_snmp_name_eth.search(n)
                    if match:
                        if match.group("unit") == "0":
                            unit = "1"
                            n = "Eth " + unit + "/" + match.group("port")
                        else:
                            n = "Eth " + match.group(
                                "unit") + "/" + match.group("port")
                    if n.startswith("Trunk ID"):
                        n = "Trunk " + n.replace("Trunk ID ", "").lstrip('0')
                    if n.startswith("Trunk Port ID"):
                        n = "Trunk " + n.replace("Trunk Port ID ",
                                                 "").lstrip('0')
                    if n.startswith("Trunk Member"):
                        n = "Eth 1/" + str(i)
                    if n.startswith("VLAN ID"):
                        n = "VLAN " + n.replace("VLAN ID ", "").lstrip('0')
                    if n.startswith("VLAN interface"):
                        n = "VLAN " + n.replace("VLAN interface ID ",
                                                "").lstrip('0')
                    if n.startswith("Console"):
                        continue
                    if n.startswith("Loopback"):
                        continue
                    r += [{
                        "snmp_ifindex": i,
                        "interface": n,
                        "status": int(s) == 1,
                        "description": d,
                        "mac": MACAddressParameter().clean(m)
                    }]  # ifOperStatus up(1)
                return r
            except self.snmp.TimeOutError:
                pass
            # Fallback to CLI
        r = []
        s = []
        if self.match_version(platform__contains="4626"):
            try:
                cmd = "show interface status | include line protocol is|alias|address is"
                buf = self.cli(cmd).replace("\n ", " ")
            except:
                cmd = "show interface status"
                buf = self.cli(cmd).replace("\n ", " ")
            for l in buf.splitlines():
                match = self.rx_interface_status.match(l)
                if match:
                    r += [{
                        "interface":
                        match.group("interface"),
                        "status":
                        match.group("status") == "up",
                        "mac":
                        MACAddressParameter().clean(match.group("mac")),
                        "snmp_ifindex":
                        match.group("ifindex")
                    }]
                    mdescr = self.rx_interface_descr.match(l)
                    if mdescr:
                        r[-1]["description"] = mdescr.group("descr")
        else:
            cmd = "show interface status"
            buf = self.cli(cmd).lstrip("\n\n")
            for l in buf.split("\n\n"):
                match = self.rx_interface_status_3526.search(l + "\n")
                if match:
                    descr = ""
                    interface = match.group("interface")
                    if interface.startswith("VLAN"):
                        intstatus = "up"
                        linestatus = "up"
                    else:
                        if match.group("block"):
                            block = match.group("block")
                            submatch = self.rx_interface_intstatus_3526.search(
                                block)
                            if submatch:
                                descr = submatch.group("descr")
                                intstatus = submatch.group("intstatus").lower()
                            linestatus = "down"
                            submatch = self.rx_interface_linestatus_3526.search(
                                block)
                            if submatch:
                                linestatus = submatch.group(
                                    "linestatus").lower()
                    r += [{
                        "interface":
                        interface,
                        "mac":
                        MACAddressParameter().clean(match.group("mac")),
                        "status":
                        linestatus.lower() == "up",
                    }]
                    if descr:
                        r[-1]["description"] = descr

        return r
Example #11
0
    def execute(self):
        ifaces = {}
        current = None
        is_bundle = False
        is_svi = False
        vlan_ids = []
        mac_svi = ""
        name_ = {}
        mac_ = {}
        snmp_ifindex_ = {}
        descr_ = {}
        stat_ = {}
        tagged_ = {}
        untagged_ = {}
        end_if = False

        # Get interface status
        for p in self.scripts.get_interface_status():
            intf = p["interface"]
            name_[intf] = intf
            if "mac" in p:
                mac_[intf] = p["mac"]
            if "description" in p:
                descr_[intf] = p["description"]
            stat_[intf] = p["status"]
            if "snmp_ifindex" in p:
                snmp_ifindex_[intf] = p["snmp_ifindex"]

        # Get switchport's
        for p in self.scripts.get_switchport():
            intf = p["interface"]
            if "tagged" in p:
                tagged_[intf] = p["tagged"]
            if "untagged" in p:
                untagged_[intf] = p["untagged"]

        # Get SVI interface
        ip_addr = []
        sub = {}
        for ls in self.cli("sh system").splitlines():
            match = self.rx_svi_name.search(ls)
            if match:
                namesviif = "Vlan " + match.group("vl_id").upper()
            match = self.rx_ip_if.search(ls)
            if match:
                ip = match.group("ip")
            match = self.rx_ip_mask.search(ls)
            if match:
                mask = match.group("mask")
                ip_addr += [IPv4(ip, netmask=mask).prefix]
            match = self.rx_ip_mac.search(ls)
            if match:
                mac_svi = MACAddressParameter().clean(match.group("mac"))
        type = "SVI"
        stat = "up"
        vlan_ids = [int(namesviif[5:])]
        enabled_afi = ["IPv4"]
        sub = {
            "name": namesviif,
            "admin_status": stat == "up",
            "oper_status": stat == "up",
            "is_ipv4": True,
            "enabled_afi": enabled_afi,
            "ipv4_addresses": ip_addr,
            "vlan_ids": vlan_ids,
            "mac": mac_svi,
        }
        ifaces[namesviif] = {
            "name": namesviif,
            "admin_status": stat == "up",
            "oper_status": stat == "up",
            "type": type,
            "mac": mac_svi,
            "subinterfaces": [sub],
        }

        # set name ifaces
        for current in name_:
            ifaces[current] = {"name": current}
        # other
        for current in ifaces:
            is_svi = current.startswith("Vlan")
            if is_svi:
                continue
            if current in mac_:
                ifaces[current]["mac"] = mac_[current]
            ifaces[current]["admin_status"] = stat_[current]
            ifaces[current]["oper_status"] = stat_[current]
            ifaces[current]["type"] = "physical"
            ifaces[current]["enabled_protocols"] = []
            enabled_afi = ["BRIDGE"]
            sub = {
                "name": current,
                "admin_status": stat_[current],
                "oper_status": stat_[current],
                "is_bridge": True,
                "enabled_afi": enabled_afi,
            }
            if current in mac_:
                sub["mac"] = mac_[current]
            if current in tagged_:
                sub["tagged_vlans"] = tagged_[current]
            if current in untagged_:
                sub["untagged_vlan"] = untagged_[current]
            if current in snmp_ifindex_:
                sub["snmp_ifindex"] = snmp_ifindex_[current]
            ifaces[current]["subinterfaces"] = [sub]

        # Get VRFs and "default" VRF interfaces
        r = []
        seen = set()
        vpns = [{"name": "default", "type": "ip", "interfaces": []}]
        for fi in vpns:
            # Forwarding instance
            rr = {
                "forwarding_instance": fi["name"],
                "type": fi["type"],
                "interfaces": []
            }
            rd = fi.get("rd")
            if rd:
                rr["rd"] = rd
            # create ifaces

            rr["interfaces"] = ifaces.values()
        r += [rr]
        # Return result
        return r
Example #12
0
    def execute(self):
        ifaces = {}
        #Get interfaces from mibs
        try:
            c = self.cli("sh snmp MIB MIB-II interfaces")
        except self.CLISyntaxError:
            return {}
        r = {}
        for l in c.split("\n\n"):
            ip_addr = []
            if l.startswith("ifName"):
                continue
            match = self.rx_int.search(l.strip())
            if match:
                match_s = self.rx_stat.search(l.strip())
                if match_s:
                    name = self.profile.convert_interface_name(
                        match.group("ifname"))
                    i = int(match.group("ifindex"))
                    match_m = self.rx_mac.search(l.strip())
                    enabled_afi = ["BRIDGE"]
                    if match_m:
                        mac = MACAddressParameter().clean(
                            match_m.group("mac").replace(" ", ":"))
                        ip_addr = self.get_ipaddr(name)
                        if ip_addr:
                            enabled_afi = ["IPv4"]
                    else:
                        mac = None

                    #Create sub
                    sub = {
                        "name": name,
                        "enabled_protocols": [],
                        "ifindex": i,
                        "admin_status": match_s.group("a_stat") == "1",
                        "oper_status": match_s.group("o_stat") == "1",
                        "enabled_afi": enabled_afi,
                    }
                    if ip_addr:
                        sub["ipv4_addresses"] = ip_addr
                    if mac:
                        sub["mac"] = mac

                    #Create iface
                    ifaces[name] = {
                        "name": name,
                        "enabled_protocols": [],
                        "admin_status": match_s.group("a_stat") == "1",
                        "oper_status": match_s.group("o_stat") == "1",
                        "type": "physical",
                        "ifindex": i,
                        "subinterfaces": [sub],
                    }
                    if mac:
                        ifaces[name]["mac"] = mac
        # Create afi
        afi_m = {
            "forwarding_instance": "Managment",
            "type": "ip",
            "interfaces": []
        }
        afi_b = {
            "forwarding_instance": "DPI",
            "type": "bridge",
            "interfaces": []
        }

        for i in ifaces:
            if ifaces[i].get("mac"):
                afi_m["interfaces"] += [ifaces[i]]
            else:
                afi_b["interfaces"] += [ifaces[i]]

        return [afi_m, afi_b]
Example #13
0
 def save(self, *args, **kwargs):
     self.name = self.managed_object.profile.convert_interface_name(self.name)
     if self.mac:
         self.mac = MACAddressParameter().clean(self.mac)
     super(Interface, self).save(*args, **kwargs)