Python ipaddress.ip_network() Examples

The following are 30 code examples of ipaddress.ip_network(). 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 ipaddress , or try the search function .
Example #1
Source File: __init__.py    From aegea with Apache License 2.0 7 votes vote down vote up
def ensure_subnet(vpc, availability_zone=None):
    if availability_zone is not None and availability_zone not in availability_zones():
        msg = "Unknown availability zone {} (choose from {})"
        raise AegeaException(msg.format(availability_zone, list(availability_zones())))
    for subnet in vpc.subnets.all():
        if availability_zone is not None and subnet.availability_zone != availability_zone:
            continue
        break
    else:
        from ipaddress import ip_network
        from ... import config
        subnet_cidrs = ip_network(str(config.vpc.cidr[ARN.get_region()])).subnets(new_prefix=config.vpc.subnet_prefix)
        subnets = {}
        for az, subnet_cidr in zip(availability_zones(), subnet_cidrs):
            logger.info("Creating subnet with CIDR %s in %s, %s", subnet_cidr, vpc, az)
            subnets[az] = resources.ec2.create_subnet(VpcId=vpc.id, CidrBlock=str(subnet_cidr), AvailabilityZone=az)
            clients.ec2.get_waiter("subnet_available").wait(SubnetIds=[subnets[az].id])
            add_tags(subnets[az], Name=__name__)
            clients.ec2.modify_subnet_attribute(SubnetId=subnets[az].id,
                                                MapPublicIpOnLaunch=dict(Value=config.vpc.map_public_ip_on_launch))
        subnet = subnets[availability_zone] if availability_zone is not None else list(subnets.values())[0]
    return subnet 
Example #2
Source File: ip.py    From CrackMapExec with BSD 2-Clause "Simplified" License 7 votes vote down vote up
def parse_targets(target):
    try:
        if '-' in target:
            start_ip, end_ip = target.split('-')
            try:
                end_ip = ip_address(end_ip)
            except ValueError:
                first_three_octets = start_ip.split(".")[:-1]
                first_three_octets.append(end_ip)
                end_ip = ip_address(
                            ".".join(first_three_octets)
                        )

            for ip_range in summarize_address_range(ip_address(start_ip), end_ip):
                for ip in ip_range:
                    yield str(ip)
        else:
            for ip in ip_network(target, strict=False):
                yield str(ip)
    except ValueError:
        yield str(target) 
Example #3
Source File: rules.py    From netjsonconfig with GNU General Public License v3.0 6 votes vote down vote up
def __intermediate_rule(self, rule, index):
        src_net = None
        dest_net = None
        family = 4
        if 'src' in rule:
            src_net = ip_network(rule['src'])
        if 'dest' in rule:
            dest_net = ip_network(rule['dest'])
        if dest_net or src_net:
            family = dest_net.version if dest_net else src_net.version
        rule.update(
            {
                '.type': 'rule{0}'.format(family).replace('4', ''),
                '.name': rule.pop('name', None) or self.__get_auto_name(index),
            }
        )
        return self.sorted_dict(rule) 
Example #4
Source File: test_reservations.py    From Paradrop with Apache License 2.0 6 votes vote down vote up
def test_SubnetReservationSet():
    resv = reservations.SubnetReservationSet()
    assert len(resv) == 0

    net = ipaddress.ip_network(u'10.0.0.0/8')
    resv.add(net)
    assert len(resv) == 1
    assert net in resv

    # Overlapping, __contains__ should return True.
    netA = ipaddress.ip_network(u'10.10.10.0/24')
    assert netA in resv

    # Non-overlapping, __contains__ should return False.
    netB = ipaddress.ip_network(u'192.168.0.0/16')
    assert netB not in resv 
Example #5
Source File: analyzer.py    From Vxscan with Apache License 2.0 6 votes vote down vote up
def get_cidr(iplist):
    cidrs = []
    for ip in iplist:
        cidr = re.sub(r'\d+$', '0/24', ip)
        cidrs.append(ipaddress.ip_network(cidr))

    result = []

    for cidr in cidrs:
        for i in iplist:
            ip = ipaddress.ip_address(i)
            if ip in cidr:
                result.append(str(cidr))
                break
    out = get_top(result)
    for i in out:
        cidr, num = i.split('|')
        print(cidr, num) 
