当前位置: 首页>>代码示例>>Python>>正文


Python netaddr.cidr_merge函数代码示例

本文整理汇总了Python中netaddr.cidr_merge函数的典型用法代码示例。如果您正苦于以下问题:Python cidr_merge函数的具体用法?Python cidr_merge怎么用?Python cidr_merge使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了cidr_merge函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: connect_line_bridge

def connect_line_bridge(line_component, bridge_component, addressing_scheme=None):
    """ Connects a ring to a bridge.

    :param line_component: ring component that will be connected to the bridge. Needs at least two free interfaces
    :param bridge_component: bridge component that will be connected to the ring. Will add new interfaces to the bridge.
    """
    link_id = used_resources.get_new_link_id()

    line_container = line_component.connection_points.pop()

    # create new interface for container in line
    line_interface_id = "%s.%03d" % (line_container.container_id, line_container.get_next_interface() )
    line_interface = NetworkInterface(line_interface_id, link_id)
    if addressing_scheme is not None:
        ip = addressing_scheme['bridge_links'].pop()
        line_interface.address = "%s/%s" % (ip, addressing_scheme['bridge_prefix'])

    # get bridge and interface for bridge
    br = bridge_component.connection_points[0]
    bridge_interface_id = "%s.%03d" % (br.bridge_id, br.get_next_interface())

    bridge_interface = NetworkInterface(bridge_interface_id, link_id)

    # summary
    if addressing_scheme is not None:
        containers = len(line_component.topology['containers'])
        print("amount of containers: %s " % containers)
        other_container = None
        if len(line_component.connection_points) > 0:
            other_container = line_component.connection_points[0]
            line_component.addresses = sorted(line_component.addresses)

        if other_container is not None and netaddr.IPNetwork(line_container.interfaces[0].address) > netaddr.IPNetwork(
                other_container.interfaces[0].address):
            summary = netaddr.cidr_merge(line_component.addresses[(containers - 1):len(line_component.addresses)])
            line_component.addresses = line_component.addresses[0: containers - 2]
            print("2: ", )
            for address in line_component.addresses:
                print(address, )
        else:
            summary = netaddr.cidr_merge(line_component.addresses[0:containers - 2])
            line_component.addresses = line_component.addresses[containers - 1:len(line_component.addresses)]

        line_interface.summarizes = summary

    print("link %s has %s and %s" % (link_id, line_interface.interface_id, bridge_interface.interface_id))

    br.add_interface(bridge_interface)
    line_container.add_interface(line_interface)
    line_container.gateway = line_interface.interface_id
开发者ID:ahluntang,项目名称:lscale,代码行数:50,代码来源:generate.py

示例2: main

def main():

	parser = argparse.ArgumentParser(description='IP Blacklist Generator')

	parser.add_argument('-if', type=lambda x: open_file(parser, x), dest='input_file', metavar='INPUT_FILE', required=True, help='Input file with blacklist URLs')
	parser.add_argument('-of', type=str, dest='output_file', metavar='OUTPUT_FILE', required=True, help='Output file')

	args = parser.parse_args()

	output_filename = args.output_file
	input_file = args.input_file


	#Create master list that will ultimately get written to file
	master_list = []

	#Read in URLs from file and build list
	urls = get_urls(input_file)

	#Download lists, parse, and append to master list
	for url in urls:
		raw_list = download_list(url)
		if raw_list is not None:
			formatted_list = parse_list(raw_list)
			for address_block in formatted_list:
				master_list.append(address_block)

	#aggregate and remove duplicates
	master_list = cidr_merge(master_list)

	#sort the list of IP objects
	master_list.sort()

	#Export list to file
	export_list(master_list, output_filename)
开发者ID:jpeg747,项目名称:blacklist_builder,代码行数:35,代码来源:blacklist_builder.py

示例3: set_summaries

