how do you get and print all IP Address in a range eg 192.168.1.1 - 192.168.1.255?

DanielCordick
Mega Patron
Mega Patron

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?

 

1 ACCEPTED SOLUTION

Kartik Sethi
Tera Guru
Tera Guru

Hi @Daniel 

 

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:
find_real_file.png

 

Screenshot2:

find_real_file.png

 


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

View solution in original post

5 REPLIES 5

Hi @Daniel 

 

Thanks for marking my answer as correct!

Yes, you are right!

Converting to Hex would be easier.

 

Thanks,

Kartik