Example #6
Source File: forms.py    From django-payfast with MIT License 6 votes vote down vote up
def is_payfast_ip_address(ip_address_str):
    """
    Return True if ip_address_str matches one of PayFast's server IP addresses.

    Setting: `PAYFAST_IP_ADDRESSES`

    :type ip_address_str: str
    :rtype: bool
    """
    # TODO: Django system check for validity?
    payfast_ip_addresses = getattr(settings, 'PAYFAST_IP_ADDRESSES',
                                   conf.DEFAULT_PAYFAST_IP_ADDRESSES)

    if sys.version_info < (3,):
        # Python 2 usability: Coerce str to unicode, to avoid very common TypeErrors.
        # (On Python 3, this should generally not happen:
        #  let unexpected bytes values fail as expected.)
        ip_address_str = unicode(ip_address_str)  # noqa: F821
        payfast_ip_addresses = [unicode(address) for address in payfast_ip_addresses]  # noqa: F821

    return any(ip_address(ip_address_str) in ip_network(payfast_address)
               for payfast_address in payfast_ip_addresses) 
Example #7
Source File: subnet.py    From open_dnsdb with Apache License 2.0 6 votes vote down vote up
def _validate_args(subnet, region, colo):
    if '/' not in subnet:
        raise BadParam('Invalid subnet.', msg_ch=u'请使用cidr格式的网段')

    _is_valide_region(region)

    if colo not in SubnetIpDal.get_colo_by_group('subnet'):
        raise BadParam('Invalid colo', msg_ch=u'请先配置机房')

    try:
        sub = ipaddress.ip_network(subnet)
    except Exception as e:
        raise BadParam('Invalid subnet: %s' % e, msg_ch=u'错误的网段格式')

    prefix = sub.prefixlen
    if sub.version == 4:
        if prefix < 16 or prefix > 32:
            raise BadParam('Invalid subnet.', msg_ch=u'IPv4掩码长度在[16-32]之间')
    else:
        if prefix < 64 or prefix > 128:
            raise BadParam('Invalid subnet.', msg_ch=u'IPv6掩码长度在[64-128]之间') 
Example #8
Source File: test_ipaddress.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_incompatible_versions(self):
        # These should always raise TypeError
        v4addr = ipaddress.ip_address('1.1.1.1')
        v4net = ipaddress.ip_network('1.1.1.1')
        v6addr = ipaddress.ip_address('::1')
        v6net = ipaddress.ip_network('::1')

        self.assertRaises(TypeError, v4addr.__lt__, v6addr)
        self.assertRaises(TypeError, v4addr.__gt__, v6addr)
        self.assertRaises(TypeError, v4net.__lt__, v6net)
        self.assertRaises(TypeError, v4net.__gt__, v6net)

        self.assertRaises(TypeError, v6addr.__lt__, v4addr)
        self.assertRaises(TypeError, v6addr.__gt__, v4addr)
        self.assertRaises(TypeError, v6net.__lt__, v4net)
        self.assertRaises(TypeError, v6net.__gt__, v4net) 
Example #9
Source File: test_ipaddress.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def testIpFromInt(self):
        self.assertEqual(self.ipv4_interface._ip,
                         ipaddress.IPv4Interface(16909060)._ip)

        ipv4 = ipaddress.ip_network('1.2.3.4')
        ipv6 = ipaddress.ip_network('2001:658:22a:cafe:200:0:0:1')
        self.assertEqual(ipv4, ipaddress.ip_network(int(ipv4.network_address)))
        self.assertEqual(ipv6, ipaddress.ip_network(int(ipv6.network_address)))

        v6_int = 42540616829182469433547762482097946625
        self.assertEqual(self.ipv6_interface._ip,
                         ipaddress.IPv6Interface(v6_int)._ip)

        self.assertEqual(ipaddress.ip_network(self.ipv4_address._ip).version,
                         4)
        self.assertEqual(ipaddress.ip_network(self.ipv6_address._ip).version,
                         6) 
