How would you automate checking if an IPv4 is in a CIDR range?

I would love to hear your ideas on this.

Goal - check if example IP 158.69.17.240 is in 200+ list of CIDR ranges (for example 158.69.17.0/24)

1 Like

Hi Hayden - we do not currently have a plugin that can return output showing whether or not an IP address was within a specific CIDR range. It would be useful to have as a plugin, though!

For now your best bet would be to write a Python Script step to check for this. Python includes a library for IP address manipulation that can help with this :slight_smile:

how do import that mod into the python script action? didnā€™t work just now ā€˜import ipaddressā€™ or from ipaddress import ip_network, ip_address

It should work - is your import within the run function? It should look something like

def run(params={}):
  import ipaddress
 
  # Do something with ipaddress here

that was itā€¦

now to check against a big list of ranges.

def run(params={}):
import ipaddress
an_address = ipaddress.ip_address(ā€œ192.168.0.1ā€)
a_network = ipaddress.ip_network(ā€˜192.168.0.0/24ā€™)

address_in_network = an_address in a_network
return {"output":str(address_in_network)}
1 Like

Is this how you would do it? I need to refactor this 4sureā€¦

def run(params={}):
import ipaddress

networks = ["1.1.1.1/24".... etc]
an_address = ipaddress.ip_address("{{["loop over ips"].[$item]}}")
s = set(networks)
trues = []
for n in s:
   if an_address in ipaddress.ip_network(n):
      trues.append("true")
return {"output":trues}
1 Like

Hi Hayden -

That is very similar to how I would do it! Let us know how that ends up working for you!