Python netaddr.IPSet() Examples

The following are 30 code examples of netaddr.IPSet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module netaddr , or try the search function .
Example #1
Source File: px.py    From px with MIT License 6 votes vote down vote up
def parse_ip_ranges(iprangesconfig):
    ipranges = netaddr.IPSet([])

    iprangessplit = [i.strip() for i in iprangesconfig.split(",")]
    for iprange in iprangessplit:
        if not iprange:
            continue

        try:
            if "-" in iprange:
                spl = iprange.split("-", 1)
                ipns = netaddr.IPRange(spl[0], spl[1])
            elif "*" in iprange:
                ipns = netaddr.IPGlob(iprange)
            else:
                ipns = netaddr.IPNetwork(iprange)
            ipranges.add(ipns)
        except:
            pprint("Bad IP definition: %s" % iprangesconfig)
            sys.exit()
    return ipranges 
Example #2
Source File: network.py    From pytos with Apache License 2.0 6 votes vote down vote up
def get_ip_subnets(ip):
    """Get a list of subnets contained in the specified subnet.

    :type ip: str
    :param ip: The IP that subnets will be returned for.
    :list[netaddr.IPNetwork] 
    """
    ip = ip.strip().replace(" ", "")
    if "/" in ip:
        return [netaddr.IPNetwork(ip)]
    elif "-" in ip:
        start_ip, end_ip = ip.split("-")
        ip_set_object = netaddr.IPSet(netaddr.IPRange(start_ip, end_ip, flags=netaddr.ZEROFILL))
        return [address for address in ip_set_object.iter_cidrs()]
    else:
        if is_ipv4_string(ip):
            return [netaddr.IPNetwork(ip)]
        else:
            raise ValueError("Invalid IP string '{}'.".format(ip)) 
Example #3
Source File: sg.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def _get_cidr_changes_after_removing_addresses(cidr_set, address_list):
        """cidr_set - IPSet
           address_list - IPAddress or string list
        """
        new_cidr_set = cidr_set - netaddr.IPSet(address_list)
        added_cidr, removed_cidr = SGApp._get_cidr_difference(cidr_set,
                                                              new_cidr_set)
        return new_cidr_set, added_cidr, removed_cidr 
Example #4
Source File: vpnlabeler.py    From stethoscope with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
    super(VPNLabeler, self).__init__(*args, **kwargs)
    self._networks = netaddr.IPSet(self.config['VPN_CIDRS']) 
Example #5
Source File: ipaddr.py    From regional-ip-addresses with MIT License 5 votes vote down vote up
def write_file(scope: str, content: IPSet, prefix=''):
    if len(prefix)>0 and not prefix.endswith('-'):
        prefix = prefix + '-'
    filename = 'output/' + prefix + scope + '.txt'
    cidrs = content.iter_cidrs()
    log.info(f"Writing output file: {filename}")
    log.info(f"There are {len(cidrs)} CIDR blocks in {filename}.")
    with open(filename, 'w') as f:
        f.writelines(f"{cidr}\n" for cidr in cidrs) 
Example #6
Source File: ipaddr.py    From regional-ip-addresses with MIT License 5 votes vote down vote up
def cal_n_write_domestic_table():
    for k, v in rawstr_table.items():
        domestic_table[k] = IPSet(v)

    for k, v in domestic_table.items():
        write_file(k, v) 
Example #7
Source File: ipaddr.py    From regional-ip-addresses with MIT License 5 votes vote down vote up
def cal_complement_ipset(scope: str, content: IPSet) -> IPSet:
    if scope.endswith('v4'):
        return IPSet(['0.0.0.0/0']) - IPV4_RESERVED - content
    elif scope.endswith('v6'):
        return IPSet(['2000::/3']) - content
    else:
        log.warning(f"Unable to determine the network type {scope}. It has to be either ipv4 or ipv6.")
        return IPSet() 
