Say I have two IPs: startip = '18.104.22.168' and endip = '22.214.171.124'
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('126.96.36.199')
endip = ipaddress.IPv4Address('188.8.131.52')
# summarize_address_range produces a generator, so force the results
networks = [n for n in ipaddress.summarize_address_range(startip, endip)]