Module, script or algorithm for converting IP range to CIDR notation

How to convert many IP ranges to CIDR notation? All I found is ip2cidr service , but I have> 200K IP entries.

+3
source share
2 answers

Using netaddr:

from netaddr import *
ips = ['192.168.37.111', '192.168.37.112']
all_ips = list(iter_iprange(ips[0], ips[1]))
ip_address = cidr_merge(all_ips)
cidr = []
for ip in ip_address:
    cidr.append(str(ip.cidr))
0
source

All Articles