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

Hitoshi Ozawa
Giga Sage
Giga Sage

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

find_real_file.png

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.

 

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

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,