def set_summaries(configured_host):
    logging.getLogger(__name__).info("Setting summaries")
    for interface_id, interface in configured_host['interfaces'].items():
        container_id = configured_host['mappings'][interface_id]
        container = configured_host['containers'][container_id]
        if interface.address is not None:
            logging.getLogger(__name__).info(interface_id)
            for summary, via in configured_host["mappings_summaries"].items():
                #print " checking for:  %s, via %s" % (summary, via)

                # via must be in same subnet as address in this interface
                same_subnet_via = (netaddr.IPNetwork(via) == netaddr.IPNetwork(interface.address))

                # check if summary is in same subnet or not
                existing_summaries = []
                for route in container.routing['routes']:
                    existing_summaries.append(netaddr.IPNetwork(route.address))
                existing_summaries = netaddr.cidr_merge(existing_summaries)
                route_exists = False
                for route in existing_summaries:
                    if route.network == netaddr.IPNetwork(summary).network:
                        route_exists = True

                if same_subnet_via and not route_exists:
                    network = netaddr.IPNetwork(summary)
                    address = "%s/%s" % (network.network, network.prefixlen)
                    summary_route = emulator.elements.Route(address, interface.veth)
                    summary_route.via = str(netaddr.IPNetwork(via).ip)
                    container.routing["routes"].append(summary_route)
                    logging.getLogger(__name__).info("Adding %s to %s", summary_route.address, container.container_id)
开发者ID:ahluntang,项目名称:lscale,代码行数:30,代码来源:topology_parser.py

示例4: test_ip_range

def test_ip_range():
    ip_list = list(iter_iprange('192.0.2.1', '192.0.2.14'))

    assert len(ip_list) == 14

    assert ip_list == [
        IPAddress('192.0.2.1'),
        IPAddress('192.0.2.2'),
        IPAddress('192.0.2.3'),
        IPAddress('192.0.2.4'),
        IPAddress('192.0.2.5'),
        IPAddress('192.0.2.6'),
        IPAddress('192.0.2.7'),
        IPAddress('192.0.2.8'),
        IPAddress('192.0.2.9'),
        IPAddress('192.0.2.10'),
        IPAddress('192.0.2.11'),
        IPAddress('192.0.2.12'),
        IPAddress('192.0.2.13'),
        IPAddress('192.0.2.14'),
    ]

    assert cidr_merge(ip_list) == [
        IPNetwork('192.0.2.1/32'),
        IPNetwork('192.0.2.2/31'),
        IPNetwork('192.0.2.4/30'),
        IPNetwork('192.0.2.8/30'),
        IPNetwork('192.0.2.12/31'),
        IPNetwork('192.0.2.14/32'),
    ]
开发者ID:drkjam,项目名称:netaddr,代码行数:30,代码来源:test_ip_ranges.py

示例5: ip_range_diff

def ip_range_diff(source_ip_range, remove_ip_range):
    """Return source_ip_range after excluding remove_ip_range."""
    # Convert IP ranges to CIDR.
    source_ip_range = ip_range_to_cidr(source_ip_range)
    remove_ip_range = ip_range_to_cidr(remove_ip_range)
    logging.debug('source_ip_range = %s' % (source_ip_range))
    logging.debug('remove_ip_range = %s' % (remove_ip_range))
    # Expand each range.
    source_ip_range_expanded = ip_range_expand(source_ip_range)
    remove_ip_range_expanded = ip_range_expand(remove_ip_range)
#    logging.debug('remove_ip_range_expanded = %s' % (remove_ip_range_expanded))
    # Remove each matching source IP address individually.
    for i in remove_ip_range_expanded:
        try:
            source_ip_range_expanded.remove(i)
        except ValueError:
            # Value not in source_ip_range
            continue
    # Convert remaining range to CIDR.
#    logging.debug('source_ip_range_expanded = %s' % (source_ip_range_expanded))
    source_ip_range = netaddr.cidr_merge(source_ip_range_expanded)
    logging.debug('source_ip_range = %s' % (source_ip_range))
    # Convert each CIDR block to string.
    result_cidr = []
    for cidr_object in source_ip_range:
        result_cidr.append(str(cidr_object))
    # Convert entire list to a string.
    result_cidr = ','.join(result_cidr)
    logging.debug('result_cidr = %s' % (result_cidr))
    # Remove '/32' (single IP) and return diff'd range.
    return result_cidr.replace('/32', '')
开发者ID:paragbaxi,项目名称:qualysguard_vm_scan_dead_hosts,代码行数:31,代码来源:qualysguard_vm_scan_dead_hosts.py

示例6: ip_range_to_cidr

