How to find the network address & broadcast address from an ip address from that range & subnet mask.

Karada Sagar Pa
Kilo Guru

I have an ip address & subnet mask of an ip range. Now I wanted to verify an entered ip by user in catalog form to check whether this ip is part of any ip range or not.

Example - 

Input - 192.168.3.4 (From user in catalog form)

In Table I have data like - (ip - 192.168.3.112), (subnet mask - 255.255.255.0)

 

Can anyone help me how to find out that this ip belongs to this range or not ?

3 REPLIES 3

Prafull Patil
Kilo Guru

Hi,

Please find the below solution for the same,

1. Convert the Subnet Mask to CIDR, you need to check how you can convert it, please find the below link for the Subnet Mask to CIDR map.

https://www.freecodecamp.org/news/subnet-cheat-sheet-24-subnet-mask-30-26-27-29-and-other-ip-address-cidr-network-references/

2. Create Client callable script include.

var CIDRinValidation = Class.create();
CIDRinValidation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    ipIsInCidr: function () {
        var cidr = this.getParameter('sysparm_cidr');
        var ip = this.getParameter('sysparm_ip');
        var cidrIp = cidr.split('/')[0];
        var cidrSm = cidr.split('/')[1];
        var ipAddressIp = ip.split('/')[0];
        var ipAddressSm = ip.split('/')[1];
        // return (this.IPnumber(ip) & this.IPmask(cidrSm)) == this.IPnumber(cidrIp);
        if (parseInt(ipAddressSm) < parseInt(cidrSm)) {
            return "No";
        }
        if ((this.IPnumber(ipAddressIp) & this.IPmask(cidrSm)) == this.IPnumber(cidrIp)) {
            return "Yes";
        } else {
            return "No";
        }

    },
    IPnumber: function (IPaddress) {
        var ip = IPaddress.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
        if (ip) {
            return (+ip[1] << 24) + (+ip[2] << 16) + (+ip[3] << 8) + (+ip[4]);
        }
        // else ... ?
        return null;
    },
    IPmask: function (maskSize) {
        return -1 << (32 - maskSize);
    },
    type: 'CIDRinValidation'
});

3. Create Client callable script in catalog item.

var ip = '192.168.1.4';
var cidr = '192.168.1.0/24';

var ga = new GlideAjax('CIDRinValidation');
ga.addParam('sysparm_name', 'ipIsInCidr');
ga.addParam('sysparm_cidr', cidr);
ga.addParam('sysparm_ip', ip);
ga.getXMLAnswer(ipIsINCIDR);

// the callback function for returning the result from the server-side code
function ipIsINCIDR(response) {
    if (response == "No") {
        alert("Entered " + ip + " should be under " + cidr);
        return false;
    } else{
        alert('IP is in CIDR');
    }
}

Please let me know if you have any issues.

 

Thanks,

Prafull Patil

This is very limited because my cidr should have first ip or network address then only it will work like cidr you have taken - 

cidr - '192.168.1.0/24'

here again I have to calculate the network address ('192.168.1.0') with mask value ('24').

 

Right now I have the data like any IP from the range &  subnet mask. is it possible to find to do it with this much data.

 

Also converting these things it's not possible in javscript I believe, It has to be done using other scripts like powershell or python.

Hello,

 

I created to function that use in script include

 

The first retrieve CIDR from IP Adresse and Netmask

 

 

 

//Retrieve CIDR (Classless Inter-Domain Routing) from ip/netmask
    getCIDR: function(ip, subnetMask) {
        var ipArray = ip.split('.');
        var subnetMaskArray = subnetMask.split('.');
        var cidr = 0;

        for (var i = 0; i < 4; i++) {
            var ipBinaryString = parseInt(ipArray[i]).toString(2);
            var subnetMaskBinaryString = parseInt(subnetMaskArray[i]).toString(2);

            for (var j = 0; j < subnetMaskBinaryString.length; j++) {
                if (subnetMaskBinaryString[j] === '1') {
                    cidr++;
                }
            }
        }

        return ip + '/' + cidr;
    },

 

 

The second retrieve Network IP, the first IP, the last IP and Broadcast IP

 

//Retrieve network detail from CIDR
    getNetworkDetails: function(cidr) {
        var ip = cidr.split('/')[0];
        var prefix = cidr.split('/')[1];

        var ipArray = ip.split('.').map(Number);
        var maskArray = [];
        for (var i = 0; i < 4; i++) {
            maskArray.push((i < Math.floor(prefix / 8)) ? 255 :
                (i > Math.floor(prefix / 8)) ? 0 :
                (256 - Math.pow(2, 8 - (prefix % 8))));
        }

        var networkArray = [],
            broadcastArray = [],
            firstIPArray = [],
            lastIPArray = [];
        for (var i = 0; i < 4; i++) {
            networkArray.push(ipArray[i] & maskArray[i]);
            broadcastArray.push(networkArray[i] | (255 ^ maskArray[i]));
        }

        firstIPArray = networkArray.slice(0);
        firstIPArray[3]++;
        lastIPArray = broadcastArray.slice(0);
        lastIPArray[3]--;

        return {
            networkAddress: networkArray.join('.'),
            firstIP: firstIPArray.join('.'),
            lastIP: lastIPArray.join('.'),
            broadcastAddress: broadcastArray.join('.')
        };
    },

 

 

 

 

maybe it would be useful for you