The CreatorCon Call for Content is officially open! Get started here.

Question on scripting to determine valid ip address range

mon4
Giga Contributor

We have a vast range of ip addresses that we need to verify against for valid requests.

The idea is to filter out any incoming email that does not contain a valid ip address in the body.

 

In the Filters module of System Mailboxes, we can set values to filter for so we can set conditions like 'body contains <ip address', but that would mean that we need to manually input all the ip addresses.   Since we have over a million of them, this is not an option.   We need to be able to script it.

 

Does anyone know how to script to verify a valid IP range based on the CIDR notation and subnet mask?

A sample script would be much appreciated.   Thank you very much.

8 REPLIES 8

Hi Michael,



Thanks for sharing this code. I have only short question. When I try to use it on my dev instance (Istanbul), I get following error:



java.lang.SecurityException: Illegal access to constructor com.snc.commons.networks.IPRangeV4(com.snc.commons.networks.IPAddressV4,com.snc.commons.networks.IPAddressV4)



Do you know if usage of "SncIPRangeV4" was somehow restricted recently and if there is any workaround (either whitelist it or do use another allowed class)?



Thanks,


Dominik


Dominik, I haven't used this code in quite some time and was able to reproduce the errors you are seeing.   I would suggest opening a ticket with Support as this appears to be a bug.


Actually did a little more research and found that you can just leverage the SncIPRangeV4 API for everything you need.   I ran the following script and got a true response:


var ipAdressV4 = new SncIPAddressV4("192.168.1.56");  


var startIP = new SncIPAddressV4("192.168.1.1");  


var endIP = new SncIPAddressV4("192.168.1.100");


gs.print((ipAdressV4.getAddressAsLong() >= startIP.getAddressAsLong() && ipAdressV4.getAddressAsLong() <= endIP.getAddressAsLong()))



I then ran the following and got a false:


var ipAdressV4 = new SncIPAddressV4("192.168.1.56");  


var startIP = new SncIPAddressV4("192.168.1.60");  


var endIP = new SncIPAddressV4("192.168.1.100");


gs.print((ipAdressV4.getAddressAsLong() >= startIP.getAddressAsLong() && ipAdressV4.getAddressAsLong() <= endIP.getAddressAsLong()))


salemsap
Tera Expert

I think,you can use regular expression to validate the IP address.