Need to match IP address to Range using Business Rule

wade88761
Kilo Contributor

I am trying to create a business rule that updates a computer's location based on the IP range it was originally discovered in. I'll have it run whenever the IP of the CI changes. The problem is the first 3 octets of an IP can be the same and they still have different locations. So I have the below code which queries the ranges that have the same first 3 octets but then I need it to pick the range with the next lowest 4th octet compared to the CI's IP.

For example if the CI's IP is 192.168.1.5 and there are ranges for 192.168.1.1/27 and 192.168.1.32/27 I need it to know to pick the 192.168.1.1 range. So I have the variable "last" that grabs the last octet of the CI's IP but I am unsure of how to set it up to compare it with the queries results. Any help is appreciated or ways to do this easier.

 

var ip = current.ip_address;
var shortened = ip.substring(0,ip.lastIndexOf(".")) + ".";

var last = ip.substring(ip.lastIndexOf(".")+1);
gs.log(last);

var gr = new GlideRecord('discovery_range_item');
gr.addQuery('network_ip','STARTSWITH',shortened);
gr.addQuery('active', true);
gr.query();

while(gr.next()) {
var site = gr.parent;
current.u_discovery_site = site;
}

13 REPLIES 13

any further help on this is appreciated. Still struggling with the logic in this.

wade88761
Kilo Contributor

Can't seem to get this to work no matter what I try. Not sure where to start with it. 

ScienceSoft
Tera Guru

It is good to use regexp for your needs

Shashikant Yada
Tera Guru

I did same for one project, I hope this will help you. You need to modify few things but just as a logic I thought to share with you

// Get IP address from mail
var ipadd = email.body.destination_ip_port;
var newipadd =ipadd;
var index=ipadd.indexOf(':');
var aftertrim=newipadd.substring(0,index);
// convert IP address from a.b.c.d to a.b.c
var result = newipadd.substring(0,newipadd.lastIndexOf("."));//a.b.c
var result2 = newipadd.substring(0,newipadd.lastIndexOf(".")+1);//a.b.c.
// Check IP Address in IP Address Info table
var getipadd = new GlideRecord('u_ip_address_info');
var qc = getipadd.addQuery('u_start_ip','CONTAINS',result2);
qc.addOrCondition('u_end_ip','CONTAINS',result2);
getipadd.query();
if(getipadd.next()){
var startip = getipadd.u_start_ip;
var endip = getipadd.u_end_ip;
var str = aftertrim;
var arr = str.split('.');//returns last
if ((aftertrim >= startip && aftertrim <= endip) || (arr >= '1' && arr <= '255')) {
current.location.setDisplayValue(getipadd.u_location_display_name);
current.assignment_group = getipadd.assignment_group;
}
else{
current.location.setDisplayValue('f5738849136c9200bb363cc12244b013');
current.assignment_group = '0b5ac0cf4f8096009b1187dab110c777';
}
}
else { // convert IP address from a.b.c.d to a.b
var result3 = result.substring(0,result.lastIndexOf(".")+1);
var getipadd1 = new GlideRecord('u_ip_address_info');
var qc1 = getipadd1.addQuery('u_start_ip','CONTAINS',result3);
qc1.addOrCondition('u_end_ip','CONTAINS',result3);
getipadd1.query();
if(getipadd1.next()){
var startip = getipadd1.u_start_ip;
var endip = getipadd1.u_end_ip;
if (aftertrim >= startip && aftertrim <= endip) {
current.location.setDisplayValue(getipadd1.u_location_display_name);
current.assignment_group = getipadd1.assignment_group;
}