Example #8
Source File: __init__.py    From quay with Apache License 2.0 5 votes vote down vote up
def _parse_amazon_ranges(ranges):
        all_amazon = IPSet()
        for service_description in ranges["prefixes"]:
            if service_description["service"] in AWS_SERVICES:
                all_amazon.add(IPNetwork(service_description["ip_prefix"]))

        return all_amazon 
Example #9
Source File: orchestra.py    From Just-Metadata with GNU General Public License v3.0 5 votes vote down vote up
def load_ips(self, file_of_systems):

        # Check to make sure file given is a valid file
        if os.path.isfile(file_of_systems):
            # read in IPs from a file
            with open(file_of_systems, "r") as system_file:
                justmetadata_system_list = system_file.readlines()
            total_systems = 0

            # Cast each IP its own object
            for system in justmetadata_system_list:
                if "/" in system:
                    try:
                        for ip in netaddr.IPSet([system]):
                            ip = str(ip)
                            activated_system_object = ip_object.IP_Information(ip)
                            if ip in self.system_objects:
                                self.system_objects[ip][1] = self.system_objects[ip][1] + 1
                                total_systems += 1
                            else:
                                self.system_objects[ip] = [activated_system_object, 1]
                                total_systems += 1
                    except netaddr.core.AddrFormatError:
                        print helpers.color("[*] Error: Bad IP CIDR range detected! (" + str(system).strip() + ")", warning=True)
                        continue
                else:
                    activated_system_object = ip_object.IP_Information(system.strip())
                    if system in self.system_objects:
                        self.system_objects[system][1] = self.system_objects[system][1] + 1
                        total_systems += 1
                    else:
                        self.system_objects[system] = [activated_system_object, 1]
                        total_systems += 1

            print helpers.color("[*] Loaded " + str(total_systems) + " systems")

        else:
            print helpers.color("\n\n[*] Error: Invalid file path provided!", warning=True)
            print helpers.color("[*] Error: Please provide the valid path to a file.", warning=True)
        return 
Example #10
Source File: utilities.py    From power-up with Apache License 2.0 5 votes vote down vote up
def is_overlapping_addr(subnet1, subnet2):
    """ Checks if two ipv4 subnets are overlapping
    Inputs:
        subnet1,subnet2 (str) ipv4 subnet in cidr format
    Returns:
        True if the two subnets overlap, False if they do not.
    """
    if IPSet([subnet1]).intersection(IPSet([subnet2])):
        return True
    else:
        return False 
Example #11
Source File: sg.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(SGApp, self).__init__(*args, **kwargs)
        self.secgroup_rule_mappings = {}
        self.next_secgroup_rule_id = 0
        self.remote_secgroup_ref = {}
        self.secgroup_associate_local_ports = {}
        self.secgroup_aggregate_addresses = collections.defaultdict(
            netaddr.IPSet
        )
        self.secgroup_ip_refs = collections.defaultdict(set)
        self.register_local_cookie_bits(COOKIE_NAME, 32) 
Example #12
Source File: discover_nodes.py    From JetPack with Apache License 2.0 5 votes vote down vote up
def ip_set_from_address_range(start, end):
    try:
        start_ip_address = ip_address_from_address(start)
        end_ip_address = ip_address_from_address(end)
    except (NotSupported, ValueError) as e:
        raise ValueError(
            ('invalid IP range: %(start)s-%(end)s (%(message)s)') %
            {
                'start': start,
                'end': end,
                'message': e.message})
    except netaddr.AddrFormatError as e:
        raise ValueError(
            ("invalid IP range: '%(start)s-%(end)s' (%(message)s)") %
            {
                'start': start,
                'end': end,
                'message': e.message})

    if start_ip_address > end_ip_address:
        raise ValueError(
            ('invalid IP range: %(start)s-%(end)s (lower bound IP greater than'
             ' upper bound)') %
            {
                'start': start,
                'end': end})

    ip_range = netaddr.IPRange(start_ip_address, end_ip_address)

    return netaddr.IPSet(ip_range) 