def ip_range_to_cidr(ip_network_string):
    """Convert ip_network_string into CIDR notation."""
    # Split string into list by ', ' delimiter.
    ip_network_cidr = []
    ip_network_list = ip_network_string.split(',')
    for ip_object in ip_network_list:
        # For every ip range ('10.182.71.0-10.182.75.255'), convert to individual slash notation, 10.182.71.0/24, 10.182.72.0/22.
        if '-' in ip_object:
            # The object is a range.
            dash = ip_object.find('-')
            # First part of ip range.
            ip_start = ip_object[:dash]
            # Last part of ip range.
            ip_end = ip_object[dash + 1:]
            # Generate lists of IP addresses in range.
            ip_range = list(netaddr.iter_iprange(ip_start, ip_end))
            # Convert start & finish range to CIDR.
            ip_range = netaddr.cidr_merge(ip_range)
            # May be one or more objects in list.
            # Example 1:  '10.182.71.0-10.182.75.255' ==> ['10.182.71.0/24, 10.182.72.0/22']
            # Example 2:  '10.182.90.0-10.182.91.255' ==> ['10.182.90.0/23']
            # Add each CIDR to ip_network.
            for ip_object in ip_range:
                 ip_network_cidr.append(str(ip_object))
        else:
            # The object is not a range, just add it.
            logging.debug('ip_object = %s' % (ip_object))
            ip_network_cidr.append(str(netaddr.IPNetwork(ip_object).cidr))
    # Return as a string with delimiter ', '
    return ip_network_cidr
开发者ID:paragbaxi,项目名称:qualysguard_vm_scan_dead_hosts,代码行数:30,代码来源:qualysguard_vm_scan_dead_hosts.py

示例7: report

def report(domains, asns=None, networks=None, addresses=None, rdns=None):
    """
    Prints the sets of given domains, autonomous system numbers, networks, PTRs, and IP addresses if user wants it.
    Args:
        domains: set of domains gathered.
        asns: set of autonomous system numbers gathered.
        networks: set of network ranges gathered.
        addresses: set of IP addresses gathered.
        rdns: set of PTR records
    """
    if domains is not None:
        print_border("DOMAINS ({})".format(len(domains)))
        print("{}".format("\n".join(str(x) for x in domains)))

    if asns is not None:
        print_border("AUTONOMOUS SYSTEM NUMBERS ({})".format(len(asns)))
        print(*asns, sep="\n")

    if networks is not None:
        networks = netaddr.cidr_merge(list(networks))
        print_border("NETWORK RANGES ({})".format(len(networks)))
        print(*networks, sep="\n")

    if addresses is not None:
        print_border("IP ADDRESSES ({})".format(len(addresses)))
        print(*addresses, sep="\n")

    if rdns is not None:
        print_border("RDNS RECORDS ({})".format(len(rdns)))
        print(*rdns, sep="\n")
开发者ID:jstnkndy,项目名称:scripts,代码行数:30,代码来源:autorecon.py

示例8: iana_rir_gen_ip_list

def iana_rir_gen_ip_list(user_rir_list):

    # generates a list of networks that can be blocked by RIR

    # we use a SortedList so that elements are inserted in order. This allows cidr_merge to work
    rir_slash_eight_list = SortedList()

    with open('iana') as iana_file:

        iana_csv = csv.reader(iana_file)

        for line in iana_csv:

            for rir in user_rir_list:

                # case in which the whois line from our csv contains the RIR
                if rir in line[3]:

                    network = line[0].lstrip('0')
                    rir_slash_eight_list.add(netaddr.IPNetwork(network))

                    # if we find a match, there is no reason to see if the other RIRs are on the same line
                    break

        # run cidr_merge to summarize
        rir_slash_eight_list = netaddr.cidr_merge(rir_slash_eight_list)

    return rir_slash_eight_list
开发者ID:bjames,项目名称:geoblock,代码行数:28,代码来源:geoblock.py

示例9: manual_ipv6_infrastructure_allocation