Example #10
Source File: __init__.py    From py-mmdb-encoder with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def insert_network(self, prefix, data_offset, strict = True):
        self.entries_count += 1
        ipnet = ipaddress.ip_network(prefix, strict=False)

        if ipnet.version == 6 and self.ip_version != 6:
            raise Exception('Encoder: insert_network: cannot add IPv6 address in IPv4 table')

        if ipnet.version == 4 and self.ip_version == 6:
            base4in6 = ipaddress.IPv6Address(u'::ffff:0:0')
            v4in6addr = ipaddress.IPv6Address(int(ipnet.network_address)+int(base4in6))

            # Maxmind DBs skips the first 96 bits (do not include the 0xffff)
            if self.compat:
                v4in6addr = ipaddress.IPv6Address(int(ipnet.network_address))

            v4in6addr_plen = ipnet.prefixlen + 96
            ipnet = ipaddress.IPv6Network(u'{}/{}'.format(str(v4in6addr), v4in6addr_plen), strict=False)

        #print(ipnet)
        self.add_to_trie(ipnet, data_offset, strict=strict) 
Example #11
Source File: cidr_validity.py    From drydock with Apache License 2.0 6 votes vote down vote up
def run_validation(self, site_design, orchestrator=None):
        """
        Ensures that the network CIDR provided is acceptable to MAAS which
        means the CIDR should not have host bits set
        """
        network_list = site_design.networks or []

        if not network_list:
            msg = 'No networks found.'
            self.report_warn(
                msg, [],
                'Site design likely incomplete without defined networks')
        else:
            for net in network_list:
                # Verify that the CIDR does not have host bits set
                try:
                    ipaddress.ip_network(net.cidr)
                except ValueError as e:
                    if str(e) == (net.cidr + " has host bits set"):
                        msg = 'The provided CIDR %s has host bits set' % net.cidr
                        valid_cidr = ipaddress.ip_network(net.cidr, strict=False)
                        self.report_error(
                                    msg, [net.doc_ref],
                                    "Provide a CIDR acceptable by MAAS: %s" % str(valid_cidr))
        return 
Example #12
Source File: models.py    From peering-manager with Apache License 2.0 6 votes vote down vote up
def has_potential_ix_peering_sessions(self, internet_exchange=None):
        """
        Returns True if there are potential `InternetExchangePeeringSession` for this
        `AutonomousSystem`. If the `internet_exchange` parameter is given, it will
        only check for potential sessions in the given `InternetExchange`.
        """
        if not self.potential_internet_exchange_peering_sessions:
            return False
        if not internet_exchange:
            return len(self.potential_internet_exchange_peering_sessions) > 0

        for session in self.potential_internet_exchange_peering_sessions:
            for prefix in internet_exchange.get_prefixes():
                if session in ipaddress.ip_network(prefix):
                    return True

        return False 
Example #13
Source File: localsubnet.py    From pscheduler with Apache License 2.0 6 votes vote down vote up
def evaluate(self,
                 hints  # Information used for doing identification
                 ):

        """Given a set of hints, evaluate this identifier and return True if
        an identification is made.

        """


        try:
            ip = ipaddress.ip_network(unicode(hints["requester"]))
        except KeyError:
            return False

        for cidr in self.cidrs:
            if cidr.overlaps(ip):
                return True

        return False



# A short test program 
Example #14
Source File: http.py    From tomodachi with MIT License 6 votes vote down vote up
def get_request_ip(request: Any, context: Optional[Dict] = None) -> Optional[str]:
        if request._cache.get('request_ip'):
            return str(request._cache.get('request_ip', ''))

        if request.transport:
            if not context:
                context = {}
            real_ip_header = context.get('options', {}).get('http', {}).get('real_ip_header', 'X-Forwarded-For')
            real_ip_from = context.get('options', {}).get('http', {}).get('real_ip_from', [])
            if isinstance(real_ip_from, str):
                real_ip_from = [real_ip_from]

            peername = request.transport.get_extra_info('peername')
            request_ip = None
            if peername:
                request_ip, _ = peername
            if real_ip_header and real_ip_from and request.headers.get(real_ip_header) and request_ip and len(real_ip_from):
                if any([ipaddress.ip_address(request_ip) in ipaddress.ip_network(cidr) for cidr in real_ip_from]):
                    request_ip = request.headers.get(real_ip_header).split(',')[0].strip().split(' ')[0].strip()

            request._cache['request_ip'] = request_ip
            return request_ip

        return None 
