def execute_cli(self): r = [] with self.profile.switch(self): lldp = self.cli("show lldp neighbors") for link in parse_table(lldp, allow_wrap=True): local_interface = link[0] remote_chassis_id = link[1] remote_port = link[2] remote_system_name = link[3] # Get capability cap = 0 for c in link[4].split(","): c = c.strip() if c: cap |= self.CAPS_MAP[c] if (is_ipv4(remote_chassis_id) or is_ipv6(remote_chassis_id)): remote_chassis_id_subtype = 5 elif is_mac(remote_chassis_id): remote_chassis_id = MACAddressParameter().clean( remote_chassis_id) remote_chassis_id_subtype = 4 else: remote_chassis_id_subtype = 7 # Get remote port subtype remote_port_subtype = 1 if is_ipv4(remote_port): # Actually networkAddress(4) remote_port_subtype = 4 elif is_mac(remote_port): # Actually macAddress(3) # Convert MAC to common form remote_port = MACAddressParameter().clean(remote_port) remote_port_subtype = 3 elif is_int(remote_port): # Actually local(7) remote_port_subtype = 7 i = {"local_interface": local_interface, "neighbors": []} n = { "remote_chassis_id": remote_chassis_id, "remote_chassis_id_subtype": remote_chassis_id_subtype, "remote_port": remote_port, "remote_port_subtype": remote_port_subtype, "remote_capabilities": cap, } if remote_system_name: n["remote_system_name"] = remote_system_name try: c = self.cli("show lldp neighbors interface %s" % local_interface) match = self.rx_detail.search(c) if match: if match.group("port_descr").strip(): port_descr = match.group("port_descr").strip() n["remote_port_description"] = port_descr except Exception: pass i["neighbors"] += [n] r += [i] return r
def test_macaddress_parameter(): assert MACAddressParameter().clean("1234.5678.9ABC") == "12:34:56:78:9A:BC" assert MACAddressParameter().clean("1234.5678.9abc") == "12:34:56:78:9A:BC" assert MACAddressParameter().clean( "0112.3456.789a.bc") == "12:34:56:78:9A:BC" with pytest.raises(InterfaceTypeError): MACAddressParameter().clean("1234.5678.9abc.def0") assert MACAddressParameter().clean( "12:34:56:78:9A:BC") == "12:34:56:78:9A:BC" assert MACAddressParameter().clean( "12-34-56-78-9A-BC") == "12:34:56:78:9A:BC" assert MACAddressParameter().clean( "0:13:46:50:87:5") == "00:13:46:50:87:05" assert MACAddressParameter().clean("123456-789abc") == "12:34:56:78:9A:BC" with pytest.raises(InterfaceTypeError): MACAddressParameter().clean("12-34-56-78-9A-BC-DE") with pytest.raises(InterfaceTypeError): MACAddressParameter().clean("AB-CD-EF-GH-HJ-KL") assert MACAddressParameter().clean("aabb-ccdd-eeff") == "AA:BB:CC:DD:EE:FF" assert MACAddressParameter().clean("aabbccddeeff") == "AA:BB:CC:DD:EE:FF" assert MACAddressParameter().clean("AABBCCDDEEFF") == "AA:BB:CC:DD:EE:FF" assert MACAddressParameter().clean( "\xa8\xf9K\x80\xb4\xc0") == "A8:F9:4B:80:B4:C0" with pytest.raises(InterfaceTypeError): MACAddressParameter( accept_bin=False).clean("\\xa8\\xf9K\\x80\\xb4\\xc0")
def execute(self): r = [] """ # Try SNMP first if self.has_snmp(): try: # lldpRemLocalPortNum # lldpRemChassisIdSubtype lldpRemChassisId # lldpRemPortIdSubtype lldpRemPortId # lldpRemSysName lldpRemSysCapEnabled for v in self.snmp.get_tables( ["1.0.8802.1.1.2.1.4.1.1.2", "1.0.8802.1.1.2.1.4.1.1.4", "1.0.8802.1.1.2.1.4.1.1.5", "1.0.8802.1.1.2.1.4.1.1.6", "1.0.8802.1.1.2.1.4.1.1.7", "1.0.8802.1.1.2.1.4.1.1.9", "1.0.8802.1.1.2.1.4.1.1.12" ], bulk=True): local_interface = self.snmp.get( "1.3.6.1.2.1.31.1.1.1.1." + v[1], cached=True) remote_chassis_id_subtype = v[2] remotechassisid = ":".join(["%02x" % ord(c) for c in v[3]]) remote_port_subtype = v[4] if remote_port_subtype == 7: remote_port_subtype = 5 remote_port = v[5] remote_system_name = v[6] remote_capabilities = v[7] i = {"local_interface": local_interface, "neighbors": []} n = { "remote_chassis_id_subtype": remote_chassis_id_subtype, "remote_chassis_id": remotechassisid, "remote_port_subtype": remote_port_subtype, "remote_port": remote_port, "remote_capabilities": remote_capabilities, } if remote_system_name: n["remote_system_name"] = remote_system_name i["neighbors"].append(n) r.append(i) return r except self.snmp.TimeOutError: pass """ # Fallback to CLI for lldp in self.cli("show lldp neighbors interface").split("\n\n"): match = self.rx_detail.search(lldp) if match: i = {"local_interface": match.group("local_if"), "neighbors": []} n = {"remote_chassis_id_subtype": match.group("rem_cid_type")} n["remote_port_subtype"] = { "Interface alias": 1, # "Port component": 2, "MAC address": 3, "Interface": 5, "Local": 7, }[match.group("p_type")] if n["remote_port_subtype"] == 3: remote_port = MACAddressParameter().clean(match.group("port_id")) elif n["remote_port_subtype"] == 7: p_id = match.group("port_id") try: remote_port = IntParameter().clean(p_id) except InterfaceTypeError: remote_port = p_id else: remote_port = match.group("port_id") n["remote_chassis_id"] = match.group("id") n["remote_port"] = str(remote_port) # Get capability cap = 0 n["remote_capabilities"] = cap i["neighbors"] += [n] r += [i] return r
def clean_mac(self): v = self.cleaned_data["mac"] if v: return MACAddressParameter().form_clean(v)
def execute(self): ifaces = {} mac_svi = "" name_ = {} mac_ = {} snmp_ifindex_ = {} descr_ = {} stat_ = {} tagged_ = {} untagged_ = {} # 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 = [] 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"] = list(six.itervalues(ifaces)) r += [rr] # Return result return r
def execute(self, interface=None): if self.has_snmp(): try: # Get interface status r = [] for i, n, s, d, m in self.snmp.join( [ mib["IF-MIB::ifDescr"], mib["IF-MIB::ifOperStatus"], mib["IF-MIB::ifAlias"], mib["IF-MIB::ifPhysAddress"], ], join="left", ): 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), } ] 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 Exception: 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"): 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") 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
def get_db_prep_value(self, value, connection, prepared=False): if not value: return None return MACAddressParameter().clean(value)
def execute_cli(self): r = [] try: v = self.cli("show lldp neighbors") except self.CLISyntaxError: raise self.NotSupportedError() if v.startswith("%"): # % LLDP is not enabled return [] v = self.rx_summary_split.split(v)[1] lldp_interfaces = [] # Get LLDP interfaces with neighbors for line in v.splitlines(): line = line.strip() if not line: break match = self.rx_s_line.match(line) if not match: continue lldp_interfaces += [match.group("local_if")] # Get LLDP neighbors for local_if in lldp_interfaces: i = {"local_interface": local_if, "neighbors": []} # Get neighbors details try: v = self.cli("show lldp neighbors %s detail" % local_if) except self.CLISyntaxError: # Found strange CLI syntax on Catalyst 4900 # Allow ONLY interface name or "detail" # Need testing... raise self.NotSupportedError() # Get remote port match = self.re_search(self.rx_remote_port, v) remote_port = match.group("remote_if") remote_port_subtype = 1 if is_ipv4(remote_port): # Actually networkAddress(4) remote_port = IPv4Parameter().clean(remote_port) remote_port_subtype = 4 elif is_mac(remote_port): # Actually macAddress(3) # Convert MAC to common form remote_port = MACAddressParameter().clean(remote_port) remote_port_subtype = 3 elif is_int(remote_port): # Actually local(7) remote_port_subtype = 7 n = { "remote_port": remote_port, "remote_port_subtype": remote_port_subtype, "remote_chassis_id_subtype": 4, } match = self.rx_descr.search(v) if match: n["remote_port_description"] = match.group("descr") # Get chassis id match = self.rx_chassis_id.search(v) if not match: continue n["remote_chassis_id"] = match.group("id") # Get capabilities cap = 0 match = self.rx_enabled_caps.search(v) if match: for c in match.group("caps").split(","): c = c.strip() if c: cap |= { "O": 1, "P": 2, "B": 4, "W": 8, "R": 16, "T": 32, "C": 64, "S": 128, }[c] n["remote_capabilities"] = cap # Get remote chassis id match = self.rx_system.search(v) if match: n["remote_system_name"] = match.group("name") if is_ipv4(n["remote_chassis_id"]) or is_ipv6( n["remote_chassis_id"]): n["remote_chassis_id_subtype"] = 5 elif is_mac(n["remote_chassis_id"]): pass else: n["remote_chassis_id_subtype"] = 7 i["neighbors"] += [n] r += [i] return r
def test_macaddress_parameter(raw, config, expected): assert MACAddressParameter(**config).clean(raw) == expected
def test_macaddress_parameter_error(raw, config): with pytest.raises(InterfaceTypeError): assert MACAddressParameter(**config).clean(raw)
def decode_mac(event, value): return MACAddressParameter().clean(value)
def execute(self): r = [] v = self.cli("show interfaces status") t = parse_table(v) for i in t: iface = {"local_interface": i[0], "neighbors": []} v = self.cli("show lldp neighbors interface %s" % i[0]) for m in self.rx_entity.finditer(v): n = {} n["remote_chassis_id_subtype"] = { "chassis component": 1, "interface alias": 2, "port component": 3, "mac address": 4, "network address": 5, "interface name": 6, "local": 7, }[m.group("chassis_id_type").strip().lower()] n["remote_chassis_id"] = m.group("chassis_id").strip() remote_port_subtype = m.group("port_id_type") remote_port_subtype.replace("_", " ") n["remote_port_subtype"] = { "interface alias": 1, "port component": 2, "mac address": 3, "network address": 4, "interface name": 5, "agent circuit id": 6, "local": 7, }[remote_port_subtype.strip().lower()] n["remote_port"] = m.group("port_id").strip() if n["remote_port_subtype"] == 3: n["remote_port"] = MACAddressParameter().clean( n["remote_port"]) if n["remote_port_subtype"] == 4: n["remote_port"] = IPv4Parameter().clean(n["remote_port"]) if m.group("port_description").strip(): n["remote_port_description"] = re.sub( r"\n\s*", "", m.group("port_description").strip()) if m.group("system_name").strip(): n["remote_system_name"] = m.group("system_name").strip() if m.group("system_description").strip(): n["remote_system_description"] = re.sub( r"\n\s*", "", m.group("system_description").strip()) caps = 0 for c in m.group("system_capabilities").split(","): c = c.strip() if not c: break caps |= { "Other": 1, "Repeater": 2, "Bridge": 4, "WLAN Access Point": 8, "Router": 16, "Telephone": 32, "DOCSIS Cable Device": 64, "Station Only": 128, }[c] n["remote_capabilities"] = caps iface["neighbors"] += [n] if iface["neighbors"]: r += [iface] return r
def execute_cli(self): r = [] try: v = self.cli("show lldp neighbors") except self.CLISyntaxError: raise self.NotSupportedError() if v.startswith("%"): # % LLDP is not enabled return [] v = self.rx_summary_split.split(v)[1] lldp_interfaces = [] # Get LLDP interfaces with neighbors for line in v.splitlines(): line = line.strip() if not line: break match = self.rx_s_line.match(line) if not match: continue lldp_interfaces += [match.group("local_if")] # Get LLDP neighbors for local_if in lldp_interfaces: i = {"local_interface": local_if, "neighbors": []} # Get neighbors details try: v = self.cli("show lldp neighbors %s detail" % local_if) except self.CLISyntaxError: # Found strange CLI syntax on Catalyst 4900 # Allow ONLY interface name or "detail" # Need testing... raise self.NotSupportedError() # Get remote port match = self.re_search(self.rx_remote_port, v) remote_port = match.group("remote_if") remote_port_subtype = LLDP_PORT_SUBTYPE_ALIAS if is_ipv4(remote_port): remote_port = IPv4Parameter().clean(remote_port) remote_port_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(remote_port): # Convert MAC to common form remote_port = MACAddressParameter().clean(remote_port) remote_port_subtype = LLDP_PORT_SUBTYPE_MAC elif is_int(remote_port): remote_port_subtype = LLDP_PORT_SUBTYPE_LOCAL n = { "remote_port": remote_port, "remote_port_subtype": remote_port_subtype, "remote_chassis_id_subtype": LLDP_CHASSIS_SUBTYPE_MAC, } match = self.rx_descr.search(v) if match: n["remote_port_description"] = match.group("descr") # Get chassis id match = self.rx_chassis_id.search(v) if not match: continue n["remote_chassis_id"] = match.group("id") # Get capabilities cap = 0 match = self.rx_enabled_caps.search(v) if match: cap = lldp_caps_to_bits( match.group("caps").strip().split(","), { "o": LLDP_CAP_OTHER, "p": LLDP_CAP_REPEATER, "b": LLDP_CAP_BRIDGE, "w": LLDP_CAP_WLAN_ACCESS_POINT, "r": LLDP_CAP_ROUTER, "t": LLDP_CAP_TELEPHONE, "c": LLDP_CAP_DOCSIS_CABLE_DEVICE, "s": LLDP_CAP_STATION_ONLY, }, ) n["remote_capabilities"] = cap # Get remote chassis id match = self.rx_system.search(v) if match: n["remote_system_name"] = match.group("name") if is_ipv4(n["remote_chassis_id"]) or is_ipv6( n["remote_chassis_id"]): n["remote_chassis_id_subtype"] = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(n["remote_chassis_id"]): pass else: n["remote_chassis_id_subtype"] = LLDP_CHASSIS_SUBTYPE_LOCAL i["neighbors"] += [n] r += [i] return r
def execute(self): r = [] # Try SNMP first # Fallback to CLI # LLDP First try: lldp = self.cli("show lldp neighbors") except self.CLISyntaxError: raise self.NotSupportedError() for match in self.rx_lldp_nei.finditer(lldp): local_interface = match.group("interface") remote_chassis_id = match.group("chassis_id") remote_port = match.group("port_id") # Build neighbor data # Get capability cap = 4 # Get remote port subtype remote_port_subtype = LLDP_PORT_SUBTYPE_NAME if is_ipv4(remote_port): # Actually networkAddress(4) remote_port_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(remote_port): # Actually macAddress(3) # Convert MAC to common form remote_port = MACAddressParameter().clean(remote_port) remote_port_subtype = LLDP_PORT_SUBTYPE_MAC elif is_int(remote_port): # Actually local(7) remote_port_subtype = LLDP_PORT_SUBTYPE_LOCAL i = {"local_interface": local_interface, "neighbors": []} n = { "remote_chassis_id": remote_chassis_id, "remote_port": remote_port, "remote_capabilities": cap, "remote_port_subtype": remote_port_subtype, } if is_ipv4(n["remote_chassis_id"]) or is_ipv6( n["remote_chassis_id"]): n["remote_chassis_id_subtype"] = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS else: n["remote_chassis_id_subtype"] = LLDP_CHASSIS_SUBTYPE_MAC try: c = self.cli("show lldp ports %s neighbors detailed" % local_interface) match = self.rx_lldp_detail.search(c) if match: port_descr = match.group("port_descr") if port_descr: n["remote_port_description"] = port_descr.replace( '"', "").strip() n["remote_port_description"] = re.sub( r"\\\n\s*", "", n["remote_port_description"]) n["remote_system_name"] = match.group( "system_name").replace('"', "").strip() n["remote_system_name"] = re.sub(r"\\\n\s*", "", n["remote_system_name"]) sys_descr = match.group("system_descr") if sys_descr: n["remote_system_description"] = sys_descr.replace( '"', "").strip() n["remote_system_description"] = re.sub( r"\\\n\s*", "", n["remote_system_description"]) n["remote_port_subtype"] = self.port_types[match.group( "port_id_subtype").strip()] n["remote_chassis_id_subtype"] = self.chassis_types[ match.group("chassis_id_subtype").strip()] except self.CLISyntaxError: pass i["neighbors"].append(n) r.append(i) # Try EDP Second try: lldp = self.cli("show edp ports all") except self.CLISyntaxError: raise self.NotSupportedError() for match in self.rx_edp_nei.finditer(lldp): local_interface = match.group("interface") remote_chassis_id = match.group("chassis_id") remote_port = match.group("port_id") remote_system_name = match.group("name") # Build neighbor data # Get capability cap = 4 # Get remote port subtype remote_port_subtype = LLDP_PORT_SUBTYPE_NAME if is_ipv4(remote_port): # Actually networkAddress(4) remote_port_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(remote_port): # Actually macAddress(3) # Convert MAC to common form remote_port = MACAddressParameter().clean(remote_port) remote_port_subtype = LLDP_PORT_SUBTYPE_MAC elif is_int(remote_port): # Actually local(7) remote_port_subtype = LLDP_PORT_SUBTYPE_LOCAL i = {"local_interface": local_interface, "neighbors": []} n = { "remote_chassis_id": remote_chassis_id, "remote_port": remote_port, "remote_capabilities": cap, "remote_port_subtype": remote_port_subtype, } if remote_system_name: n["remote_system_name"] = remote_system_name if is_ipv4(n["remote_chassis_id"]) or is_ipv6( n["remote_chassis_id"]): n["remote_chassis_id_subtype"] = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS else: n["remote_chassis_id_subtype"] = LLDP_CHASSIS_SUBTYPE_MAC i["neighbors"].append(n) r.append(i) return r
def execute(self): r = [] try: v = self.cli("show lldp neighbor-information") except self.CLISyntaxError: raise self.NotSupportedError() for match in self.rx_port.finditer(v): i = {"local_interface": match.group("port"), "neighbors": []} for m in self.rx_entity.finditer(match.group("entities")): n = {} n["remote_chassis_id_subtype"] = { "chassis component": 1, "interface alias": 2, "port component": 3, "mac address": 4, "MAC address": 4, "macaddress": 4, "network address": 5, "interface name": 6, "local": 7, }[m.group("chassis_id_type").strip().lower()] n["remote_chassis_id"] = m.group("chassis_id").strip() remote_port_subtype = m.group("port_id_type") remote_port_subtype.replace("_", " ") n["remote_port_subtype"] = { "interface alias": 1, "port component": 2, "mac address": 3, "macAddress": 3, "network address": 4, "interface name": 5, "Interface Name": 5, "Interface name": 5, "agent circuit id": 6, "locally assigned": 7, "local": 7, }[remote_port_subtype.strip().lower()] n["remote_port"] = m.group("port_id").strip() if n["remote_port_subtype"] == 3: n["remote_port"] = MACAddressParameter().clean( n["remote_port"]) if n["remote_port_subtype"] == 4: n["remote_port"] = IPv4Parameter().clean(n["remote_port"]) if m.group("port_description").strip(): n["remote_port_description"] = m.group( "port_description").strip() if m.group("system_name").strip(): n["remote_system_name"] = m.group("system_name").strip() if m.group("system_description").strip(): n["remote_system_description"] = m.group( "system_description").strip() caps = 0 for c in m.group("system_capabilities").split(","): c = c.strip() if not c: break caps |= { "Other": 1, "Repeater": 2, "Bridge": 4, "WLAN Access Point": 8, "Router": 16, "Telephone": 32, "DOCSIS Cable Device": 64, "Station Only": 128, }[c] n["remote_capabilities"] = caps i["neighbors"] += [n] if i["neighbors"]: r += [i] return r
def execute_cli(self): d = {} if self.has_snmp(): try: for s in self.snmp.getnext("1.3.6.1.2.1.2.2.1.2", max_repetitions=10): n = s[1] sifindex = s[0][len("1.3.6.1.2.1.2.2.1.2") + 1:] if int(sifindex) < 3000: sm = str( self.snmp.get("1.3.6.1.2.1.2.2.1.6.%s" % sifindex)) smac = MACAddressParameter().clean(sm) if n.startswith("oob"): continue sname = self.profile.convert_interface_name(n) else: continue d[sname] = {"sifindex": sifindex, "smac": smac} except self.snmp.TimeOutError: pass # Get portchannels portchannel_members = {} for pc in self.scripts.get_portchannel(): i = pc["interface"] t = pc["type"] == "L" for m in pc["members"]: portchannel_members[m] = (i, t) # Get LLDP interfaces lldp = [] if self.has_capability("Network | LLDP"): c = self.cli("show lldp configuration", ignore_errors=True) if self.rx_lldp_en.search(c): lldp = self.rx_lldp.findall(c) # Get GVRP interfaces gvrp = [] c = self.cli("show gvrp configuration", ignore_errors=True) if self.rx_gvrp_en.search(c): gvrp = self.rx_gvrp.findall(c) # Get STP interfaces stp = [] if self.has_capability("Network | STP"): c = self.cli("show spanning-tree", ignore_errors=True) if self.rx_stp_en.search(c): stp = self.rx_stp.findall(c) # Get ifname and description c = self.cli("show interfaces description").split("\n\n") i = self.rx_sh_int_des.findall("".join(["%s\n\n%s" % (c[0], c[1])])) if not i: i = self.rx_sh_int_des2.findall("".join( ["%s\n\n%s" % (c[0], c[1])])) interfaces = [] mtu = None for res in i: mac = None ifindex = 0 name = res[0].strip() if self.match_version(version__regex=r"[12]\.[15]\.4[4-9]" ) or self.match_version( version__regex=r"4\.0\.[4-7]$"): v = self.cli("show interface %s" % name) time.sleep(0.5) for match in self.rx_sh_int.finditer(v): # ifname = match.group("interface") ifindex = match.group("ifindex") mac = match.group("mac") mtu = match.group("mtu") if len(res) == 4: a_stat = res[1].strip().lower() == "up" o_stat = res[2].strip().lower() == "up" description = res[3].strip() else: a_stat = True o_stat = match.group("oper_status").lower() == "up" description = match.group("descr").strip() if not description: description = "" else: if self.profile.convert_interface_name(name) in d: ifindex = d[self.profile.convert_interface_name( name)]["sifindex"] mac = d[self.profile.convert_interface_name(name)]["smac"] if len(res) == 4: a_stat = res[1].strip().lower() == "up" o_stat = res[2].strip().lower() == "up" description = res[3].strip() else: o_stat = True a_stat = True description = res[1].strip() sub = { "name": self.profile.convert_interface_name(name), "admin_status": a_stat, "oper_status": o_stat, "enabled_afi": [], } if description: sub["description"] = description if mtu: sub["mtu"] = mtu if ifindex: sub["snmp_ifindex"] = ifindex if mac: sub["mac"] = mac iface = { "type": self.profile.get_interface_type(name), "name": self.profile.convert_interface_name(name), "admin_status": a_stat, "oper_status": o_stat, "enabled_protocols": [], "subinterfaces": [sub], } if description: iface["description"] = description if ifindex: iface["snmp_ifindex"] = ifindex if mac: iface["mac"] = mac # LLDP protocol if name in lldp: iface["enabled_protocols"] += ["LLDP"] # GVRP protocol if name in gvrp: iface["enabled_protocols"] += ["GVRP"] # STP protocol if name in stp: iface["enabled_protocols"] += ["STP"] # Portchannel member name = self.profile.convert_interface_name(name) if name in portchannel_members: ai, is_lacp = portchannel_members[name] iface["aggregated_interface"] = ai if is_lacp: iface["enabled_protocols"] += ["LACP"] iface["subinterfaces"][0]["enabled_afi"] += ["BRIDGE"] # Vlans cmd = self.cli("show interfaces switchport %s" % name) time.sleep(0.5) rcmd = cmd.split("\n\n") tvlan = [] utvlan = None for vlan in parse_table(rcmd[0]): vlan_id = vlan[0] rule = vlan[2] if rule == "Tagged": tvlan.append(int(vlan_id)) elif rule == "Untagged": utvlan = vlan_id iface["subinterfaces"][0]["tagged_vlans"] = tvlan if utvlan: iface["subinterfaces"][0]["untagged_vlan"] = utvlan cmd = self.cli("show ip interface %s" % name) time.sleep(0.5) for match in self.rx_sh_ip_int.finditer(cmd): if not match: continue ip = match.group("ip") netmask = match.group("mask") ip = ip + "/" + netmask ip_list = [ip] enabled_afi = [] if ":" in ip: ip_interfaces = "ipv6_addresses" enabled_afi += ["IPv6"] else: ip_interfaces = "ipv4_addresses" enabled_afi += ["IPv4"] iface["subinterfaces"][0]["enabled_afi"] = enabled_afi iface["subinterfaces"][0][ip_interfaces] = ip_list interfaces += [iface] ip_iface = self.cli("show ip interface") for match in self.rx_sh_ip_int.finditer(ip_iface): ifname = match.group("interface") typ = self.profile.get_interface_type(ifname) ip = match.group("ip") netmask = match.group("mask") ip = ip + "/" + netmask ip_list = [ip] enabled_afi = [] if ":" in ip: ip_interfaces = "ipv6_addresses" enabled_afi += ["IPv6"] else: ip_interfaces = "ipv4_addresses" enabled_afi += ["IPv4"] if ifname.startswith("vlan"): vlan = ifname.split(" ")[1] ifname = ifname.strip() else: continue if match.group("admin_status"): a_stat = match.group("admin_status").lower() == "up" else: a_stat = True if match.group("oper_status"): o_stat = match.group("oper_status").lower() == "up" else: o_stat = True iface = { "name": self.profile.convert_interface_name(ifname), "type": typ, "admin_status": a_stat, "oper_status": o_stat, "subinterfaces": [{ "name": ifname, "admin_status": a_stat, "oper_status": o_stat, "enabled_afi": enabled_afi, ip_interfaces: ip_list, "vlan_ids": self.expand_rangelist(vlan), }], } interfaces += [iface] return [{"interfaces": interfaces}]
def execute_cli(self): r = [] try: v = self.cli("show lldp remote_ports mode normal") except self.CLISyntaxError: raise self.NotSupportedError() for match in self.rx_port.finditer(v): i = { "local_interface": match.group("port"), "neighbors": [] } for m in self.rx_entity.finditer(match.group("entities")): n = {} remote_chassis_id_subtype = \ m.group("chassis_id_subtype").replace("_", " ") n["remote_chassis_id_subtype"] = { "chassis component": 1, "interface alias": 2, "port component": 3, "mac address": 4, "macaddress": 4, "network address": 5, "interface name": 6, "local": 7 }[remote_chassis_id_subtype.strip().lower()] n["remote_chassis_id"] = m.group("chassis_id").strip() remote_port_subtype = \ m.group("port_id_subtype").replace("_", " ") n["remote_port_subtype"] = { "interface alias": 1, # DES-3526 6.00 B48, DES-3526 6.00 B49, # DES-3200-28 1.85.B008 "nterface alias": 1, "port component": 2, "mac address": 3, "macaddress": 3, "network address": 4, "interface name": 5, "agent circuit id": 6, "locally assigned": 7, "local": 7 }[remote_port_subtype.strip().lower()] n["remote_port"] = m.group("port_id").strip() if n["remote_port_subtype"] == 3: try: n["remote_port"] = \ MACAddressParameter().clean(n["remote_port"]) except ValueError: continue if n["remote_port_subtype"] == 4: n["remote_port"] = \ IPv4Parameter().clean(n["remote_port"]) if m.group("port_description").strip(): p = m.group("port_description").strip() n["remote_port_description"] = re.sub("\n\s{49}", "", p) if m.group("system_name").strip(): p = m.group("system_name").strip() n["remote_system_name"] = re.sub("\n\s{49}", "", p) if m.group("system_description").strip(): p = m.group("system_description").strip() n["remote_system_description"] = re.sub("\n\s{49}", "", p) caps = 0 for c in m.group("system_capabilities").split(","): c = re.sub("\s{49,50}", "", c) c = c.strip() if not c: break caps |= { "Other": 1, "Repeater": 2, "Bridge": 4, "Access Point": 8, "WLAN Access Point": 8, "Router": 16, "Telephone": 32, "DOCSIS Cable Device": 64, "Station Only": 128 }[c] n["remote_capabilities"] = caps i["neighbors"] += [n] if i["neighbors"]: r += [i] return r
def execute_cli(self): r = [] # Fallback to CLI lldp = self.cli("show lldp neighbors") for link in self.parse_table(lldp, allow_wrap=True, expand_tabs=False, strip_rows=True): local_interface = link[0] remote_chassis_id = link[1] remote_port = link[2] remote_system_name = link[3] # Build neighbor data # Get capability cap = lldp_caps_to_bits( link[4].strip().split(","), { "O": LLDP_CAP_OTHER, "r": LLDP_CAP_REPEATER, "B": LLDP_CAP_BRIDGE, "W": LLDP_CAP_WLAN_ACCESS_POINT, "R": LLDP_CAP_ROUTER, "T": LLDP_CAP_TELEPHONE, "D": LLDP_CAP_DOCSIS_CABLE_DEVICE, "S": LLDP_CAP_STATION_ONLY, # S-VLAN "C": 256, # C-VLAN "H": 512, # Host "TP": 1024, # Two Ports MAC Relay }, ) if is_ipv4(remote_chassis_id) or is_ipv6(remote_chassis_id): remote_chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(remote_chassis_id): remote_chassis_id = MACAddressParameter().clean(remote_chassis_id) remote_chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC else: remote_chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL # Get remote port subtype remote_port_subtype = LLDP_PORT_SUBTYPE_ALIAS if is_ipv4(remote_port): # Actually networkAddress(4) remote_port_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(remote_port): # Actually macAddress(3) # Convert MAC to common form remote_port = MACAddressParameter().clean(remote_port) remote_port_subtype = LLDP_PORT_SUBTYPE_MAC elif is_int(remote_port): # Actually local(7) remote_port_subtype = LLDP_PORT_SUBTYPE_LOCAL i = {"local_interface": local_interface, "neighbors": []} n = { "remote_chassis_id": remote_chassis_id, "remote_chassis_id_subtype": remote_chassis_id_subtype, "remote_port": remote_port, "remote_port_subtype": remote_port_subtype, "remote_capabilities": cap, } if remote_system_name: n["remote_system_name"] = remote_system_name # # XXX: Dirty hack for older firmware. Switch rebooted. # if remote_chassis_id_subtype != LLDP_CHASSIS_SUBTYPE_LOCAL: i["neighbors"] = [n] r += [i] continue try: c = self.cli("show lldp neighbors %s" % local_interface) match = self.rx_detail.search(c) if match: remote_chassis_id = match.group("dev_id") if is_ipv4(remote_chassis_id) or is_ipv6(remote_chassis_id): remote_chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(remote_chassis_id): remote_chassis_id = MACAddressParameter().clean(remote_chassis_id) remote_chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC else: remote_chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL n["remote_chassis_id"] = remote_chassis_id n["remote_chassis_id_subtype"] = remote_chassis_id_subtype if match.group("sys_name").strip(): sys_name = match.group("sys_name").strip() n["remote_system_name"] = sys_name if match.group("sys_descr").strip(): sys_descr = match.group("sys_descr").strip() n["remote_system_description"] = sys_descr if match.group("port_descr").strip(): port_descr = match.group("port_descr").strip() n["remote_port_description"] = port_descr except Exception: pass i["neighbors"] += [n] r += [i] return r
def execute(self): d = {} if self.has_snmp(): try: for s in self.snmp.getnext("1.3.6.1.2.1.31.1.1.1.1"): n = s[1] sifindex = s[0][len("1.3.6.1.2.1.31.1.1.1.1") + 1:] if int(sifindex) < 1000: sm = str( self.snmp.get("1.3.6.1.2.1.2.2.1.6.%s" % sifindex)) mtu = self.snmp.get("1.3.6.1.2.1.2.2.1.4.%s" % sifindex) smac = MACAddressParameter().clean(sm) else: continue d[n] = {"sifindex": sifindex, "smac": smac, "mtu": mtu} except self.snmp.TimeOutError: pass # Get portchannels portchannel_members = {} for pc in self.scripts.get_portchannel(): i = pc["interface"] t = pc["type"] == "L" for m in pc["members"]: portchannel_members[m] = (i, t) # Get LLDP interfaces lldp = [] c = self.cli("show lldp configuration", ignore_errors=True) if self.rx_lldp_en.search(c): lldp = self.rx_lldp.findall(c) # Get GVRP interfaces gvrp = [] c = self.cli("show gvrp configuration", ignore_errors=True) if self.rx_gvrp_en.search(c): gvrp = self.rx_gvrp.findall(c) # Get STP interfaces stp = [] c = self.cli("show spanning-tree", ignore_errors=True) if self.rx_stp_en.search(c): stp = self.rx_stp.findall(c) # Get ifname and description i = [] c = self.cli("show interfaces description").split("\n\n") i = self.rx_sh_int_des.findall(c[0]) if not i: i = self.rx_sh_int_des2.findall(c[0]) interfaces = [] for res in i: mac = None ifindex = 0 name = res[0].strip() if name in d: ifindex = d[name]["sifindex"] mac = d[name]["smac"] mtu = d[name]["mtu"] if len(res) == 4: o_stat = res[1].strip().lower() == "up" a_stat = res[2].strip().lower() == "up" description = res[3].strip() else: o_stat = True a_stat = True description = res[1].strip() sub = { "name": name, "mtu": mtu, "admin_status": a_stat, "oper_status": o_stat, "description": description.strip(), "enabled_afi": [] } if ifindex: sub["snmp_ifindex"] = ifindex if mac: sub["mac"] = mac iface = { "type": self.profile.get_interface_type(name), "name": name, "admin_status": a_stat, "oper_status": o_stat, "enabled_protocols": [], "description": description.strip(), "subinterfaces": [sub] } if ifindex: iface["snmp_ifindex"] = ifindex if mac: iface["mac"] = mac # LLDP protocol if name in lldp: iface["enabled_protocols"] += ["LLDP"] # GVRP protocol if name in gvrp: iface["enabled_protocols"] += ["GVRP"] # STP protocol if name in stp: iface["enabled_protocols"] += ["STP"] # Portchannel member if name in portchannel_members: ai, is_lacp = portchannel_members[name] iface["aggregated_interface"] = ai if is_lacp: iface["enabled_protocols"] += ["LACP"] iface["subinterfaces"][0]["enabled_afi"] += ["BRIDGE"] # Vlans cmd = self.cli("show interfaces switchport ethernet %s" % name) rcmd = cmd.split("\n\n") tvlan = [] utvlan = None for vlan in parse_table(rcmd[1]): vlan_id = vlan[0] rule = vlan[2] if rule == "Tagged": tvlan.append(int(vlan_id)) elif rule == "Untagged": utvlan = vlan_id iface["subinterfaces"][0]["tagged_vlans"] = tvlan if utvlan: iface["subinterfaces"][0]["untagged_vlan"] = utvlan cmd = self.cli("show ip interface %s" % name) for match in self.rx_sh_ip_int.finditer(cmd): if not match: continue ip = match.group("ip") netmask = match.group("mask") ip = ip + '/' + netmask ip_list = [ip] enabled_afi = [] if ":" in ip: ip_interfaces = "ipv6_addresses" enabled_afi += ["IPv6"] else: ip_interfaces = "ipv4_addresses" enabled_afi += ["IPv4"] iface["subinterfaces"][0]["enabled_afi"] = enabled_afi iface["subinterfaces"][0][ip_interfaces] = ip_list interfaces += [iface] ip_iface = self.cli("show ip interface") for match in self.rx_sh_ip_int.finditer(ip_iface): ifname = match.group("interface") typ = self.profile.get_interface_type(ifname) ip = match.group("ip") netmask = match.group("mask") ip = ip + '/' + netmask ip_list = [ip] enabled_afi = [] if ":" in ip: ip_interfaces = "ipv6_addresses" enabled_afi += ["IPv6"] else: ip_interfaces = "ipv4_addresses" enabled_afi += ["IPv4"] if ifname.startswith("vlan"): vlan = ifname.split(' ')[1] ifname = ifname.strip() else: continue if match.group("admin_status"): a_stat = match.group("admin_status").lower() == "up" else: a_stat = True if match.group("oper_status"): o_stat = match.group("oper_status").lower() == "up" else: o_stat = True iface = { "name": ifname, "type": typ, "admin_status": a_stat, "oper_status": o_stat, "subinterfaces": [{ "name": ifname, "admin_status": a_stat, "oper_status": o_stat, "enabled_afi": enabled_afi, ip_interfaces: ip_list, "vlan_ids": self.expand_rangelist(vlan), }] } interfaces += [iface] return [{"interfaces": interfaces}]