Python ipaddress.collapse_addresses() Examples
The following are 2
code examples of ipaddress.collapse_addresses().
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: vpn.py From telepresence with Apache License 2.0 | 6 votes |
def covering_cidr(ips: List[str]) -> str: """ Given list of IPs, return CIDR that covers them all. Presumes it's at least a /24. """ def collapse(ns): return list(ipaddress.collapse_addresses(ns)) assert len(ips) > 0 networks = collapse([ ipaddress.IPv4Interface(ip + "/24").network for ip in ips ]) # Increase network size until it combines everything: while len(networks) > 1: networks = collapse([networks[0].supernet()] + networks[1:]) return networks[0].with_prefixlen # Script to dump resolved IPs to stdout as JSON list:
Example #2
Source File: networkutils.py From ceph-lcm with Apache License 2.0 | 5 votes |
def spanning_network(networks): if not networks: raise ValueError("List of networks is empty") if len(networks) == 1: return networks[0] sorter = operator.attrgetter("num_addresses") while True: networks = sorted( ipaddress.collapse_addresses(networks), key=sorter, reverse=True) if len(networks) == 1: return networks[0] networks[-1] = networks[-1].supernet()