- 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
07-04-2022 10:15 PM
Hi
Thanks for marking my answer as correct!
Yes, you are right!
Converting to Hex would be easier.
Thanks,
Kartik