Chris Hall Chris Hall - 1 year ago 263
Python Question

IP Range to CIDR conversion in Python?

How can I get a CIDR notation representing a range of IP addresses, given the start and end IP addresses of the range, in Python? I can find CIDR to IP Range but cannot find any code for the reverse.

Example of the desired output:

startip = ''
endip = ''

return ''

Answer Source

You may use iprange_to_cidrs provided by netaddr module. Example:

cidrs = netaddr.iprange_to_cidrs(startip, endip)

Here are the official docs:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download