def manual_ipv6_infrastructure_allocation(anm):
    """Applies manual IPv6 allocation"""

    import netaddr

    g_ipv6 = anm["ipv6"]
    log.info("Using specified IPv6 infrastructure allocation")

    for node in g_ipv6.l3devices():
        for interface in node.physical_interfaces:
            if not interface["input"].is_bound:
                continue  # unbound interface
            ip_address = netaddr.IPAddress(interface["input"].ipv6_address)
            prefixlen = interface["input"].ipv6_prefixlen
            interface.ip_address = ip_address
            interface.prefixlen = prefixlen
            cidr_string = "%s/%s" % (ip_address, prefixlen)
            interface.subnet = netaddr.IPNetwork(cidr_string)

    broadcast_domains = [d for d in g_ipv6 if d.broadcast_domain]

    # TODO: allow this to work with specified ip_address/subnet as well as ip_address/prefixlen

    from netaddr import IPNetwork

    for coll_dom in broadcast_domains:
        connected_interfaces = [edge.dst_int for edge in coll_dom.edges()]
        cd_subnets = [IPNetwork("%s/%s" % (i.subnet.network, i.prefixlen)) for i in connected_interfaces]

        if len(cd_subnets) == 0:
            log.warning("Collision domain %s is not connected to any nodes" % coll_dom)
            continue

        try:
            assert len(set(cd_subnets)) == 1
        except AssertionError:
            mismatch_subnets = "; ".join("%s: %s/%s" % (i, i.subnet.network, i.prefixlen) for i in connected_interfaces)
            log.warning("Non matching subnets from collision domain %s: %s" % (coll_dom, mismatch_subnets))
        else:
            coll_dom.subnet = cd_subnets[0]  # take first entry

        # apply to remote interfaces

        for edge in coll_dom.edges():
            edge.dst_int.subnet = coll_dom.subnet

    # also need to form aggregated IP blocks (used for e.g. routing prefix
    # advertisement)
    # import autonetkit
    # autonetkit.update_http(anm)

    infra_blocks = {}
    for (asn, devices) in g_ipv6.groupby("asn").items():
        broadcast_domains = [d for d in devices if d.broadcast_domain]
        subnets = [cd.subnet for cd in broadcast_domains if cd.subnet is not None]  # only if subnet is set
        infra_blocks[asn] = netaddr.cidr_merge(subnets)

    g_ipv6.data.infra_blocks = infra_blocks
开发者ID:rackbone,项目名称:autonetkit,代码行数:58,代码来源:ip.py

示例10: read_rirs

def read_rirs(country_list, permit, rir_list=RIR_NAMES):

    # list containing our file objects
    file_list = []

    # we use a SortedList so that elements are inserted in order. This allows cidr_merge to work
    rir_ips = SortedList()

    # Open the files we downloaded earlier and store the file object
    for rir in rir_list:
        file_list.append(open(rir))

    for f in file_list:

        for line in f:

            curr_line = line.split('|')

            try:

                # we want only the ipv4 lines that are for a specific country
                # also only want countries that we are going to block
                if (curr_line[2] == "ipv4" and curr_line[1] != "*") and \
                    ((permit and curr_line[1] not in country_list) or
                     (not permit and curr_line[1] in country_list)):

                    country_code = curr_line[1]
                    network_id = curr_line[3]
                    wildcard = int(curr_line[4])-1

                    try:

                        # Add network to list, if the number of IPs was not a
                        # power of 2 (wildcard is not valid).
                        # AddrFormatError is thrown
                        rir_ips.add(netaddr.IPNetwork(network_id + "/" + str(netaddr.IPAddress(wildcard))))

                    # Handle case in where our mask is invalid by rounding DOWN
                    except netaddr.AddrFormatError:

                        print "rounded network " + network_id + " with " + str(wildcard) + \
                              " hosts up to nearest power of 2"
                        wildcard = next_power_of_2(wildcard) - 1
                        print wildcard + 1
                        rir_ips.add(netaddr.IPNetwork(network_id + "/" + str(netaddr.IPAddress(wildcard))))

            # IndexErrors only occur when parsing columns we don't need
            except IndexError:

                pass

        f.close()

    # cidr_merge takes our list of IPs and summarizes subnets where possible
    # this greatly decreases the number of ACL entries
    rir_ips = netaddr.cidr_merge(rir_ips)

    return rir_ips
开发者ID:bjames,项目名称:geoblock,代码行数:58,代码来源:geoblock.py

示例11: _setup_routes

 def _setup_routes(self, if_dev, ver, ip_start, ip_end, gw_ip):
     gw_ip_net = netaddr.IPNetwork(gw_ip)
     if_dev.addr.add(ver, "%s/%s" % (ip_start, gw_ip_net.prefixlen), gw_ip_net.broadcast)
     if_dev.route.add_gateway(str(gw_ip_net.ip))
     if ip_start != ip_end:
         local_nets = netaddr.cidr_merge(netaddr.iter_iprange(ip_start, ip_end))
         max_pfx_len = ver == 4 and 32 or 128
         for l in local_nets:
             if l.prefixlen < max_pfx_len or str(l.ip) != ip_start:
                 if_dev.route._as_root("add", "local", str(l), "dev", if_dev.name, options=[ver])
