How to use the netaddr.IPNetwork function in netaddr

To help you get started, we’ve selected a few netaddr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jathanism / cidrize / tests / test_cidrize.py View on Github external
def test_hyphen_style_loose_toobig(self):
        # IPRange objects larger than /18 will always be strict.
        expected = [IPNetwork('10.0.0.0/18'), IPNetwork('10.0.64.0/29')]
        _input = '10.0.0.0-10.0.64.7'
        self.assertEqual(expected, self.test(_input, strict=False))
github drkjam / netaddr / test / ip / test_ip_v4.py View on Github external
def test_ipnetwork_index_operations_v4():
    ip = IPNetwork('192.0.2.16/29')
    assert ip[0] == IPAddress('192.0.2.16')
    assert ip[1] == IPAddress('192.0.2.17')
    assert ip[-1] == IPAddress('192.0.2.23')
github drkjam / netaddr / test / ip / test_platform_osx.py View on Github external
def test_ip_behaviour_osx():
    assert iprange_to_cidrs('::1', '::255.255.255.254') == [
        IPNetwork('::1/128'),
        IPNetwork('::0.0.0.2/127'),
        IPNetwork('::0.0.0.4/126'),
        IPNetwork('::0.0.0.8/125'),
        IPNetwork('::0.0.0.16/124'),
        IPNetwork('::0.0.0.32/123'),
        IPNetwork('::0.0.0.64/122'),
        IPNetwork('::0.0.0.128/121'),
        IPNetwork('::0.0.1.0/120'),
        IPNetwork('::0.0.2.0/119'),
        IPNetwork('::0.0.4.0/118'),
        IPNetwork('::0.0.8.0/117'),
        IPNetwork('::0.0.16.0/116'),
        IPNetwork('::0.0.32.0/115'),
        IPNetwork('::0.0.64.0/114'),
        IPNetwork('::0.0.128.0/113'),
        IPNetwork('::0.1.0.0/112'),
        IPNetwork('::0.2.0.0/111'),
        IPNetwork('::0.4.0.0/110'),
        IPNetwork('::0.8.0.0/109'),
        IPNetwork('::0.16.0.0/108'),
        IPNetwork('::0.32.0.0/107'),
        IPNetwork('::0.64.0.0/106'),
        IPNetwork('::0.128.0.0/105'),
        IPNetwork('::1.0.0.0/104'),
        IPNetwork('::2.0.0.0/103'),
        IPNetwork('::4.0.0.0/102'),
        IPNetwork('::8.0.0.0/101'),
        IPNetwork('::16.0.0.0/100'),
github drkjam / netaddr / test / ip / test_platform_osx.py View on Github external
IPNetwork('::4/126'),
        IPNetwork('::8/125'),
        IPNetwork('::10/124'),
        IPNetwork('::20/123'),
        IPNetwork('::40/122'),
        IPNetwork('::80/121'),
        IPNetwork('::100/120'),
        IPNetwork('::200/119'),
        IPNetwork('::400/118'),
        IPNetwork('::800/117'),
        IPNetwork('::1000/116'),
        IPNetwork('::2000/115'),
        IPNetwork('::4000/114'),
        IPNetwork('::8000/113'),
        IPNetwork('::0.1.0.0/112'),
        IPNetwork('::0.2.0.0/111'),
        IPNetwork('::0.4.0.0/110'),
        IPNetwork('::0.8.0.0/109'),
        IPNetwork('::0.16.0.0/108'),
        IPNetwork('::0.32.0.0/107'),
        IPNetwork('::0.64.0.0/106'),
        IPNetwork('::0.128.0.0/105'),
        IPNetwork('::1.0.0.0/104'),
        IPNetwork('::2.0.0.0/103'),
        IPNetwork('::4.0.0.0/102'),
        IPNetwork('::8.0.0.0/101'),
        IPNetwork('::16.0.0.0/100'),
        IPNetwork('::32.0.0.0/99'),
        IPNetwork('::64.0.0.0/98'),
        IPNetwork('::128.0.0.0/98'),
        IPNetwork('::192.0.0.0/99'),
        IPNetwork('::224.0.0.0/100'),
github drkjam / netaddr / test / ip / test_network_ops.py View on Github external
def test_subnetting():
    ip = IPNetwork('172.24.0.0/23')
    assert isinstance(ip.subnet(28), types.GeneratorType)

    subnets = list(ip.subnet(28))
    assert len(subnets) == 32

    assert subnets == [
        IPNetwork('172.24.0.0/28'),
        IPNetwork('172.24.0.16/28'),
        IPNetwork('172.24.0.32/28'),
        IPNetwork('172.24.0.48/28'),
        IPNetwork('172.24.0.64/28'),
        IPNetwork('172.24.0.80/28'),
        IPNetwork('172.24.0.96/28'),
        IPNetwork('172.24.0.112/28'),
        IPNetwork('172.24.0.128/28'),
        IPNetwork('172.24.0.144/28'),
github datacenter / cloudcenter-content / callouts / infoblox / ipam.py View on Github external
"*networkId": network_id,
        "_return_fields": "extattrs,network"
    }
    headers = {}
    response = s.request("GET", url, headers=headers, params=querystring, verify=False,
                         auth=(ib_user, ib_pass))
    if len(response.json()) != 1:
        logging.error("Must have exactly one network in Infoblox with "
                      "extensible attribute networkId matching network "
                      "{}. Found {} instead.".format(
                        network_id, len(response.json())
                        ))
        exit(1)
    gateway = response.json()[0]['extattrs']['Gateway']['value']
    subnet = response.json()[0]['network']
    netmask = str(netaddr.IPNetwork(subnet).netmask)

    # Create Host Record
    url = "{}/record:host".format(ib_api_endpoint)
    fqdn = "{hostname}nic{idx}.{domain}".format(hostname=hostname, idx=nic_index, domain=domain)
    payload = {
        "ipv4addrs": [
            {
                "ipv4addr": "func:nextavailableip:{subnet}".format(subnet=subnet)
            }
        ],
        "name": fqdn,
        "configure_for_dns": True
    }
    headers = {'content-type': "application/json"}
    try:
        response = s.request("POST", url, data=json.dumps(payload), headers=headers, verify=False,
github DanMcInerney / SMB-reverse-brute / SMB-reverse-brute.py View on Github external
def parse_nmap(args):
    '''
    Either performs an Nmap scan or parses an Nmap xml file
    Will either return the parsed report or exit script
    '''
    if args.xml:
        report = NmapParser.parse_fromfile(args.xml)
    elif args.hostlist:
        hosts = []
        with open(args.hostlist, 'r') as hostlist:
            host_lines = hostlist.readlines()
            for line in host_lines:
                line = line.strip()
                if '/' in line:
                    hosts += [str(ip) for ip in IPNetwork(line)]
                elif '*' in line:
                    sys.exit('[-] CIDR notation only in the host list e.g. 10.0.0.0/24')
                else:
                    hosts.append(line)
        report = nmap_scan(hosts)
    else:
        print('Use the "-x [path/to/nmap-output.xml]" option if you already have an Nmap XML file \
or "-l [hostlist.txt]" option to run an Nmap scan with a hostlist file.')
        sys.exit()
    return report
github duo-labs / cloudmapper / shared / common.py View on Github external
def is_unblockable_cidr(cidr):
    ipnetwork = IPNetwork(cidr)
    if (
        ipnetwork in IPNetwork("169.254.0.0/16")
        or ipnetwork in IPNetwork("127.0.0.0/8")  # link local
        or ipnetwork in IPNetwork("192.0.2.0/24")  # loopback
        or ipnetwork in IPNetwork("198.51.100.0/24")  # Test network from RFC 5737
        or ipnetwork in IPNetwork("203.0.113.0/24")  # Test network
        or ipnetwork in IPNetwork("224.0.0.0/4")  # Test network
        or ipnetwork in IPNetwork("240.0.0.0/5")  # class D multicast
        or ipnetwork in IPNetwork("248.0.0.0/5")  # class E reserved
        or ipnetwork in IPNetwork("255.255.255.255/32")  # reserved  # broadcast
    ):
        return True
    return False
github openstack / tempest / tempest / api / network / base.py View on Github external
def create_subnet(cls, network, gateway='', cidr=None, mask_bits=None,
                      ip_version=None, client=None, **kwargs):
        """Wrapper utility that returns a test subnet."""
        # allow tests to use admin client
        if not client:
            client = cls.subnets_client

        # The cidr and mask_bits depend on the ip version.
        ip_version = ip_version if ip_version is not None else cls._ip_version
        gateway_not_set = gateway == ''
        if ip_version == 4:
            cidr = cidr or netaddr.IPNetwork(CONF.network.project_network_cidr)
            mask_bits = mask_bits or CONF.network.project_network_mask_bits
        elif ip_version == 6:
            cidr = (cidr or
                    netaddr.IPNetwork(CONF.network.project_network_v6_cidr))
            mask_bits = mask_bits or CONF.network.project_network_v6_mask_bits
        # Find a cidr that is not in use yet and create a subnet with it
        for subnet_cidr in cidr.subnet(mask_bits):
            if gateway_not_set:
                gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)
            else:
                gateway_ip = gateway
            try:
                body = client.create_subnet(
                    network_id=network['id'],
                    cidr=str(subnet_cidr),
                    ip_version=ip_version,
github globocom / GloboNetworkAPI / networkapi / api_rack / autoprovision.py View on Github external
prefixInternoFEV6 = int(net.subnet_mask)

        elif prod.divisao_dc.nome == "BO_DSR":
            for net in prod.configs:
                if net.ip_version == "v4":
                    CIDRBO_DSRipv4interno = IPNetwork(str(net.network))
                    prefixInternoBO_DSRV4 = int(net.subnet_mask)
                else:
                    log.debug(str(net.network))
                    CIDRBO_DSRipv6interno = IPNetwork(str(net.network))
                    prefixInternoBO_DSRV6 = int(net.subnet_mask)

        elif prod.divisao_dc.nome == "BOCACHOS-A":
            for net in prod.configs:
                if net.ip_version == "v4":
                    CIDRBOCAAipv4interno = IPNetwork(str(net.network))
                    prefixInternoBOCAAV4 = int(net.subnet_mask)
                else:
                    log.debug(str(net.network))
                    CIDRBOCAAipv6interno = IPNetwork(str(net.network))
                    prefixInternoBOCAAV6 = int(net.subnet_mask)
        elif prod.divisao_dc.nome == "BOCACHOS-B":
            for net in prod.configs:
                if net.ip_version == "v4":
                    CIDRBOCABipv4interno = IPNetwork(str(net.network))
                    prefixInternoBOCABV4 = int(net.subnet_mask)
                else:
                    log.debug(str(net.network))
                    CIDRBOCABipv6interno = IPNetwork(str(net.network))
                    prefixInternoBOCABV6 = int(net.subnet_mask)

    log.debug(str(lf_env))