Say I have two IPs: startip = '184.108.40.206' and endip = '220.127.116.11'
I understand that in python netaddr.iprange_to_cidrs(startip, endip) will give you a list of CIDR subnets that fit exactly between the boundaries. However, I was hoping to go for one CIDR subnet that covers it all even if it gives a few more IPs.
I prefer a function that does it, but I would also welcome any math/logic to calculate it and eventually turn it into code.
Have you tried the ipaddress module? It comes with Python 3, but is installable for Python 2.
startip = ipaddress.IPv4Address('18.104.22.168')
endip = ipaddress.IPv4Address('22.214.171.124')
# summarize_address_range produces a generator, so force the results
networks = [n for n in ipaddress.summarize_address_range(startip, endip)]