def get_port_info(self, port): try: v = self.cli("show port %s ethernet lldp remote-info" % port) except self.CLISyntaxError: raise self.NotSupportedError() else: match_obj = self.rx_remote_info.search(v) pri = match_obj.groupdict() pri["remote_capabilities"] = lldp_caps_to_bits( pri["remote_capabilities"].split(), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wlanaccesspoint": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "cvlan": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station": LLDP_CAP_STATION_ONLY, }, ) pri["remote_port"] = self.fixport(pri["remote_port"], pri["remote_port_subtype"]) if "n/a" in pri["remote_system_name"]: del pri["remote_system_name"] return pri
def execute_cli(self): result = [] chassis = {} v = self.cli("show lldp remote") for match in self.rx_lldp.finditer(v): chassis[match.group("port")] = match.group("chassis_id") v = self.cli("show lldp remote detail") for match in self.rx_int.finditer(v): neighbor = { "remote_chassis_id_subtype": { "macAddress": LLDP_CHASSIS_SUBTYPE_MAC, "networkAddress": LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS, "ifName": LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME, }[match.group("chassis_subtype")], # "remote_chassis_id": match.group("chassis_id"), "remote_port_subtype": { "ifAlias": LLDP_PORT_SUBTYPE_ALIAS, "macAddress": LLDP_PORT_SUBTYPE_MAC, "ifName": LLDP_PORT_SUBTYPE_NAME, "portComponent": LLDP_PORT_SUBTYPE_COMPONENT, "local": LLDP_PORT_SUBTYPE_LOCAL, }[match.group("port_subtype")], "remote_port": match.group("port_id"), } if match.group("chassis_id"): neighbor["remote_chassis_id"] = match.group("chassis_id") else: neighbor["remote_chassis_id"] = chassis[match.group( "interface")] if match.group("port_descr").strip(): p = match.group("port_descr").strip() if p != "N/A": neighbor["remote_port_description"] = re.sub( r"\n\s{30}", "", p) if match.group("sys_name").strip(): p = match.group("sys_name").strip() if p != "N/A": neighbor["remote_system_name"] = re.sub(r"\n\s{30}", "", p) if match.group("sys_descr").strip(): p = match.group("sys_descr").strip() if p != "N/A": neighbor["remote_system_description"] = re.sub( r"\n\s{30}", "", p) caps = lldp_caps_to_bits( match.group("caps").strip().split(","), { "Other": LLDP_CAP_OTHER, "Repeater/Hub": LLDP_CAP_REPEATER, "Bridge/Switch": LLDP_CAP_BRIDGE, "Router": LLDP_CAP_ROUTER, "Telephone": LLDP_CAP_TELEPHONE, "Station": LLDP_CAP_STATION_ONLY, }, ) neighbor["remote_capabilities"] = caps result += [{ "local_interface": match.group("interface"), "neighbors": [neighbor] }] return result
def execute_cli(self): r = [] t = parse_table(self.cli("show lldp neighbor"), allow_wrap=True) for i in t: c = self.cli("show lldp neighbor %s" % i[0]) match = self.rx_neighbor.search(c) chassis_id = match.group("chassis_id") if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC else: chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL port_id = match.group("port_id") if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_MAC else: port_id_subtype = LLDP_PORT_SUBTYPE_LOCAL neighbor = { "remote_chassis_id": chassis_id, "remote_chassis_id_subtype": chassis_id_subtype, "remote_port": port_id, "remote_port_subtype": port_id_subtype, } if match.group("port_descr"): port_descr = match.group("port_descr").strip() if port_descr: neighbor["remote_port_description"] = port_descr if match.group("system_name"): system_name = match.group("system_name").strip() if system_name: neighbor["remote_system_name"] = system_name if match.group("system_descr"): system_descr = match.group("system_descr").strip() if system_descr: neighbor["remote_system_description"] = system_descr caps = 0 match = self.rx_caps.search(c) if match: caps = lldp_caps_to_bits( match.group("caps").strip().split(","), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "access point": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "cable device": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station only": LLDP_CAP_STATION_ONLY, }, ) neighbor["remote_capabilities"] = caps r += [{"local_interface": i[0], "neighbors": [neighbor]}] return r
def execute_cli(self): r = [] try: v = self.cli("show lldp neighbors") # This is strange behavior, but it helps us v = self.blank_line.sub("", v) except self.CLISyntaxError: raise self.NotSupportedError() for i in parse_table( v, allow_wrap=True, line_wrapper=None, row_wrapper=lambda x: x.strip(), footer="\r\n\r\n", ): chassis_id = i[1] if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS else: try: MACAddressParameter().clean(chassis_id) chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC except ValueError: chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL port_id = i[2] if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS else: try: MACAddressParameter().clean(port_id) port_id_subtype = LLDP_PORT_SUBTYPE_MAC except ValueError: port_id_subtype = LLDP_PORT_SUBTYPE_LOCAL caps = lldp_caps_to_bits( i[4].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, }, ) neighbor = { "remote_chassis_id": chassis_id, "remote_chassis_id_subtype": chassis_id_subtype, "remote_port": port_id, "remote_port_subtype": port_id_subtype, "remote_capabilities": caps, } if i[3]: neighbor["remote_system_name"] = i[3] r += [{"local_interface": i[0], "neighbors": [neighbor]}] return r
def execute(self): res = [] interfaces = [] for n, f, r in self.cli_detail( '/interface print detail without-paging where type="ether"'): interfaces += [r["name"]] for n, f, r in self.cli_detail( "/ip neighbor print detail without-paging"): if "system-caps" not in r or r["system-caps"] == "": continue if r["interface"] not in interfaces: continue if "address4" in r and "address4" != "": chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS chassis_id = r["address4"] elif "mac-address" in r and "mac-address" != "": chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC chassis_id = r["mac-address"] else: raise self.NotSupportedError() if "interface-name" in r and "interface-name" != "": port_subtype = LLDP_PORT_SUBTYPE_NAME port = r["interface-name"] else: raise self.NotSupportedError() caps = lldp_caps_to_bits( r["system-caps"].strip().split(","), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wlan-ap": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, # "": LLDP_CAP_DOCSIS_CABLE_DEVICE, # "": LLDP_CAP_STATION_ONLY, # S-VLAN }, ) interface = { "local_interface": r["interface"], "neighbors": [{ "remote_chassis_id_subtype": chassis_id_subtype, "remote_chassis_id": chassis_id, "remote_port_subtype": port_subtype, "remote_port": port, "remote_capabilities": caps, }], } if "system-description" in r: interface["neighbors"][0]["remote_system_description"] = r[ "system-description"] res += [interface] return res
def test_lldp_caps_to_bits(value, expected): cmap = { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wap": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "phone": LLDP_CAP_TELEPHONE, "docsis": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station": LLDP_CAP_STATION_ONLY, } assert lldp_caps_to_bits(value, cmap) == expected
def execute_cli(self): r = [] v = self.cli("show lldp neighbors detail") for match in self.rx_lldp.finditer(v): iface = { "local_interface": match.group("local_port"), "neighbors": [] } if match.group("caps").strip() != "[not advertised]": cap = lldp_caps_to_bits( match.group("caps").strip().split(", "), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "access point": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "d": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station only": LLDP_CAP_STATION_ONLY, }, ) else: cap = 0 n = { "remote_chassis_id": match.group("chassis_id").strip(), "remote_chassis_id_subtype": self.CHASSIS_SUBTYPE[match.group( "chassis_id_subtype").strip()], "remote_port": match.group("port_id").strip(), "remote_port_subtype": self.PORT_SUBTYPE[match.group("port_id_subtype").strip()], "remote_capabilities": cap, } system_name = match.group("system_name").strip() if system_name and system_name != "[not advertised]": n["remote_system_name"] = system_name system_description = match.group("system_description").strip() if system_description and system_description != "[not advertised]": n["remote_system_description"] = re.sub( r"\s+", " ", system_description) port_description = match.group("port_description").strip() if port_description and port_description != "[not advertised]": n["remote_port_description"] = re.sub(r"\s+", " ", port_description) iface["neighbors"] += [n] r += [iface] return r
def execute_cli(self, **kwargs): result = [] lldp = self.cli("lldp report") for match in self.rx_int.finditer(lldp): if "ChassisID" not in lldp: continue result += [{ "local_interface": match.group("interface"), "neighbors": [{ "remote_chassis_id_subtype": { "chassis component": LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT, "interface alias": LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS, "port component": LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT, "mac": LLDP_CHASSIS_SUBTYPE_MAC, "network address": LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS, "interface name": LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME, "local": LLDP_CHASSIS_SUBTYPE_LOCAL, }[self.rx_ChassisID.search(lldp).group( "chassis_sub").lower()], "remote_chassis_id": self.rx_ChassisID.search(lldp).group("chassis_id"), "remote_port_subtype": { "interface alias": LLDP_PORT_SUBTYPE_ALIAS, "port component": LLDP_PORT_SUBTYPE_COMPONENT, "mac": LLDP_PORT_SUBTYPE_MAC, "ifname": LLDP_PORT_SUBTYPE_NAME, "local": LLDP_PORT_SUBTYPE_LOCAL, }[self.rx_PortID.search(lldp).group( "port_subtype").lower()], "remote_port": self.rx_PortID.search(lldp).group("port_id"), "remote_port_description": self.rx_PortDescr.search(lldp).group("port_descr").strip(), "remote_capabilities": lldp_caps_to_bits( self.rx_Caps.search(lldp).group( "caps").strip().lower().split(", "), { "repeater*": LLDP_CAP_REPEATER, "bridge*": LLDP_CAP_BRIDGE, "router*": LLDP_CAP_ROUTER, }, ), }], }] return result
def execute_cli(self): r = [] c = self.cli("show lldp neighbors detail") for match in self.rx_detail.finditer(c): iface = { "local_interface": match.group("local_port").strip(), "neighbors": [] } cap = lldp_caps_to_bits( match.group("caps").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, }, ) n = { "remote_chassis_id": match.group("chassis_id").strip(), "remote_chassis_id_subtype": self.CHASSIS_SUBTYPE[match.group( "chassis_id_subtype").strip()], "remote_port": match.group("port_id").strip(), "remote_port_subtype": self.PORT_SUBTYPE[match.group("port_id_subtype").strip()], "remote_capabilities": cap, } if match.group("system_name").strip(): n["remote_system_name"] = match.group("system_name").strip() if match.group("system_description").strip(): n["remote_system_description"] = match.group( "system_description").strip() if match.group("port_description").strip(): n["remote_port_description"] = match.group( "port_description").strip() iface["neighbors"] += [n] r += [iface] return r
def execute_cli(self): r = [] v = self.cli("show lldp neighbour detail") for match in self.rx_lldp.finditer(v): local_port = "port" + match.group("local_port_num").strip() iface = {"local_interface": local_port, "neighbors": []} cap = lldp_caps_to_bits( match.group("caps").strip().split(","), { "Other": LLDP_CAP_OTHER, "Repeater": LLDP_CAP_REPEATER, "Bridge": LLDP_CAP_BRIDGE, "Access Point": LLDP_CAP_WLAN_ACCESS_POINT, "Router": LLDP_CAP_ROUTER, "Telephone": LLDP_CAP_TELEPHONE, "D": LLDP_CAP_DOCSIS_CABLE_DEVICE, "Station only": LLDP_CAP_STATION_ONLY, }, ) n = { "remote_chassis_id": match.group("chassis_id").strip(), "remote_chassis_id_subtype": match.group("chassis_id_subtype").strip(), "remote_port": match.group("port_id").strip(), "remote_port_subtype": match.group("port_id_subtype").strip(), "remote_capabilities": cap, } system_name = match.group("system_name").strip() if system_name and system_name != "-": n["remote_system_name"] = system_name system_description = match.group("system_description").strip() if system_description and system_description != "-": n["remote_system_description"] = re.sub( r"\s+", " ", system_description) port_description = match.group("port_description").strip() if port_description and port_description != "-": n["remote_port_description"] = re.sub(r"\s+", " ", port_description) iface["neighbors"] += [n] r += [iface] return r
def execute_cli(self): r = [] r_rem = [] v = self.cli("show lldp remote") for match in self.rx_lldp_rem.finditer(v): chassis_id = match.group("ch_id") if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC else: chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL r_rem += [ { "local_interface": match.group("port"), "remote_chassis_id": chassis_id, "remote_chassis_id_subtype": chassis_id_subtype, } ] v = self.cli("show lldp remote detail") # If detail command not contain ch id ext_ch_id = False lldp_iter = list(self.rx_lldp.finditer(v)) if not lldp_iter: ext_ch_id = True lldp_iter = list(self.rx_lldp_womac.finditer(v)) self.logger.debug("Not Find MAC in re") for match in lldp_iter: i = {"local_interface": match.group("port"), "neighbors": []} cap = lldp_caps_to_bits( match.group("sys_caps_enabled").strip().split(","), { "n/a": 0, "other": LLDP_CAP_OTHER, "repeater/hub": LLDP_CAP_REPEATER, "bridge/switch": LLDP_CAP_BRIDGE, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "station": LLDP_CAP_STATION_ONLY, }, ) n = { "remote_chassis_id_subtype": { "macAddress": LLDP_CHASSIS_SUBTYPE_MAC, "networkAddress": LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS, }[match.group("ch_type")], "remote_chassis_id": match.group("ch_id") if not ext_ch_id else None, "remote_port_subtype": { "ifAlias": LLDP_PORT_SUBTYPE_ALIAS, "macAddress": LLDP_PORT_SUBTYPE_MAC, "ifName": LLDP_PORT_SUBTYPE_NAME, "portComponent": LLDP_PORT_SUBTYPE_NAME, "local": LLDP_PORT_SUBTYPE_LOCAL, }[match.group("port_id_subtype")], "remote_port": match.group("port_id"), "remote_capabilities": cap, } if match.group("sys_name").strip() != "N/A": n["remote_system_name"] = match.group("sys_name").strip() if match.group("sys_descr").strip() != "N/A": sd = match.group("sys_descr").strip() if "SysDesc:" in sd: sd = sd.split()[-1] n["remote_system_description"] = re.sub("\n\s{29,30}", "", sd) if match.group("port_descr").strip() != "N/A": n["remote_port_description"] = re.sub( "\n\s{29,30}", "", match.group("port_descr").strip() ) match.group("port_descr") if n["remote_chassis_id"] is None: for j in r_rem: if i["local_interface"] == j["local_interface"]: n["remote_chassis_id"] = j["remote_chassis_id"] n["remote_chassis_id_subtype"] = j["remote_chassis_id_subtype"] break i["neighbors"] += [n] r += [i] return r
def execute_cli(self): r = [] # Fallback to CLI lldp = self.cli("show lldp neighbors") for link in parse_table( lldp, allow_wrap=True, line_wrapper=None, row_wrapper=lambda x: x.strip() ): 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_cli(self): r = [] try: v = self.cli("show lldp neighbors") except self.CLISyntaxError: raise self.NotSupportedError() t = parse_table(v, allow_wrap=True) for i in t: chassis_id = i[1] if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC else: chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL port_id = i[2] if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_MAC else: port_id_subtype = LLDP_PORT_SUBTYPE_LOCAL caps = lldp_caps_to_bits( i[4].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, }, ) neighbor = { "remote_chassis_id": chassis_id, "remote_chassis_id_subtype": chassis_id_subtype, "remote_port": port_id, "remote_port_subtype": port_id_subtype, "remote_capabilities": caps, } if i[3]: neighbor["remote_system_name"] = i[3] r += [{"local_interface": i[0], "neighbors": [neighbor]}] if not t: for iface in self.scripts.get_interface_status(): c = self.cli("show lldp neighbors interface %s" % iface["interface"], ignore_errors=True) c = c.replace("\n\n", "\n") neighbors = [] for match in self.rx_neighbor.finditer(c): chassis_id = match.group("chassis_id") if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = 5 elif is_mac(chassis_id): chassis_id_subtype = 4 else: chassis_id_subtype = 7 port_id = match.group("port_id") if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = 4 elif is_mac(port_id): port_id_subtype = 3 else: port_id_subtype = 7 caps = 0 if match.group("caps").strip(): for c in match.group("caps").split(): c = c.strip() if c in {"not", "advertised"}: # not caps break if c and (c != "--"): caps |= { "O": 1, "P": 2, "B": 4, "W": 8, "R": 16, "r": 16, "T": 32, "C": 64, "S": 128, }[c] neighbor = { "remote_chassis_id": chassis_id, "remote_chassis_id_subtype": chassis_id_subtype, "remote_port": port_id, "remote_port_subtype": port_id_subtype, "remote_capabilities": caps, } port_descr = match.group("port_descr").strip() system_name = match.group("system_name").strip() system_descr = match.group("system_descr").strip() if bool(port_descr): neighbor["remote_port_description"] = port_descr if bool(system_name): neighbor["remote_system_name"] = system_name if bool(system_descr): neighbor["remote_system_description"] = system_descr neighbors += [neighbor] if neighbors: r += [{ "local_interface": iface["interface"], "neighbors": neighbors }] return r
def execute_cli(self): r = [] data = [] try: v = self.cli("show lldp neighbors") except self.CLISyntaxError: raise self.NotSupportedError() v = v.replace("\n\n", "\n") for l in parse_table(v): if not l[0]: data[-1] = [s[0] + s[1] for s in zip(data[-1], l)] continue data += [l] for d in data: try: ifname = self.profile.convert_interface_name(d[0]) except ValueError: continue v = self.cli("show lldp neighbors %s" % ifname) match = self.rx_neighbor.search(v) chassis_id = match.group("chassis_id").strip() if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS elif is_mac(chassis_id): chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_MAC else: chassis_id_subtype = LLDP_CHASSIS_SUBTYPE_LOCAL port_id = match.group("port_id").strip() if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_NETWORK_ADDRESS elif is_mac(port_id): port_id_subtype = LLDP_PORT_SUBTYPE_MAC else: port_id_subtype = LLDP_PORT_SUBTYPE_LOCAL caps = lldp_caps_to_bits( match.group("caps").strip().split(","), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wlan-access-point": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "d": LLDP_CAP_DOCSIS_CABLE_DEVICE, "h": LLDP_CAP_STATION_ONLY, }, ) neighbor = { "remote_chassis_id": chassis_id, "remote_chassis_id_subtype": chassis_id_subtype, "remote_port": port_id, "remote_port_subtype": port_id_subtype, "remote_capabilities": caps, } system_name = match.group("system_name").strip() if system_name: neighbor["remote_system_name"] = system_name system_descr = match.group("system_descr").strip() if system_descr: neighbor["remote_system_description"] = system_descr port_descr = match.group("port_descr").strip() if port_descr: neighbor["remote_port_description"] = port_descr r += [{"local_interface": ifname, "neighbors": [neighbor]}] return r
def execute(self): r = [] # HP.ProCurve advertises local(7) 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[port] = int(local_id) # Get neighbors v = self.cli("show lldp info remote-device") for ln in self.rx_split.split(v)[1].splitlines(): ln = ln.strip() if not ln: continue match = self.rx_line.search(ln) if not match: continue local_interface = match.group("port") i = {"local_interface": local_interface, "neighbors": []} # Add locally assigned port id, if exists if local_interface in local_port_ids: i["local_interface_id"] = local_port_ids[local_interface] v = self.cli("show lldp info remote-device %s" % local_interface) # Get chassis id match = self.rx_chassis_id.search(v) if not match: continue remote_chassis_id_subtype = { "mac-address": LLDP_CHASSIS_SUBTYPE_MAC, "network-address": LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS, "local": LLDP_CHASSIS_SUBTYPE_LOCAL, }[match.group(1)] remote_chassis_id = match.group(2).strip().replace(" ", "") if remote_chassis_id_subtype == LLDP_CHASSIS_SUBTYPE_MAC: remote_chassis_id = MAC(remote_chassis_id) else: remote_chassis_id = remote_chassis_id.strip() # Get remote port match = self.rx_port_id.search(v) if not match: continue remote_port_subtype = { "mac-address": LLDP_PORT_SUBTYPE_MAC, "network-address": LLDP_PORT_SUBTYPE_NETWORK_ADDRESS, "interface-name": LLDP_PORT_SUBTYPE_NAME, "inte...": LLDP_PORT_SUBTYPE_NAME, # Found in 2910al ver. W.14.38 "local": LLDP_PORT_SUBTYPE_LOCAL, }[match.group(1)] remote_port = match.group(2).strip().replace(" ", "") if remote_port_subtype == LLDP_PORT_SUBTYPE_MAC: remote_port = MAC(remote_port) else: remote_chassis_id = remote_chassis_id.strip() 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, } # Get remote system name match = self.rx_sys_name.search(v) if match: n["remote_system_name"] = match.group("sys_name").strip() # Get remote system description match = self.rx_sys_descr.search(v) if match: n["remote_system_description"] = match.group("sys_descr").strip() # Get remote port description match = self.rx_port_descr.search(v) if match: n["remote_port_description"] = match.group("port_descr").strip() # Get capabilities caps = 0 match = self.rx_cap.search(v) if match: caps = lldp_caps_to_bits( match.group(1).strip().split(", "), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wlanaccesspoint": LLDP_CAP_WLAN_ACCESS_POINT, "wlan-access-point": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "docsis": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station": LLDP_CAP_STATION_ONLY, "station-only": LLDP_CAP_STATION_ONLY, }, ) n["remote_capabilities"] = caps i["neighbors"] += [n] r += [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": LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT, "interface alias": LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS, "port component": LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT, "mac address": LLDP_CHASSIS_SUBTYPE_MAC, "network address": LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS, "interface name": LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME, "local": LLDP_CHASSIS_SUBTYPE_LOCAL, }[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": LLDP_PORT_SUBTYPE_ALIAS, "port component": LLDP_PORT_SUBTYPE_COMPONENT, "mac address": LLDP_PORT_SUBTYPE_MAC, "network address": LLDP_PORT_SUBTYPE_NETWORK_ADDRESS, "interface name": LLDP_PORT_SUBTYPE_NAME, "agent circuit id": LLDP_PORT_SUBTYPE_AGENT_CIRCUIT_ID, "locally assigned": LLDP_PORT_SUBTYPE_LOCAL, }[remote_port_subtype.strip().lower()] n["remote_port"] = m.group("port_id").strip() 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() if m.group("system_capabilities"): caps = lldp_caps_to_bits( m.group("system_capabilities").split(","), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wlan access point": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "docsis cable device": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station only": LLDP_CAP_STATION_ONLY, }, ) n["remote_capabilities"] = caps i["neighbors"] += [n] if i["neighbors"]: r += [i] return r
def execute_cli(self): # No lldp on 46xx if self.is_platform_46: raise self.NotSupportedError() 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": LLDP_CHASSIS_SUBTYPE_MAC} if match: n["remote_port_subtype"] = { "MAC Address": LLDP_PORT_SUBTYPE_MAC, "Interface name": LLDP_PORT_SUBTYPE_NAME, "Interface Name": LLDP_PORT_SUBTYPE_NAME, "Inerface Alias": LLDP_PORT_SUBTYPE_NAME, "Inerface alias": LLDP_PORT_SUBTYPE_NAME, "Interface Alias": LLDP_PORT_SUBTYPE_NAME, "Interface alias": LLDP_PORT_SUBTYPE_NAME, "Local": LLDP_PORT_SUBTYPE_LOCAL, "Locally Assigned": LLDP_PORT_SUBTYPE_LOCAL, "Locally assigned": LLDP_PORT_SUBTYPE_LOCAL, }[match.group("p_type")] if n["remote_port_subtype"] == LLDP_PORT_SUBTYPE_MAC: remote_port = MACAddressParameter().clean( match.group("p_id")) elif n["remote_port_subtype"] == LLDP_PORT_SUBTYPE_NAME: remote_port = match.group("p_id").strip() elif "-" in match.group("p_id"): # Removing bug try: remote_port = binascii.unhexlify("".join( match.group("p_id").split("-"))) except TypeError: remote_port = str(match.group("p_id")) remote_port = remote_port.rstrip("\x00") else: remote_port = match.group("p_id").strip() n["remote_chassis_id"] = match.group("id") if match.group("name"): n["remote_system_name"] = match.group("name") n["remote_port"] = str(remote_port) if match.group("capability"): caps = lldp_caps_to_bits( match.group("capability").strip().split(", "), { "other": LLDP_CAP_OTHER, "repeater": LLDP_CAP_REPEATER, "bridge": LLDP_CAP_BRIDGE, "wlan": LLDP_CAP_WLAN_ACCESS_POINT, "wlan access point": LLDP_CAP_WLAN_ACCESS_POINT, "router": LLDP_CAP_ROUTER, "telephone": LLDP_CAP_TELEPHONE, "cable": LLDP_CAP_DOCSIS_CABLE_DEVICE, "station": LLDP_CAP_STATION_ONLY, }, ) n["remote_capabilities"] = caps match = self.rx_system_descr.search(v) if match: n["remote_system_description"] = match.group("descr") match = self.rx_port_descr.search(v) if match: n["remote_port_description"] = match.group("descr") i["neighbors"] += [n] r += [i] 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