Example #15
Source File: localif.py    From pscheduler with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 data   # Data suitable for this class
                 ):

        valid, message = data_is_valid(data)
        if not valid:
            raise ValueError("Invalid data: %s" % message)

        self.cidrs = []
        for iface in netifaces.interfaces():
            ifaddrs = netifaces.ifaddresses(iface)
            if netifaces.AF_INET in ifaddrs:
                for ifaddr in ifaddrs[netifaces.AF_INET]:
                    if 'addr' in ifaddr:
                        self.cidrs.append(ipaddress.ip_network(unicode(ifaddr['addr'])))
            if netifaces.AF_INET6 in ifaddrs:
                for ifaddr in ifaddrs[netifaces.AF_INET6]:
                    if 'addr' in ifaddr:
                        #add v6 but remove stuff like %eth0 that gets thrown on end of some addrs
                        self.cidrs.append(ipaddress.ip_network(unicode(ifaddr['addr'].split('%')[0]))) 
Example #16
Source File: ipcymrubogon.py    From pscheduler with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 data   # Data suitable for this class
                 ):

        valid, message = data_is_valid(data)
        if not valid:
            raise ValueError("Invalid data: %s" % message)

        self.exclude = [ ipaddress.ip_network(unicode(addr))
                         for addr in data['exclude'] ]

        try:
            timeout = pscheduler.iso8601_as_timedelta(data['timeout'])
            self.timeout = pscheduler.timedelta_as_seconds(timeout)
        except KeyError:
            self.timeout = 2

        try:
            self.fail_result = data['fail-result']
        except KeyError:
            self.fail_result = False

        self.resolver = dns.resolver.Resolver()
        self.resolver.timeout = self.timeout
        self.resolver.lifetime = self.timeout 
Example #17
Source File: action.py    From insightconnect-plugins with MIT License 6 votes vote down vote up
def _match_whitelist(self, address: str, whitelist: str) -> bool:
        trimmed_address = re.sub(r"/32$", "", address)
        if address in whitelist:
            raise PluginException(
                cause=f"Address Object not created because the host {address} was found in the whitelist as {address}.",
                assistance=f"If you would like to block this host, remove {address} from the whitelist and try again.")
        elif '/' not in trimmed_address:
            pass

        for address_object in whitelist:
            if self._determine_address_type(address_object) == "cidr":
                net = ip_network(address_object, False)
                ip = ip_address(trimmed_address)
                if ip in net:
                    raise PluginException(
                        cause=f"Address Object not created because the host {address}"
                              f" was found in the whitelist as {address_object}.",
                        assistance="If you would like to block this host,"
                              f" remove {address_object} from the whitelist and try again.")

        return False 
Example #18
Source File: nat.py    From pyquarkchain with MIT License 6 votes vote down vote up
def find_internal_ip_on_device_network(upnp_dev: upnpclient.upnp.Device) -> str:
    """
    For a given UPnP device, return the internal IP address of this host machine that can
    be used for a NAT mapping.
    """
    parsed_url = urlparse(upnp_dev.location)
    # Get an ipaddress.IPv4Network instance for the upnp device's network.
    upnp_dev_net = ipaddress.ip_network(parsed_url.hostname + "/24", strict=False)
    for iface in netifaces.interfaces():
        for family, addresses in netifaces.ifaddresses(iface).items():
            # TODO: Support IPv6 addresses as well.
            if family != netifaces.AF_INET:
                continue
            for item in addresses:
                if ipaddress.ip_address(item["addr"]) in upnp_dev_net:
                    return item["addr"]
    raise NoInternalAddressMatchesDevice(device_hostname=parsed_url.hostname) 
