Documentation For SNC Network APIs

Tom Rausch
Tera Guru

Is there any online documentation for these network APIs? If so, where is it?

  • SncIPAddressV4 
  • SncIPAddressV6
  • SncIPIterator
  • SncIPList
  • SncIPMetaCollection
  • SncIPNetmaskV4
  • SncIPNetworkV4
  • SncIPRangeV4
  • SncMACAddress
  • SncMACAddressMfr
  • SncIPAuthenticator
9 REPLIES 9

Hello, @Lalit Gupta,

My primary goals relate to CIDR ranges.

  • Find the beginning and ending IP addresses of a CIDR Range
  • Determine whether an IP address is in a CIDR range
  • Determine whether an IP address is valid

I found subroutines to perform all these tasks. These subroutines are shown here.

// Function "cidr2Range"
// Input     - CIDR Range
// Return    - Beginning and end IP addresses of the CIDR range
// Reference - https://stackoverflow.com/questions/13683436/retrieve-max-min-ips-from-cidr-range/20004699
IPUtilities.cidr2Range = function(cidr) {
  var range = [2];
  cidr = cidr.split('/');
  var start = IPUtilities.ip2long(cidr[0]);
  range[0] = IPUtilities.long2ip(start);
  range[1] = IPUtilities.long2ip(Math.pow(2, 32 - cidr[1]) + start - 1);
  return range;
}


// Function "hex2ip"
// Input     - Hexadecimal IP address
// Return    - IPv4 dotted address
// Reference - http://jsfiddle.net/sL7VM/
IPUtilities.hex2ip = function(hexipaddress) {
  var oct4ip = (hexipaddress>>24) & 0xff;
  var oct3ip = (hexipaddress>>16) & 0xff;
  var oct2ip = (hexipaddress>>8) & 0xff;
  var oct1ip = hexipaddress & 0xff;
  return (oct4ip + "." + oct3ip + "." + oct2ip + "." + oct1ip);
}


// Function "inCidrSubNet"
// Input     - IP Address (Example: "192.168.0.1"), CIDR Range [CIDR Address Block + "/" + CIDR Mask Bits (Example: "192.168.0.0/24")]
// Return    - IP Address is in CIDR Range (true/false)
// Reference - https://stackoverflow.com/questions/503052/javascript-is-ip-in-one-of-these-subnets
IPUtilities.inCidrSubNet = function(ip, subnet) {
    var mask, base_ip, long_ip = IPUtilities.ip2longcidr(ip);
    if( (mask = subnet.match(/^(.*?)\/(\d{1,2})$/)) && ((base_ip = IPUtilities.ip2longcidr(mask[1])) >= 0) ) {
        var freedom = Math.pow(2, 32 - parseInt(mask[2]));
        return (long_ip > base_ip) && (long_ip < base_ip + freedom - 1);
    }
    else return false;
};


// Function "ip2long"
// Input     - IPv4 dotted address
// Return    - Proper IP address
// Reference - http://phpjs.org/functions/ip2long
// Reference - http://www.navioo.com/javascript/tutorials/Javascript_ip2long_1543.html
// Reference - https://stackoverflow.com/questions/503052/javascript-is-ip-in-one-of-these-subnets
IPUtilities.ip2long = function(ip_address) {
    var output = false;
    var parts = [];
    if (ip_address.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
        parts  = ip_address.split('.');
        output = ( parts[0] * 16777216 +
        ( parts[1] * 65536 ) +
        ( parts[2] * 256 ) +
        ( parts[3] * 1 ) );
    }
    return output;
}


// Function "ip2longcidr"
// Input     -
// Return    -
// Reference - https://stackoverflow.com/questions/503052/javascript-is-ip-in-one-of-these-subnets
IPUtilities.ip2longcidr = function(ip) {
    var components;
    if(components = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) {
        var iplong = 0;
        var power  = 1;
        for(var i=4; i>=1; i-=1) {
            iplong += power * parseInt(components[i]);
            power  *= 256;
        }
        return iplong;
    }
    else return -1;
};


