Gregory
Gregory

Reputation: 493

Module, script or algorithm to convert IP range into a CIDR notation

How can I convert many IP ranges into a CIDR notation? All I found is the ip2cidr service, but I have >200K IP-records.

Upvotes: 3

Views: 3971

Answers (2)

coanor
coanor

Reputation: 3944

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))

Upvotes: 0

Related Questions