Example #19
Source File: tag.py    From ACE with Apache License 2.0 6 votes vote down vote up
def __init__(self, match_type, ignore_case, value, tags):
        assert match_type in [ _tag_mapping.MATCH_TYPE_DEFAULT,
                               _tag_mapping.MATCH_TYPE_GLOB,
                               _tag_mapping.MATCH_TYPE_REGEX,
                               _tag_mapping.MATCH_TYPE_SUBDOMAIN,
                               _tag_mapping.MATCH_TYPE_CIDR ]
        assert isinstance(ignore_case, bool)
        assert value is not None
        assert isinstance(tags, list)
        assert all([isinstance(t, str) for t in tags])

        self.match_type = match_type
        self.ignore_case = ignore_case
        self.value = value
        self.tags = tags

        # if we have a regex go ahead and compile it
        if self.match_type == _tag_mapping.MATCH_TYPE_REGEX:
            self.compiled_regex = re.compile(self.value, flags=re.I if ignore_case else 0)

        # if we have a cidr go ahead and create the object used to match it
        if self.match_type == _tag_mapping.MATCH_TYPE_CIDR:
            self.compiled_cidr = ipaddress.ip_network(value) 
Example #20
Source File: test_http.py    From peering-manager with Apache License 2.0 6 votes vote down vote up
def test_get_prefixes_for_ix_network(self, *_):
        api = PeeringDB()
        ix_network_id = 29146

        with patch(
            "peeringdb.http.requests.get", return_value=MockedResponse(status_code=404)
        ):
            # Must be empty
            self.assertFalse(api.get_prefixes_for_ix_network(0))

        known_prefixes = [
            ipaddress.ip_network("2001:db8:1337::/64"),
            ipaddress.ip_network("203.0.113.0/24"),
        ]
        found_prefixes = []

        for ix_prefix in api.get_prefixes_for_ix_network(ix_network_id):
            self.assertIn(ix_prefix, known_prefixes) 
Example #21
Source File: iscdn.py    From Vxscan with Apache License 2.0 6 votes vote down vote up
def iscdn(host):
    result = True
    # noinspection PyBroadException
    try:
        if not re.search(r'\d+\.\d+\.\d+\.\d+', host):
            host = parse_ip(host)
        for cdn in cdns:
            if ipaddress.ip_address(host) in ipaddress.ip_network(cdn):
                result = False
    except Exception:
        pass

    # noinspection PyBroadException
    try:
        with geoip2.database.Reader('data/GeoLite2-ASN.mmdb') as reader:
            response = reader.asn(host)
            for i in ASNS:
                if response.autonomous_system_number == int(i):
                    result = False
    except Exception:
        pass
    return result 
Example #22
Source File: dns-reverse-generator.py    From NeoNetwork with The Unlicense 5 votes vote down vote up
def iter_route(route_type: str):
    items = []
    for f in ROUTE_FILE.iterdir():
        routes = toml.loads(f.read_text())
        items.extend(
            (entity["name"], ip_network(route).network_address)
            for route, entity in routes.items()
            if entity["type"] == route_type
        )
    return sorted(items, key=lambda item: item[1]) 
