paragbaxi paragbaxi -5 years ago 366
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)

Answer Source

You can do it in one line using netaddr:

cidrs = netaddr.ip_range_to_cidrs(ip_start, ip_end)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download