本文整理汇总了Python中netaddr.IPNetwork.supernet方法的典型用法代码示例。如果您正苦于以下问题:Python IPNetwork.supernet方法的具体用法?Python IPNetwork.supernet怎么用?Python IPNetwork.supernet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netaddr.IPNetwork
的用法示例。
在下文中一共展示了IPNetwork.supernet方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: neighbors_using_ip
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork import supernet [as 别名]
def neighbors_using_ip():
if g.edge.ip == None:
return []
edge_ip = IPNetwork(g.edge.ip)
superblock = str(edge_ip.supernet(32 - BLOCK_SIZE)[0])
ip_neighbors = Edge.query.filter(Edge.objects.contains(obj), Edge.id != g.edge.id, Edge.ip.op("<<")(superblock))
knn = []
knn_cache = set()
tree = dict([(neighbor, IPNetwork(neighbor.ip).supernet(32 - BLOCK_SIZE)) for neighbor in ip_neighbors])
for distance in reversed(xrange(BLOCK_SIZE)):
for neighbor, subnet in tree.iteritems():
if len(knn) < IP_PEER_MAX and neighbor not in knn_cache and edge_ip in subnet[distance]:
knn.append(neighbor)
knn_cache.add(neighbor)
if len(knn) == IP_PEER_MAX:
break
return [{'ip': n.ip,
'public_key': n.public_key,
'connect_with': 'network'} for n in knn]
示例2: assign_management_interfaces
# 需要导入模块: from netaddr import IPNetwork [as 别名]
# 或者: from netaddr.IPNetwork import supernet [as 别名]
def assign_management_interfaces(self):
g_phy = self.anm['phy']
lab_topology = self.nidb.topology[self.host]
oob_management_ips = {}
#TODO: make this seperate function
from netaddr import IPNetwork, IPRange
mgmt_address_start = g_phy.data.mgmt_address_start
mgmt_address_end = g_phy.data.mgmt_address_end
mgmt_prefixlen = int(g_phy.data.mgmt_prefixlen)
#TODO: need to check if range is insufficient
mgmt_ips = (IPRange(mgmt_address_start, mgmt_address_end))
mgmt_ips_iter = iter(mgmt_ips) # to iterate over
mgmt_address_start_network = IPNetwork(mgmt_address_start) # as /32 for supernet
mgmt_address_end_network = IPNetwork(mgmt_address_end) # as /32 for supernet
# retrieve the first supernet, as this is the range requested. subsequent are the subnets
start_subnet = mgmt_address_start_network.supernet(mgmt_prefixlen)[0] # retrieve first
end_subnet = mgmt_address_end_network.supernet(mgmt_prefixlen)[0] # retrieve first
try: # validation
assert(start_subnet == end_subnet)
log.debug("Verified: Cisco management subnets match")
except AssertionError:
log.warning("Error: Cisco management subnets do not match: %s and %s, using start subnet"
% (start_subnet, end_subnet))
mgmt_subnet = start_subnet
hosts_to_allocate = sorted(self.nidb.nodes('is_router', host=self.host))
dhcp_subtypes = {"os"}
dhcp_hosts = [h for h in hosts_to_allocate if h.device_subtype in dhcp_subtypes]
non_dhcp_hosts = [h for h in hosts_to_allocate if h.device_subtype not in dhcp_subtypes]
try: # validation
assert(len(mgmt_ips) >= len(non_dhcp_hosts))
log.debug("Verified: Cisco management IP range is sufficient size %s for %s hosts"
% (len(mgmt_ips), len(non_dhcp_hosts)))
except AssertionError:
log.warning("Error: Cisco management IP range is insufficient size %s for %s hosts"
% (len(mgmt_ips), len(non_dhcp_hosts)))
# TODO: need to use default range
return
for nidb_node in hosts_to_allocate:
for interface in nidb_node.physical_interfaces:
if interface.management:
interface.description = "OOB Management"
interface.physical = True
if nidb_node in dhcp_hosts:
interface.use_dhcp = True
oob_management_ips[str(nidb_node)] = "dhcp"
else:
ipv4_address = mgmt_ips_iter.next()
interface.ipv4_address = ipv4_address
interface.ipv4_subnet = mgmt_subnet
interface.ipv4_cidr = sn_preflen_to_network(ipv4_address, mgmt_prefixlen)
oob_management_ips[str(nidb_node)] = ipv4_address
lab_topology.oob_management_ips = oob_management_ips