Example #23
Source File: roa.py    From NeoNetwork with The Unlicense 5 votes vote down vote up
def route_to_roa(asn_table: dict):
    def make_route():
        for item, entity in iter_toml_file("route"):
            asn = int(item.stem.lstrip("AS"))
            for prefix, fields in entity.items():
                if fields["type"] not in ("loopback", "subnet"):
                    continue
                fields["asn"] = asn
                fields["prefix"] = ip_network(prefix, strict=True)
                supernet = fields.get("supernet")
                fields["supernet"] = (
                    ip_network(supernet, strict=True) if supernet else None
                )
                assert fields["name"]
                assert is_neo_network(fields["prefix"])
                assert not fields["supernet"] or is_neo_network(fields["supernet"])
                yield pick(fields, ["asn", "name", "type", "prefix", "supernet"])

    entities = sorted(make_route(), key=lambda item: item["asn"])
    prefixes = [item["prefix"] for item in entities]
    for net1, net2 in combinations(
        sorted(entities, key=lambda net: net["prefix"].prefixlen), 2
    ):
        if not net1["prefix"].overlaps(net2["prefix"]):
            continue
        assert net1["prefix"] != net2["prefix"]
        assert net1["prefix"].supernet_of(net2["prefix"])
        s1net, s2net = (net1["supernet"], net2["supernet"])
        assert s2net  # please include supernet = <cidr> in your route
        # if net1(the bigger net) has a supernet s1net, then s1net and net1
        # will be checked or must have been checked, same for net2
        assert not s1net or s1net in prefixes  # net1.supernet is garbage
        assert s2net == net1["prefix"] or s2net in prefixes  # net2.supernet is garbage
    return entities 
Example #24
Source File: factory.py    From network_tech with Apache License 2.0 5 votes vote down vote up
def _clean_ipv4_prefix(prefix):
    ip, prefix_length = prefix.split('/')
    octets = ip.split('.')
    for _ in (range(4 - len(octets))):
        octets.append('0')
    octets = [str(int(i)) for i in octets]
    return ipaddress.ip_network('{}/{}'.format(
            '.'.join(octets),
            prefix_length
        )) 
Example #25
Source File: factory.py    From network_tech with Apache License 2.0 5 votes vote down vote up
def ipv6(cls, content):
        tree = _strip_namespaces(content)
        reservations = list()
        for record in tree.findall("./record"):
            instance = Ipv6Record()
            for item in record.iter():
                if hasattr(instance, item.tag):
                    value = item.text
                    if item.tag == 'prefix':
                        value = ipaddress.ip_network(value)
                    setattr(instance, item.tag, value)                    
            reservations.append(instance)
        return reservations 
Example #26
Source File: ripe_loader.py    From IPASN-History with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_all(self):
        set_running(self.__class__.__name__)
        for path in sorted(self.storage_root.glob('**/*.gz'), reverse=True):
            if shutdown_requested():
                break
            date_str = re.findall('.*/bview.(.*).gz', str(path))[0]
            date = datetime.strptime(date_str, '%Y%m%d.%H%M').isoformat()

            oldest_to_load = self.cache.hget('META:expected_interval', 'first')
            if oldest_to_load and oldest_to_load > date:
                # The RIPE dump we're trying to load is older than the oldest date we want to cache, skipping.
                continue

            if self.already_loaded(date):
                self.logger.debug(f'Already loaded {path}')
                continue
            self.logger.info(f'Loading {path}')
            routes = routeview(path)
            self.logger.debug('Content loaded')
            for address_family, entries in routes.items():
                to_import = defaultdict(lambda: {address_family: set(), 'ipcount': 0})
                for prefix, asn in entries:
                    network = ip_network(prefix)
                    to_import[asn][address_family].add(str(network))
                    to_import[asn]['ipcount'] += network.num_addresses
                p = self.storagedb.pipeline()
                p.sadd(f'{self.key_prefix}|{address_family}|dates', date)
                p.sadd(f'{self.key_prefix}|{address_family}|{date}|asns', *to_import.keys())  # Store all ASNs
                for asn, data in to_import.items():
                    p.sadd(f'{self.key_prefix}|{address_family}|{date}|{asn}', *data[address_family])  # Store all prefixes
                    p.set(f'{self.key_prefix}|{address_family}|{date}|{asn}|ipcount', data['ipcount'])  # Total IPs for the AS
                self.logger.debug('All keys ready')
                p.execute()
                self.update_last(address_family, date)
            self.logger.info(f'Done with {path}')

            self.logger.debug('Done.')
        unset_running(self.__class__.__name__) 
