- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 02:28 PM
Hi all,
I have a need to print all the IP Address within a range, so if the range is eg 192.168.1.1 - 192.168.1.255 i need everything in between.
192.168.1.1
192.168.1.2
192.168.1.3
and so on.
has anyone done this before?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2022 07:51 AM
Hi
Please find the code to fetch the IP Addresses between provided ranges:
var startIp = '192.168.19.0',
endIp = '192.168.22.10',
startDeciIp = ipToInt32(startIp),
endDeciIp = ipToInt32(endIp);
gs.print('Start IP: ' + startDeciIp + '\nEnd IP: ' + endDeciIp);
if((endDeciIp - startDeciIp) > 0) {
gs.print('Inside IF as IP ranges are correct');
for(var i = startDeciIp; i <= endDeciIp; i++) {
gs.print(int32ToIp(i));
}
} else {
gs.print('End IP Address is less then Start IP Address');
}
//Convert Decimal to IPv4
function int32ToIp (num) {
//gs.print('Working for Dec IP: ' + num);
return (num >>> 24 & 0xFF) + '.' +
(num >>> 16 & 0xFF) + '.' +
(num >>> 8 & 0xFF) + '.' +
(num & 0xFF);
};
//IP to Decimal
function ipToInt32(ip) {
gs.print('Converting IP Address: ' + ip);
return ip.split(".").reduce(function (x, v) {
//gs.print('x: ' + x + '\tv: ' + v);
return x*256 + +v;
});
}
The code above works on the principle of converting IP Address to Decimal (base 32) values and then converting back to IP Address.
Remark: Above code is only for IPv4 address
Please check the screenshot with the results:
Screenshot1:
Screenshot2:
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 04:56 PM
Hi Daniel,
Create 3 fields: "start ip address", "end ip address", and "ip addresses" to hold result.
Created onChange() Client scripts on fields "start ip address" and "end ip address" both will following content.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
var startIPAddress = g_form.getValue('start_ip_address');
var endIPAddress = g_form.getValue('end_ip_address');
if (startIPAddress.length < 1 || endIPAddress.length < 1) {
return;
}
var startIPAddressArray = startIPAddress.split('.');
var start = Number(startIPAddressArray[3]);
var end = Number(endIPAddress.split('.')[3]);
if (start > end) {
g_form.showFieldMsg('start_ip_address', 'start ip address must be less than end ip address.', 'error');
}
startIPAddressArray.pop();
var baseAddress = startIPAddressArray.join('.');
var addressArray = [];
for (var i = start; i <= end; i++) {
addressArray.push(baseAddress + '.' + i);
}
g_form.setValue('ip_adresses', addressArray.join('\n'));
} catch (e) {
alert(e.message);
}
}
Execution result
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 11:23 PM
HI Hitoshi,
this is awesome thanks, however this only changes the last octet ie, if i put 192.168.1.1 - 192.168.2.255
or 192.170.1.10 - 192. 178.10.11
its only going to display the ip's for the range 192.168.1.1 - 192.168.1.255.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2022 07:51 AM
Hi
Please find the code to fetch the IP Addresses between provided ranges:
var startIp = '192.168.19.0',
endIp = '192.168.22.10',
startDeciIp = ipToInt32(startIp),
endDeciIp = ipToInt32(endIp);
gs.print('Start IP: ' + startDeciIp + '\nEnd IP: ' + endDeciIp);
if((endDeciIp - startDeciIp) > 0) {
gs.print('Inside IF as IP ranges are correct');
for(var i = startDeciIp; i <= endDeciIp; i++) {
gs.print(int32ToIp(i));
}
} else {
gs.print('End IP Address is less then Start IP Address');
}
//Convert Decimal to IPv4
function int32ToIp (num) {
//gs.print('Working for Dec IP: ' + num);
return (num >>> 24 & 0xFF) + '.' +
(num >>> 16 & 0xFF) + '.' +
(num >>> 8 & 0xFF) + '.' +
(num & 0xFF);
};
//IP to Decimal
function ipToInt32(ip) {
gs.print('Converting IP Address: ' + ip);
return ip.split(".").reduce(function (x, v) {
//gs.print('x: ' + x + '\tv: ' + v);
return x*256 + +v;
});
}
The code above works on the principle of converting IP Address to Decimal (base 32) values and then converting back to IP Address.
Remark: Above code is only for IPv4 address
Please check the screenshot with the results:
Screenshot1:
Screenshot2:
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2022 07:24 PM
This works.. I figured out different way to go about this, I also convert to hex because it a little easier to deal with Instead of a 32bit decimal number,