How can I determine if an ip address exists inside a cidr range in python?I have a long list of cidr ip address ranges in my firewall. In some instances I need to determine if a specified IP address falls inside one of my cidr deny range rules. If possible this should be written in python. Example: ip = "192.168.1.5" cidrs = [ "10.10.10.0/24", "10.11.10.0/22", "192.168.1.0/20" ] The code should give me "192.168.1.0/20". Thanks |
from netaddr import all matching cidrsThe netaddr module has a method that does exactly what you are asking.
Here is the usage for this method:
Basically you provide an ip address as the first argument and a list of cidrs as the second argument. A list of hits are returned. |
Website screenshot