Example #13
Source File: sg.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def _get_cidr_changes_after_adding_addresses(cidr_set, address_list):
        """cidr_set - IPSet
           address_list - IPAddress or string list
        """
        new_cidr_set = cidr_set | netaddr.IPSet(address_list)
        added_cidr, removed_cidr = SGApp._get_cidr_difference(cidr_set,
                                                              new_cidr_set)
        return new_cidr_set, added_cidr, removed_cidr 
Example #14
Source File: sg.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def _get_cidr_changes_after_updating_addresses(cidr_set, addresses_to_add,
                                                   addresses_to_remove):
        """cidr_set - IPSet
           addresses_to_add - IPAddress or string list
           addresses_to_remove - IPAddress or string list
        """
        new_cidr_set = ((cidr_set | netaddr.IPSet(addresses_to_add)) -
                        (netaddr.IPSet(addresses_to_remove)))
        added_cidr, removed_cidr = SGApp._get_cidr_difference(cidr_set,
                                                              new_cidr_set)
        return new_cidr_set, added_cidr, removed_cidr 
Example #15
Source File: test_sg_app.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def test_aggregating_flows_for_addresses(self):
        # add one address
        old_cidr_set = netaddr.IPSet(['192.168.10.6'])
        new_cidr_set, added_cidr, deleted_cidr = \
            self.app._get_cidr_changes_after_adding_addresses(
                old_cidr_set, ['192.168.10.7'])
        expected_new_cidr_set = netaddr.IPSet(['192.168.10.6/31'])
        expected_added_cidr = {netaddr.IPNetwork('192.168.10.6/31')}
        expected_deleted_cidr = {netaddr.IPNetwork('192.168.10.6/32')}
        self.assertEqual(new_cidr_set, expected_new_cidr_set)
        self.assertEqual(added_cidr, expected_added_cidr)
        self.assertEqual(deleted_cidr, expected_deleted_cidr)

        # remove one address
        old_cidr_set = new_cidr_set
        new_cidr_set, added_cidr, deleted_cidr = \
            self.app._get_cidr_changes_after_removing_addresses(
                old_cidr_set, ['192.168.10.7'])
        expected_new_cidr_set = netaddr.IPSet(['192.168.10.6/32'])
        expected_added_cidr = {netaddr.IPNetwork('192.168.10.6/32')}
        expected_deleted_cidr = {netaddr.IPNetwork('192.168.10.6/31')}
        self.assertEqual(new_cidr_set, expected_new_cidr_set)
        self.assertEqual(added_cidr, expected_added_cidr)
        self.assertEqual(deleted_cidr, expected_deleted_cidr)

        # update addresses
        old_cidr_set = new_cidr_set
        new_cidr_set, added_cidr, deleted_cidr = \
            self.app._get_cidr_changes_after_updating_addresses(
                old_cidr_set, ['192.168.10.7'], ['192.168.10.6'])
        expected_new_cidr_set = netaddr.IPSet(['192.168.10.7/32'])
        expected_added_cidr = {netaddr.IPNetwork('192.168.10.7/32')}
        expected_deleted_cidr = {netaddr.IPNetwork('192.168.10.6/32')}
        self.assertEqual(new_cidr_set, expected_new_cidr_set)
        self.assertEqual(added_cidr, expected_added_cidr)
        self.assertEqual(deleted_cidr, expected_deleted_cidr) 
Example #16
Source File: configuration_data_randomizer.py    From netconf-examples with Apache License 2.0 5 votes vote down vote up
def random_address(base):
    """Return a random address based on a base prefix."""
    prefix = netaddr.IPNetwork(base)
    addresses = netaddr.IPSet(prefix)
    for address in [prefix.network, prefix.broadcast]:
        addresses.remove(address)
    return str(random.choice(list(addresses))) + '/' + str(prefix.prefixlen) 
