paragbaxi paragbaxi - 10 months ago 103
Python Question

List of IP addresses in Python to a list of CIDR

How do I convert a list of IP addresses to a list of CIDRs? Google's ipaddr-py library has a method called summarize_address_range(first, last) that converts two IP addresses (start & finish) to a CIDR list. However, it cannot handle a list of IP addresses.

>>> list_of_ips = ['', '', '', '', '']
>>> convert_to_cidr(list_of_ips)


You can do it in one line using netaddr:

cidrs = netaddr.ip_range_to_cidrs(ip_start, ip_end)