Example #27
Source File: utils.py    From open_dnsdb with Apache License 2.0 5 votes vote down vote up
def format_subnet(subnet):
    try:
        subnet_obj = ipaddress.ip_network(subnet)
    except:
        raise BadParam('invalid subnet: %s' % subnet, msg_ch='网段格式错误: %s' % subnet)
    start_ip = subnet_obj.network_address
    end_ip = subnet_obj.broadcast_address
    is_ipv6 = (subnet_obj.version == 6)
    return str(subnet_obj), is_ipv6, float(int(start_ip)), float(int(end_ip)) 
Example #28
Source File: utils.py    From Chimay-Red with MIT License 5 votes vote down vote up
def check_cidr_overlap(address1: str, address2: str) -> bool:
    """

    :param address1:
    :param address2:
    :return:
    """

    return ipaddress.ip_address(address1) in ipaddress.ip_network(address2) 
Example #29
Source File: subnet_ip.py    From open_dnsdb with Apache License 2.0 5 votes vote down vote up
def add_subnet(subnet, region, colo, comment, username):
        subnet = ipaddress.ip_network(subnet)
        intranet = subnet.is_private
        net_id = subnet.network_address
        broadcast_ip = subnet.broadcast_address
        is_ipv6 = (subnet.version == 6)
        ips_dict_list = []
        for i in subnet:
            if i == net_id or i == broadcast_ip:
                continue
            ips_dict_list.append({
                'region': region,
                'fixed_ip': str(i),
                'is_ipv6': is_ipv6
            })
        if Subnets.query.filter_by(region_name=region).first():
            raise BadParam('region already exist', msg_ch='网段名已存在')
        try:
            with db.session.begin(subtransactions=True):
                subnet_item = Subnets(
                    region_name=region,
                    subnet=str(subnet),
                    create_user=username,
                    intranet=intranet,
                    colo=colo,
                    is_ipv6=is_ipv6
                )
                if comment:
                    subnet_item.comment = comment
                db.session.add(subnet_item)
                db.session.bulk_insert_mappings(IpPool, ips_dict_list)
        except Exception:
            raise BadParam('Ip conflict with other regions', msg_ch=u'和已有的网段有交叉,请检查后重试') 
Example #30
Source File: caida_loader.py    From IPASN-History with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_all(self):
        set_running(self.__class__.__name__)
        for path in sorted(self.storage_root.glob('**/*.gz'), reverse=True):
            if shutdown_requested():
                break
            address_family, year, month, date_str = re.findall('.*/(.*)/(.*)/(.*)/routeviews-rv[2,6]-(.*).pfx2as.gz', str(path))[0]
            date = parse(date_str).isoformat()

            oldest_to_load = self.cache.hget('META:expected_interval', 'first')
            if oldest_to_load and oldest_to_load > date:
                # The CAIDA dump we're trying to load is older than the oldest date we want to cache, skipping.
                continue

            if self.already_loaded(address_family, date):
                self.logger.debug(f'Already loaded {path}')
                continue
            self.logger.info(f'Loading {path}')
            to_import = defaultdict(lambda: {address_family: set(), 'ipcount': 0})
            with gzip.open(path) as f:
                for line in f:
                    prefix, length, asns = line.decode().strip().split('\t')
                    # The meaning of AS set and multi-origin AS in unclear. Taking the first ASN in the list only.
                    asn = re.split('[,_]', asns)[0]
                    network = ip_network(f'{prefix}/{length}')
                    to_import[asn][address_family].add(str(network))
                    to_import[asn]['ipcount'] += network.num_addresses

            self.logger.debug('Content loaded')
            p = self.storagedb.pipeline()
            p.sadd(f'{self.key_prefix}|{address_family}|dates', date)
            p.sadd(f'{self.key_prefix}|{address_family}|{date}|asns', *to_import.keys())  # Store all ASNs
            for asn, data in to_import.items():
                p.sadd(f'{self.key_prefix}|{address_family}|{date}|{asn}', *data[address_family])  # Store all prefixes
                p.set(f'{self.key_prefix}|{address_family}|{date}|{asn}|ipcount', data['ipcount'])  # Total IPs for the AS
            self.logger.debug('All keys ready')
            p.execute()
            self.update_last(address_family, date)
            self.logger.debug('Done.')
        unset_running(self.__class__.__name__)