有人可以帮助我为第二个"else if"循环中可用的脚本创建一个函数吗?我需要在其他 if 循环中一次又一次地使用相同的脚本。我非常感谢您的帮助。谢谢!
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 {
}
为了回答你的问题,你只需将该代码块放在函数中,但是如果你想在函数外部使用变量rangeadd和excludeadd,它们不能在函数内声明为var。
更新:如果要更改 upToNumber 变量,则应将其转换为传递给函数的参数,但如果您声明该变量。
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);