Python netaddr.IPSet() Examples

The following are 30 code examples for showing how to use netaddr.IPSet(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module netaddr , or try the search function .

Example 1
Project: px   Author: genotrance   File: px.py    License: 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
Project: pytos   Author: Tufin   File: network.py    License: 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
Project: JetPack   Author: dsp-jetpack   File: discover_nodes.py    License: 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 4
Project: JetPack   Author: dsp-jetpack   File: discover_nodes.py    License: 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 5
Project: JetPack   Author: dsp-jetpack   File: discover_nodes.py    License: 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 6
Project: networking-sfc   Author: openstack   File: flowclassifier_db.py    License: 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 7
Project: a10-neutron-lbaas   Author: a10networks   File: utils.py    License: 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 8
Project: restconf-examples   Author: CiscoDevNet   File: configuration_data_randomizer.py    License: 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 9
Project: neutron-dynamic-routing   Author: openstack   File: test_bgp_db.py    License: 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 10
Project: neutron-dynamic-routing   Author: openstack   File: test_bgp_db.py    License: 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 11
Project: ftpknocker   Author: kennell   File: utils.py    License: 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 12
Project: pytos   Author: Tufin   File: rules.py    License: 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 13
Project: pytos   Author: Tufin   File: base_types.py    License: 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()) 
Example 14
Project: pytos   Author: Tufin   File: base_types.py    License: 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()) 
Example 15
Project: pytos   Author: Tufin   File: base_types.py    License: Apache License 2.0 5 votes vote down vote up
def get_ip_set(self):
        """
        This returns an IPset representing the object.
        :rtype: IPSet
        """
        return IPSet(self.get_ip_network()) 
Example 16
Project: aws-servicebroker   Author: awslabs   File: lambda_function.py    License: Apache License 2.0 5 votes vote down vote up
def get_cidrs(size, qty, vpc_id):
    # TODO: add locking mechanism to prevent collisions when run concurrently
    vpc = ec2_client.Vpc(vpc_id)
    allocated_cidrs = netaddr.IPSet([s.cidr_block for s in vpc.subnets.all()])
    unused_cidrs = netaddr.IPSet([vpc.cidr_block]) ^ allocated_cidrs
    available_cidrs = []
    for sl in [list(s.subnet(size)) for s in unused_cidrs.iter_cidrs()]:
        available_cidrs = available_cidrs + sl
    if len(available_cidrs) < qty:
        raise Exception("Not enough available space in the vpc\n")
    return [str(s) for s in available_cidrs[-(qty):]] 
Example 17
Project: aws-servicebroker   Author: awslabs   File: lambda_function.py    License: Apache License 2.0 5 votes vote down vote up
def get_cidrs(size, qty, vpc_id):
    # TODO: add locking mechanism to prevent collisions when run concurrently
    vpc = ec2_client.Vpc(vpc_id)
    allocated_cidrs = netaddr.IPSet([s.cidr_block for s in vpc.subnets.all()])
    unused_cidrs = netaddr.IPSet([vpc.cidr_block]) ^ allocated_cidrs
    available_cidrs = []
    for sl in [list(s.subnet(size)) for s in unused_cidrs.iter_cidrs()]:
        available_cidrs = available_cidrs + sl
    if len(available_cidrs) < qty:
        raise Exception("Not enough available space in the vpc\n")
    return [str(s) for s in available_cidrs[-(qty):]] 
Example 18
Project: online-judge   Author: DMOJ   File: base_handler.py    License: 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 19
Project: stethoscope   Author: Netflix-Skunkworks   File: vpnlabeler.py    License: 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 20
Project: regional-ip-addresses   Author: x1angli   File: ipaddr.py    License: 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 21
Project: regional-ip-addresses   Author: x1angli   File: ipaddr.py    License: 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 22
Project: regional-ip-addresses   Author: x1angli   File: ipaddr.py    License: 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 23
Project: quay   Author: quay   File: __init__.py    License: 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 24
Project: Just-Metadata   Author: FortyNorthSecurity   File: orchestra.py    License: 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 25
Project: power-up   Author: IBM   File: utilities.py    License: 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 26
Project: dragonflow   Author: openstack   File: sg.py    License: 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 27
Project: dragonflow   Author: openstack   File: sg.py    License: 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 28
Project: dragonflow   Author: openstack   File: sg.py    License: 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 29
Project: dragonflow   Author: openstack   File: sg.py    License: 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 30
Project: dragonflow   Author: openstack   File: test_sg_app.py    License: 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)