-3

can someone help me with creating a function for the script available in the second 'else if' loop. I need to use the same script again and again in other else if loops.I really appreciate your help. Thanks!

var network = '10.31.224.0/21'; //This address(/21) = 2047
var network_1 = '10.31.224.0/22'; //This address(/22) = 1023
var network_2 = '10.31.224.0/22'; //This address(/23) = 511
if (cidrToRange(network.toString()) > 2048) {
  //skip these ranges 

} else if (cidrToRange(network.toString()) < 126) {
  //below code inside else if goes in here
} else if (cidrToRange(network.toString()) == 2047) {
  var IPstart = network.toString().substring(0, network.lastIndexOf('.')) + ".1";
  var IPend = network.toString().substring(0, network.lastIndexOf('.')) + ".10";
  var upToNumber = 8;
  for (var i = 0; i < upToNumber; i++) {
    var res_ipstart = IPstart.split(".");
    res_ipstart[2] = parseInt(res_ipstart[2]) + i;
    var excludestart = res_ipstart.join(".");
    var res_ipend = IPend.split(".");
    res_ipend[2] = parseInt(res_ipend[2]) + i;
    var excludeend = res_ipend.join(".");
    var excludename = excludestart + "-" + excludeend;
  }
  var recadd = new GlideRecord('discovery_range_item');
  recadd.initialize();
  recadd.network_ip = cidr_0.toString();
  recadd.netmask = cidr_1.toString();
  recadd.type = "IP Network";
  recadd.u_discovery_range_item_description = parsed[i].comment;
  var networkadd = recadd.insert();

  recadd.initialize();
  recadd.name = excludename;
  recadd.start_ip_address = excludestart;
  recadd.end_ip_address = excludeend;
  recadd.type = "IP Address Range";
  var rangeadd = recadd.insert();

  var recexclude = new GlideRecord('discovery_range_item_exclude');
  recexclude.initialize();
  recexclude.name = excludename;
  recexclude.start_ip_address = excludestart;
  recexclude.end_ip_address = excludeend;
  recexclude.parent = networkadd.toString();
  recexclude.type = "IP Address Range";
  var excludeadd = recexclude.insert();
} else if (cidrToRange(network.toString()) == 1023) {
  //above code from else if goes here 

} else {

}
Ivan
  • 34,531
  • 8
  • 55
  • 100
evan
  • 59
  • 6
  • Wouldn't the `||` logical OR operator work for you here? Seems like you want to execute the same code upon multiple different conditions, yeah? – mhodges Jun 01 '18 at 21:28
  • It would also be advantageous for you to store the results of `cidrToRange(network.toString())` in a variable, so you don't have to keep calling the function and calculating the same answer. – mhodges Jun 01 '18 at 21:29
  • @mhodges Thanks for the reply, it should work but var upToNumber = 8; will changes in different loops. – evan Jun 01 '18 at 21:32

1 Answers1

0

To answer your question, you just put that block of code inside of a function, but then if you want to use the variables rangeadd and excludeadd outside of the function they can't be declared as var inside the function.

Update: If the upToNumber variable is going to be changed then it should be made into a parameter passed into the function, but if you declare that variable.

var rangeadd, excludeadd;

function myFunction(upToNumber) {
    var IPstart = network.toString().substring(0,network.lastIndexOf('.')) + ".1";
    var IPend = network.toString().substring(0,network.lastIndexOf('.')) + ".10";
    //var upToNumber=8;
    for(var i=0;i<upToNumber;i++){
        var res_ipstart = IPstart.split(".");
        res_ipstart[2]=parseInt(res_ipstart[2]) + i;
        var excludestart = res_ipstart.join(".");
        var res_ipend = IPend.split(".");
        res_ipend[2]=parseInt(res_ipend[2]) + i;
        var excludeend = res_ipend.join(".");
        var excludename=excludestart+"-"+excludeend;
    }
    var recadd = new GlideRecord('discovery_range_item');
    recadd.initialize();
    recadd.network_ip = cidr_0.toString();
    recadd.netmask = cidr_1.toString();
    recadd.type = "IP Network";
    recadd.u_discovery_range_item_description = parsed[i].comment;
    var networkadd = recadd.insert();

    recadd.initialize();
    recadd.name = excludename;
    recadd.start_ip_address = excludestart;
    recadd.end_ip_address = excludeend;
    recadd.type = "IP Address Range";
    rangeadd = recadd.insert();

    var recexclude = new GlideRecord('discovery_range_item_exclude');
    recexclude.initialize();
    recexclude.name = excludename;
    recexclude.start_ip_address = excludestart;
    recexclude.end_ip_address = excludeend;
    recexclude.parent = networkadd.toString();
    recexclude.type = "IP Address Range";
    excludeadd = recexclude.insert();
}

// Call function
var upToNumber = 8;
myFunction(upToNumber);
primehalo
  • 859
  • 1
  • 14
  • 26