开发者ID:kendriu,项目名称:python-opflex-agent,代码行数:10,代码来源:snat_iptables_manager.py

示例12: manual_ipv4_allocation

def manual_ipv4_allocation(anm):
    """Applies manual IPv4 allocation"""
    import netaddr
    g_in_directed = anm['input_directed']
    g_ipv4 = anm['ipv4']

    for l3_device in g_ipv4.nodes("is_l3device"):
        directed_node = g_in_directed.node(l3_device)
        l3_device.loopback = directed_node.ipv4loopback
        for edge in l3_device.edges():
            # find edge in g_in_directed
            directed_edge = g_in_directed.edge(edge)
            edge.ip_address = netaddr.IPAddress(directed_edge.ipv4)

            # set subnet onto collision domain (can come from either
            # direction)
            collision_domain = edge.dst
            if not collision_domain.subnet:
                # TODO: see if direct method in netaddr to deduce network
                prefixlen = directed_edge.netPrefixLenV4
                cidr_string = "%s/%s" % (edge.ip_address, prefixlen)

                intermediate_subnet = netaddr.IPNetwork(cidr_string)
                cidr_string = "%s/%s" % (
                    intermediate_subnet.network, prefixlen)
                subnet = netaddr.IPNetwork(cidr_string)
                collision_domain.subnet = subnet

    # also need to form aggregated IP blocks (used for e.g. routing prefix
    # advertisement)
    loopback_blocks = {}
    infra_blocks = {}
    for asn, devices in g_ipv4.groupby("asn").items():
        routers = [d for d in devices if d.is_router]
        loopbacks = [r.loopback for r in routers]
        loopback_blocks[asn] = netaddr.cidr_merge(loopbacks)

        collision_domains = [d for d in devices if d.collision_domain]
        subnets = [cd.subnet for cd in collision_domains]
        infra_blocks[asn] = netaddr.cidr_merge(subnets)

    g_ipv4.data.loopback_blocks = loopback_blocks
    g_ipv4.data.infra_blocks = infra_blocks
开发者ID:Rom2BE,项目名称:autonetkit,代码行数:43,代码来源:build_network.py

示例13: cidr_subtract

def cidr_subtract(supernet_cidr, subnet_cidr):
    supernet = netaddr.IPNetwork(supernet_cidr)
    subnet = netaddr.IPNetwork(subnet_cidr)

    if subnet not in supernet:
        return [supernet]

    supernet_subnets = list(supernet.subnet(subnet.prefixlen))
    supernet_subnets.remove(subnet)

    return netaddr.cidr_merge(supernet_subnets)
开发者ID:devonbleak,项目名称:arm-vnet-generator,代码行数:11,代码来源:cidr_subtract.py

示例14: _validate_range_with_network

 def _validate_range_with_network(self):
     ip_networks = []
     if self.range:
         start, end = self.range.split('-')
         ip_addresses = list(netapi.iter_iprange(start, end))
         ip_networks.extend(netapi.cidr_merge(ip_addresses))
     else:
         ip_addresses = get_ips_from_cidr(self.cidr)
         ip_networks.extend(netapi.cidr_merge(ip_addresses))
     address_lst = []
     net = netapi.IPNetwork(self.cidr)
     for network in ip_networks:
         for address in network:
             address_lst.append(str(address))
             if address not in net:
                 excep_msg = 'given dhcp range:%s not a subset of network:%s' % (self.range, net)
                 LOGGER.error(excep_msg)
                 raise Exception(excep_msg)
                 
     self.iplist = address_lst
开发者ID:smarkm,项目名称:ovm,代码行数:20,代码来源:IP.py

示例15: main

def main():
	if len(sys.argv) < 2:
		usage()
		sys.exit()
	
	ipFile = open(sys.argv[1])
	ipAddresses = [i for i in ipFile.readlines()]
	ipAddresses = sorted(ipAddresses)
	cidrs = netaddr.cidr_merge(ipAddresses)
	for cidr in cidrs:
		print cidr
开发者ID:jstnkndy,项目名称:scripts,代码行数:11,代码来源:ipListToCidr.py


注:本文中的netaddr.cidr_merge函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。