def list_interface_route_ips(ip_type, interface): """ List IP addresses for which there are routes to a given interface. :param str ip_type: IP type, either futils.IPV4 or futils.IPV6 :param str interface: Interface name :returns: a set of all addresses for which there is a route to the device. """ ips = set() if ip_type == futils.IPV4: data = futils.check_call( ["ip", "route", "list", "dev", interface]).stdout else: data = futils.check_call( ["ip", "-6", "route", "list", "dev", interface]).stdout lines = data.split("\n") _log.debug("Existing routes to %s : %s", interface, lines) for line in lines: # Example of the lines we care about is (having specified the # device above): "10.11.2.66 proto static scope link" words = line.split() if len(words) > 1: ip = words[0] if common.validate_ip_addr(ip, futils.IP_TYPE_TO_VERSION[ip_type]): # Looks like an IP address. Note that we here are ignoring # routes to networks configured when the interface is created. ips.add(words[0]) _log.debug("Found existing IP addresses : %s", ips) return ips
def list_interface_route_ips(ip_type, interface): """ List IP addresses for which there are routes to a given interface. :param str ip_type: IP type, either futils.IPV4 or futils.IPV6 :param str interface: Interface name :returns: a set of all addresses for which there is a route to the device. """ ips = set() if ip_type == futils.IPV4: data = futils.check_call(["ip", "route", "list", "dev", interface]).stdout else: data = futils.check_call( ["ip", "-6", "route", "list", "dev", interface]).stdout lines = data.split("\n") _log.debug("Existing routes to %s : %s", interface, lines) for line in lines: # Example of the lines we care about is (having specified the # device above): "10.11.2.66 proto static scope link" words = line.split() if len(words) > 1: ip = words[0] if common.validate_ip_addr(ip, futils.IP_TYPE_TO_VERSION[ip_type]): # Looks like an IP address. Note that we here are ignoring # routes to networks configured when the interface is created. ips.add(words[0]) _log.debug("Found existing IP addresses : %s", ips) return ips
def __init__(self, fields): #*********************************************************************# #* This constructor throws InvalidAddress if there is a missing or *# #* invalid field. *# #*********************************************************************# try: self.ip = fields['addr'].encode('ascii') except KeyError: raise InvalidAddress if common.validate_ip_addr(self.ip, 4): self.type = futils.IPV4 elif common.validate_ip_addr(self.ip, 6): self.type = futils.IPV6 else: raise InvalidAddress
def validate_endpoint(config, endpoint): """ Ensures that the supplied endpoint is valid. Once this routine has returned successfully, we know that all required fields are present and have valid values. :param config: configuration structure :param endpoint: endpoint dictionary as read from etcd :raises ValidationFailed """ issues = [] if not isinstance(endpoint, dict): raise ValidationFailed("Expected endpoint to be a dict.") if "state" not in endpoint: issues.append("Missing 'state' field.") elif endpoint["state"] not in ("active", "inactive"): issues.append("Expected 'state' to be one of active/inactive.") for field in ["name", "mac", "profile_id"]: if field not in endpoint: issues.append("Missing '%s' field." % field) elif not isinstance(endpoint[field], StringTypes): issues.append("Expected '%s' to be a string; got %r." % (field, endpoint[field])) if "name" in endpoint: if not endpoint["name"].startswith(config.IFACE_PREFIX): issues.append("Interface %r does not start with %r." % (endpoint["name"], config.IFACE_PREFIX)) for version in (4, 6): nets = "ipv%d_nets" % version if nets not in endpoint: issues.append("Missing network %s." % nets) else: for ip in endpoint.get(nets, []): if not common.validate_cidr(ip, version): issues.append("IP address %r is not a valid IPv%d CIDR." % (ip, version)) break gw_key = "ipv%d_gateway" % version try: gw_str = endpoint[gw_key] if gw_str is not None and not common.validate_ip_addr(gw_str, version): issues.append("%s is not a valid IPv%d gateway address." % (gw_key, version)) except KeyError: pass if issues: raise ValidationFailed(" ".join(issues))
def list_interface_ips(type, interface): """ List IP addresses for which there are routes to a given interface. Returns a set with all addresses for which there is a route to the device. """ ips = set() if type == futils.IPV4: data = futils.check_call(["ip", "route", "list", "dev", interface]).stdout else: data = futils.check_call( ["ip", "-6", "route", "list", "dev", interface]).stdout lines = data.split("\n") log.debug("Existing routes to %s : %s" % (interface, ",".join(lines))) for line in lines: #*********************************************************************# #* Example of the lines we care about is (having specified the *# #* device above) : *# #* 10.11.2.66 proto static scope link *# #*********************************************************************# words = line.split() if len(words) > 1: ip = words[0] if common.validate_ip_addr(ip, None): # Looks like an IP address. Note that we here are ignoring # routes to networks configured when the interface is created. ips.add(words[0]) log.debug("Found existing IP addresses : %s", ips) return ips
def parse_host_ip(hostname, raw_value): if raw_value is None or validate_ip_addr(raw_value): return canonicalise_ip(raw_value, None) else: _log.debug("%s has invalid IP: %r", hostname, raw_value) return None
def test_validate_ip_addr(self): self.assertTrue(common.validate_ip_addr("1.2.3.4", 4)) self.assertFalse(common.validate_ip_addr("1.2.3.4.5", 4)) self.assertFalse(common.validate_ip_addr("1.2.3.4/32", 4)) self.assertTrue(common.validate_ip_addr("1.2.3", 4)) self.assertFalse(common.validate_ip_addr("bloop", 4)) self.assertFalse(common.validate_ip_addr("::", 4)) self.assertFalse(common.validate_ip_addr("2001::abc", 4)) self.assertFalse(common.validate_ip_addr("2001::a/64", 4)) self.assertFalse(common.validate_ip_addr("1.2.3.4", 6)) self.assertFalse(common.validate_ip_addr("1.2.3.4.5", 6)) self.assertFalse(common.validate_ip_addr("1.2.3.4/32", 6)) self.assertFalse(common.validate_ip_addr("1.2.3", 6)) self.assertFalse(common.validate_ip_addr("bloop", 6)) self.assertTrue(common.validate_ip_addr("::", 6)) self.assertTrue(common.validate_ip_addr("2001::abc", 6)) self.assertFalse(common.validate_ip_addr("2001::a/64", 6)) self.assertTrue(common.validate_ip_addr("1.2.3.4", None)) self.assertFalse(common.validate_ip_addr("1.2.3.4.5", None)) self.assertFalse(common.validate_ip_addr("1.2.3.4/32", None)) self.assertTrue(common.validate_ip_addr("1.2.3", None)) self.assertFalse(common.validate_ip_addr("bloop", None)) self.assertTrue(common.validate_ip_addr("::", None)) self.assertTrue(common.validate_ip_addr("2001::abc", None)) self.assertFalse(common.validate_ip_addr("2001::a/64", None)) self.assertFalse(common.validate_ip_addr(None, None))