Five Stars

IP Address match to CIDR range

Using Talend 6 Open data integration. Is it possible to compare an IP address from an apache log to a number of CIDR ranges and pick the correct range or do I need to expand out the CIDR range and have a much larger lookup ?
If you imagine my lookup table would be structured something like the simple csv example below. If the IP address was 192.168.100.30 I get the networkname "Blue" 
CIDR,NetworkName
192.168.0.0/24, Orange
192.168.100.0/24,Blue

Would I need to expand the CIDR value to include all the IP addresses and do a simple string comparison or is there a smarter way with Talend?
2 REPLIES
Fifteen Stars

Re: IP Address match to CIDR range

I think the best way to do this is to use the Apache Commons Net library (https://commons.apache.org/proper/commons-net/). Use a tJavaRow to use something similar to the following code....
String cidr = input_row.cidr;
String ip = input_row.ip;
SubnetUtils su = new SubnetUtils(cidr);
SubnetUtils.SubnetInfo info = su.getInfo();
output_row.ip = ip;
output_row.pass = info.isInRange(ip);
output_row.cidr = cidr;

The "cidr" and "ip" columns are String columns holding the cidr code and ip address to be compared. The "pass" column is a boolean used to return whether the ip is in the cidr range.
Rilhia Solutions
Five Stars

Re: IP Address match to CIDR range

Thanks, looks like an elegant solution instead of expanding out the subnets