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(self): r = [] data = [] try: v = self.cli("show lldp remote-device all") except self.CLISyntaxError: raise self.NotSupportedError() v = v.replace("\n\n", "\n") for l in parse_table(v, allow_extend=True): if not l[0]: data[-1] = [s[0] + s[1] for s in zip(data[-1], l)] continue data += [l] for d in data: chassis_id = d[2] if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = 5 else: try: MACAddressParameter().clean(chassis_id) chassis_id_subtype = 4 except ValueError: chassis_id_subtype = 7 port_id = d[3] if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = 4 else: try: MACAddressParameter().clean(port_id) port_id_subtype = 3 except ValueError: port_id_subtype = 7 # caps = sum([self.CAPS[s.strip()] for s in d[4].split(",")]) caps = 0 if not chassis_id: continue 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 match.group("system_name"): neighbor["remote_system_name"] = match.group("system_name") """ neighbor["remote_system_name"] = d[4] r += [{ # "local_interface": match.group("port"), "local_interface": d[0], "neighbors": [neighbor], }] return r
def execute(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() t = parse_table(v, allow_wrap=True, allow_extend=True) for i in t: chassis_id = i[1] if is_ipv4(chassis_id) or is_ipv6(chassis_id): chassis_id_subtype = 5 else: try: MACAddressParameter().clean(chassis_id) chassis_id_subtype = 4 except ValueError: chassis_id_subtype = 7 port_id = i[2] if is_ipv4(port_id) or is_ipv6(port_id): port_id_subtype = 4 else: try: MACAddressParameter().clean(port_id) port_id_subtype = 3 except ValueError: port_id_subtype = 7 caps = 0 for c in i[4].split(","): c = c.strip() if c: caps |= { "O": 1, "P": 2, "B": 4, "W": 8, "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 } if i[3]: neighbor["remote_system_name"] = i[3] r += [{ "local_interface": i[0], "neighbors": [neighbor] }] return r
def execute(self): r = [] try: v = self.cli("show lldp info") except self.CLISyntaxError: raise self.NotSupportedError() for match in self.rx_line.finditer(v): 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 # Need more examples if "Bridge" in match.group("caps"): caps += 4 if "Router" in match.group("caps"): caps += 16 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 match.group("system_name"): neighbor["remote_system_name"] = \ match.group("system_name").strip() if match.group("system_description"): neighbor["remote_system_description"] = \ match.group("system_description").strip() if match.group("port_description"): neighbor["remote_port_description"] = \ match.group("port_description").strip() r += [{ "local_interface": match.group("port"), "neighbors": [neighbor] }] return r
def fix_ip_addr(self, ipaddr_section): """ :rtype : dict """ result = { "ipv4_addresses": [], "ipv6_addresses": [], "enabled_afi": [] } if "Unnumbered If" in ipaddr_section: return result for line in ipaddr_section.splitlines(): match_obj = self.re_ipaddr.search(line) if match_obj: afi = match_obj.group(1) ip = match_obj.group(2) if afi == "IP Addr/mask" and "Not" not in ip: result["ipv4_addresses"] += [ip] elif afi == "IPv6 Addr" and is_ipv6(ip): result["ipv6_addresses"] += [ip] if result["ipv4_addresses"]: result["enabled_afi"] += ["IPv4"] if result["ipv6_addresses"]: result["enabled_afi"] += ["IPv6"] return result
def fix_ip_addr(self, ipaddr_section): """ :rtype : dict """ result = { 'ipv4_addresses': [], 'ipv6_addresses': [], 'enabled_afi': [], } if "Unnumbered If" in ipaddr_section: return result for line in ipaddr_section.splitlines(): match_obj = self.re_ipaddr.search(line) if match_obj: afi = match_obj.group(1) ip = match_obj.group(2) if afi == 'IP Addr/mask' and 'Not' not in ip: result['ipv4_addresses'] += [ip] elif afi == 'IPv6 Addr' and is_ipv6(ip): result['ipv6_addresses'] += [ip] if result['ipv4_addresses']: result['enabled_afi'] += ['IPv4'] if result['ipv6_addresses']: result['enabled_afi'] += ['IPv6'] return result
def execute(self, address, count=None, source_address=None, size=None): if is_ipv4(address): cmd = "ping ip %s" % address elif is_ipv6(address): cmd = "ping ipv6 %s" % address if count: cmd += " repeat %d" % int(count) if size: cmd += " size %d" % int(size) s = self.cli(cmd) match = self.rx_result.search(s) return match.groupdict()
def execute(self, address, count=None, source_address=None, size=None): if is_ipv4(address): cmd = "ping ip %s" % address elif is_ipv6(address): cmd = "ping ipv6 %s" % address if count: cmd += " count %d" % int(count) if size: cmd += " size %d" % int(size) try: s = self.cli(cmd) except self.CLISyntaxError: if is_ipv4(address): cmd = "ping" elif is_ipv6(address): cmd = "ping6 %s" if count: cmd += " -n %d" % int(count) if size: cmd += " -l %d" % int(size) cmd = "%s %s" % (cmd, address) s = self.cli(cmd) match = self.rx_result1.search(s) if match: return { "success": match.group("success"), "count": match.group("count"), "min": match.group("min"), "avg": match.group("avg"), "max": match.group("max") } else: match = self.rx_result2.search(s) return { "success": match.group("success"), "count": match.group("count") }
def clean(self, data): data = super(AddressRangeApplication, self).clean(data) afi = data["afi"] from_address = data["from_address"] to_address = data["to_address"] # Check AFI address_validator = is_ipv4 if afi == "4" else is_ipv6 if not address_validator(from_address): raise ValueError("Invalid IPv%(afi)s 'From Address'" % {"afi": afi}) if not address_validator(to_address): raise ValueError("Invalid IPv%(afi)s 'To Address'" % {"afi": afi}) # Check from address not greater than to address if IP.prefix(from_address) > IP.prefix(to_address): raise ValueError( "'To Address' must be greater or equal than 'From Address'") # Check for valid "action" combination if "fqdn_template" in data and data[ "fqdn_template"] and data["action"] != "G": raise ValueError( "'FQDN Template' must be clean for selected 'Action'") if "reverse_nses" in data and data[ "reverse_nses"] and data["action"] != "D": raise ValueError( "'Reverse NSes' must be clean for selected 'Action'") # Set range as locked for "G" and "D" actions if data["action"] != "N": data["is_locked"] = True # @todo: check FQDN template # Check reverse_nses is a list of FQDNs or IPs if "reverse_nses" in data and data["reverse_nses"]: reverse_nses = data["reverse_nses"] for ns in reverse_nses.split(","): ns = ns.strip() if not is_ipv4(ns) and not is_ipv6(ns) and not is_fqdn(ns): raise ValueError("%s is invalid nameserver" % ns) # Check no locked range overlaps another locked range if data["is_locked"]: r = [ r for r in AddressRange.get_overlapping_ranges( data["vrf"], data["afi"], data["from_address"], data["to_address"]) if r.is_locked is True and r.name != data["name"] ] if r: raise ValueError( "Locked range overlaps with ahother locked range: %s" % unicode(r[0])) return data
def migrate(self): fixes, dot_fixes = [], [] for mo_id, address in self.db.execute( "SELECT id, address FROM sa_managedobject"): if not address: continue if is_ipv4(address) or is_ipv6(address): continue if address.endswith("."): fixes += [str(mo_id)] else: dot_fixes += [str(mo_id)] if fixes: self.db.execute(""" UPDATE sa_managedobject SET fqdn=address, address_resolution_policy='O' WHERE id IN (%s) """ % ",".join(fixes)) if dot_fixes: self.db.execute(""" UPDATE sa_managedobject SET fqdn=concat(address, '.'), address_resolution_policy='O' WHERE id IN (%s) """ % ",".join(dot_fixes))
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: local_if = i[0] v = self.cli("show lldp neighbors %s" % local_if) match = self.rx_neighbor.search(v) remote_chassis_id = match.group("remote_chassis_id") if is_ipv4(remote_chassis_id) or is_ipv6(remote_chassis_id): # Actually networkAddress(4) remote_chassis_id_subtype = 5 elif is_mac(remote_chassis_id): # Actually macAddress(3) # Convert MAC to common form remote_chassis_id_subtype = 4 else: remote_chassis_id_subtype = 7 remote_port = match.group("remote_port") if is_ipv4(remote_port) or is_ipv6(remote_port): # Actually networkAddress(4) remote_port_subtype = 4 elif is_mac(remote_port): # Actually macAddress(3) remote_port_subtype = 3 elif is_int(remote_port): # Actually local(7) remote_port_subtype = 7 else: remote_port_subtype = 5 # Get capability cap = 0 s = match.group("caps") for c in s.strip().split(", "): cap |= { "Other": 1, "Repeater": 2, "Bridge": 4, "WLAN": 8, "Router": 16, "Telephone": 32, "Cable": 64, "Station": 128 }[c] 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 } match = self.rx_system_name.search(v) if match and match.group("system_name"): n["remote_system_name"] = match.group("system_name") match = self.rx_port_descr.search(v) if match and match.group("port_descr"): n["remote_port_description"] = match.group("port_descr") i = { "local_interface": local_if, "neighbors": [n] } r += [i] 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 = 5 elif is_mac(chassis_id): chassis_id_subtype = 4 else: chassis_id_subtype = 7 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 = 0 for c in match.group("sys_caps_enabled").strip().split(","): cap |= { "N/A": 0, "Other": 1, "Repeater/Hub": 2, "Bridge/Switch": 4, "Router": 16, "Telephone": 32, "Station": 128 }[c] n = { "remote_chassis_id_subtype": { "macAddress": 4, "networkAddress": 5 }[match.group("ch_type")], "remote_chassis_id": match.group("ch_id") if not ext_ch_id else None, "remote_port_subtype": { "ifAlias": 1, "macAddress": 3, "ifName": 5, "portComponent": 5, "local": 7 }[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