// Function "long2ip"
// Input     - Proper IP address; Example: '3221234342'
// Return    - IPv4 dotted address; Example '192.0.34.166'
// Reference - http://phpjs.org/functions/long2ip
IPUtilities.long2ip = function(proper_address) {
var output = false;
if (!isNaN(proper_address) && (proper_address >= 0 || proper_address <= 4294967295)) {
    output = Math.floor(proper_address / Math.pow(256, 3)) + '.' +
    Math.floor((proper_address % Math.pow(256, 3)) / Math.pow(256, 2)) + '.' +
    Math.floor(((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) / Math.pow(256, 1)) + '.' +
    Math.floor((((proper_address % Math.pow(256, 3)) % Math.pow(256, 2)) % Math.pow(256, 1)) / Math.pow(256, 0));
}
return output;
}


// Function "validateIP"
// Input     - IPv4 dotted address
// Return    - Input is a valid IPv4 dotted address (true/false)
// Reference - http://infofy.info/info/computers/web-development/javascript/validating-ip-address-javascript/#.W2DustVKjIU
IPUtilities.validateIP = function(inputIp) {
    // Check Format
    var ip = inputIp.split(".");
    if (ip.length != 4) {
        return false;
    }
    // Check Numbers
    for (var c = 0; c < 4; c++) {
        // Perform Test
        if ( ip[c] <= -1 || ip[c] > 255 ||
             isNaN(parseFloat(ip[c])) ||
             !isFinite(ip[c])  ||
             ip[c].indexOf(" ") !== -1 ) {
             return false;
        }
    }
    return true;
};

I placed these in a custom Script Include. I can then call them from any script.

These do not "input a CIDR range and then output a list of the constituent IP addresses" as I stated a few months ago. I found I do not need to do this. It would be relatively easy to construct such a subroutine using these subroutines.

Regards,

Tom Rausch

thanks for response Tom,

Is there any way if you can guide in subroutine for finding all IPs in a subnet range.

 

Thanks

Lalit Gupta

> Is there any way if you can guide in subroutine for finding all IPs in a subnet range.

Hello, @Lalit Gupta,

Does the function "ipBeginningAndEndToList" help you?

// Function "ip2long"
// Input     - IPv4 Dotted Address; Example: '192.0.34.166'
// Return    - Long IP Address (Decimal); Example: '3221234342'
// Reference - http://phpjs.org/functions/ip2long
// Reference - http://www.navioo.com/javascript/tutorials/Javascript_ip2long_1543.html
// Reference - https://stackoverflow.com/questions/503052/javascript-is-ip-in-one-of-these-subnets
ip2long = function(ip_address) {
  var output = false;
  var parts = [];
  if (ip_address.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
      parts  = ip_address.split('.');
      output = ( parts[0] * 16777216 +
      ( parts[1] * 65536 ) +
      ( parts[2] * 256 ) +
      ( parts[3] * 1 ) );
  }
  return output;
};


// Function "long2ip"
// Input     - Long IP Address (Decimal); Example: '3221234342'
// Return    - IPv4 Dotted Address; Example '192.0.34.166'
// Reference - http://phpjs.org/functions/long2ip
long2ip = function(longIpAddress) {
  var output = false;
  output = Math.floor(longIpAddress / Math.pow(256, 3)) + '.' +
  Math.floor((longIpAddress % Math.pow(256, 3)) / Math.pow(256, 2)) + '.' +
  Math.floor(((longIpAddress % Math.pow(256, 3)) % Math.pow(256, 2)) / Math.pow(256, 1)) + '.' +
  Math.floor((((longIpAddress % Math.pow(256, 3)) % Math.pow(256, 2)) % Math.pow(256, 1)) / Math.pow(256, 0));
  return output;
};


// Function "ipBeginningAndEndToList"
// Input  - IPv4 Dotted Address (Beginning Of Range), IPv4 Dotted Address (End Of Range)
// Return - List Of IPv4 Dotted Addresses From Beginning IP Address To End IP Address, Inclusive
ipBeginningAndEndToList = function(beginning_ip_address,end_ip_address) {
  var ipList = [];
  beginning_ip_address_long = ip2long(beginning_ip_address);
  end_ip_address_long = ip2long(end_ip_address);
  for (var step = beginning_ip_address_long; step <= end_ip_address_long; step++) {
     step_ip_address = long2ip(step);
     ipList.push(step_ip_address);
  }
  return ipList;
};

var beginEnd = ipBeginningAndEndToList("192.168.0.1", "192.168.0.10");
gs.print(beginEnd);

Thanks Tom,

Its working , doing some more testing with subnets.

regards

Lalit Gupta

This was a life saver!  Thank you!