Example #17
Source File: scan_network.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def yield_cidrs_on_interface(cidr_set: IPSet, ifname: str, interfaces: dict):
    """Yields each CIDR in the `cidr_set` configured on `ifname`.

    Also yields each CIDR which is a subset of a configured CIDR.
    """
    network_set = IPSet(yield_ipv4_networks_on_link(ifname, interfaces))
    for cidr in (cidr_set & network_set).iter_cidrs():
        yield str(cidr) 
Example #18
Source File: test_ntp.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_prefers_closest_addresses(self):
        subnet4 = factory.make_Subnet(version=4)
        subnet6 = factory.make_Subnet(version=6)
        # Separate subnets but sharing the VLAN, hence routable.
        subnet4v = factory.make_Subnet(version=4, vlan=subnet4.vlan)
        subnet6v = factory.make_Subnet(version=6, vlan=subnet6.vlan)

        # Create a node with an address in the first two subnets...
        node = self.make_node()
        populate_node_with_addresses(node, {subnet4, subnet6})
        # ... and a server with an address in every subnet.
        server = self.make_server()
        populate_node_with_addresses(
            server, {subnet4, subnet6, subnet4v, subnet6v}
        )

        # The NTP server addresses chosen will be those that are "closest" to
        # the node, and same-subnet wins in this over same-VLAN. No additional
        # preference is made between IPv4 or IPv6, hence we allow for either.
        preferred_subnets = subnet4, subnet6
        preferred_networks = IPSet(
            subnet.get_ipnetwork() for subnet in preferred_subnets
        )

        servers = get_servers_for(node)
        self.assertThat(servers, Not(HasLength(0)))
        self.assertThat(preferred_networks, ContainsAll(servers)) 
Example #19
Source File: test_ntp.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_prefers_closest_addresses(self):
        subnet4 = factory.make_Subnet(version=4)
        subnet6 = factory.make_Subnet(version=6)
        # Separate subnets but sharing the VLAN, hence routable.
        subnet4v1 = factory.make_Subnet(version=4, vlan=subnet4.vlan)
        subnet6v1 = factory.make_Subnet(version=6, vlan=subnet6.vlan)
        subnet4v2 = factory.make_Subnet(version=4, vlan=subnet4.vlan)
        subnet6v2 = factory.make_Subnet(version=6, vlan=subnet6.vlan)

        # Create a node with an address in the first two subnets and the first
        # two same-VLAN subnets.
        node1 = self.make_node()
        populate_node_with_addresses(
            node1, {subnet4, subnet6, subnet4v1, subnet6v1}
        )
        # Create a node with an address in the first two subnets and the
        # second two same-VLAN subnets.
        node2 = self.make_node()
        populate_node_with_addresses(
            node2, {subnet4, subnet6, subnet4v2, subnet6v2}
        )

        # The NTP server addresses chosen will be those that are "closest" to
        # the node, and same-subnet wins in this over same-VLAN. No additional
        # preference is made between IPv4 or IPv6, hence we allow for either.
        preferred_subnets = subnet4, subnet6
        preferred_networks = IPSet(
            subnet.get_ipnetwork() for subnet in preferred_subnets
        )

        for node in (node1, node2):
            peers = get_peers_for(node)
            self.assertThat(peers, Not(HasLength(0)))
            self.assertThat(preferred_networks, ContainsAll(peers)) 
Example #20
Source File: test_bgp_db.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def test_get_ipv4_tenant_subnet_routes_by_bgp_speaker_ipv4(self):
        tenant_cidr = '172.16.10.0/24'
        binding_cidr = '20.10.1.0/24'
        routes = self._advertised_routes_by_bgp_speaker(4, 1234, tenant_cidr,
                                                        binding_cidr)
        routes = list(routes)
        self.assertEqual(1, len(routes))
        dest_prefix = routes[0]['destination']
        next_hop = routes[0]['next_hop']
        self.assertEqual(tenant_cidr, dest_prefix)
        self.assertTrue(netaddr.IPSet([binding_cidr]).__contains__(next_hop)) 
Example #21
Source File: discover_nodes.py    From JetPack with Apache License 2.0 5 votes vote down vote up
def parse_idrac_arguments(idrac_list):
    ip_set = netaddr.IPSet()

    for idrac in idrac_list:
        ip_set = ip_set.union(ip_set_from_idrac(idrac))

    return ip_set 
Example #22
Source File: discover_nodes.py    From JetPack with Apache License 2.0 5 votes vote down vote up
def ip_set_from_address(address):
    ip_set = netaddr.IPSet()

    try:
        ip_address = ip_address_from_address(address)
        ip_set.add(ip_address)
    except ValueError:
        ip_network = ip_network_from_address(address)
        ip_set.update(ip_network.iter_hosts())

    return ip_set 
Example #23
Source File: flowclassifier_db.py    From networking-sfc with Apache License 2.0 5 votes vote down vote up
def _ip_prefix_conflict(cls, first_ip_prefix, second_ip_prefix):
        if first_ip_prefix is None or second_ip_prefix is None:
            return True
        first_ipset = netaddr.IPSet([first_ip_prefix])
        second_ipset = netaddr.IPSet([second_ip_prefix])
        return bool(first_ipset & second_ipset) 
Example #24
Source File: utils.py    From a10-neutron-lbaas with Apache License 2.0 5 votes vote down vote up
def find_unused_ip(ip_range_begin, ip_range_end, ips_in_use):
        candidate = None

        in_range = ipset(iprange(ip_range_begin, ip_range_end))
        in_use = ipset(ips_in_use)

        try:
            candidate = str(ipaddr((in_range - in_use).pop()))
        except Exception:
            LOG.error("Could not allocate IP address for range:{0}-{1}".format(
                ip_range_begin, ip_range_end))
        finally:
            return candidate 
Example #25
Source File: configuration_data_randomizer.py    From restconf-examples with Apache License 2.0 5 votes vote down vote up
def random_address(base):
    """Return a random address based on a base prefix."""
    prefix = netaddr.IPNetwork(base)
    addresses = netaddr.IPSet(prefix)
    for address in [prefix.network, prefix.broadcast]:
        addresses.remove(address)
    return str(random.choice(list(addresses))) + '/' + str(prefix.prefixlen) 
Example #26
Source File: test_bgp_db.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def test_get_ipv6_tenant_subnet_routes_by_bgp_speaker_ipv6(self):
        tenant_cidr = '2001:db8::/64'
        binding_cidr = '2001:ab8::/64'
        routes = self._advertised_routes_by_bgp_speaker(6, 1234, tenant_cidr,
                                                        binding_cidr)
        self.assertEqual(1, len(routes))
        dest_prefix = routes[0]['destination']
        next_hop = routes[0]['next_hop']
        self.assertEqual(tenant_cidr, dest_prefix)
        self.assertTrue(netaddr.IPSet([binding_cidr]).__contains__(next_hop)) 
Example #27
Source File: base_handler.py    From online-judge with GNU Affero General Public License v3.0 5 votes vote down vote up
def proxy_list(human_readable):
    globs = []
    addrs = []
    for item in human_readable:
        if '*' in item or '-' in item:
            globs.append(IPGlob(item))
        else:
            addrs.append(item)
    return IPSet(chain(chain.from_iterable(globs), addrs)) 
Example #28
Source File: utils.py    From ftpknocker with MIT License 5 votes vote down vote up
def targets_to_ip_list(targets):
    ipset = IPSet()
    for t in targets:
        ipset.add(t)
    return [str(ip) for ip in ipset] 
Example #29
Source File: rules.py    From pytos with Apache License 2.0 5 votes vote down vote up
def as_netaddr_set(self):
        """This returns a netaddr set representing the TrafficRange"""
        return netaddr.IPSet(self.as_netaddr_obj()) 
Example #30
Source File: base_types.py    From pytos with Apache License 2.0 5 votes vote down vote up
def as_netaddr_set(self):
        """This returns a netaddr set representing the Network_Object"""
        return netaddr.IPSet(self.as_netaddr_obj())