Skip to content

Instantly share code, notes, and snippets.

@binarymax
Last active July 30, 2021 11:03
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save binarymax/6114792 to your computer and use it in GitHub Desktop.
Save binarymax/6114792 to your computer and use it in GitHub Desktop.
JavaScript function to parse a CIDR Range string into beginning and ending IPv4 Addresses
//MIT License
//Copyright (c) 2013, Max Irwin
//Parses a CIDR Range into beginning and ending IPv4 Addresses
//For example: '10.0.0.0/24'
//Returns ['10.0.0.0', '10.0.0.255']
var parseCIDR = function(CIDR) {
//Beginning IP address
var beg = CIDR.substr(CIDR,CIDR.indexOf('/'));
var end = beg;
var off = (1<<(32-parseInt(CIDR.substr(CIDR.indexOf('/')+1))))-1;
var sub = beg.split('.').map(function(a){return parseInt(a)});
//An IPv4 address is just an UInt32...
var buf = new ArrayBuffer(4); //4 octets
var i32 = new Uint32Array(buf);
//Get the UInt32, and add the bit difference
i32[0] = (sub[0]<<24) + (sub[1]<<16) + (sub[2]<<8) + (sub[3]) + off;
//Recombine into an IPv4 string:
var end = Array.apply([],new Uint8Array(buf)).reverse().join('.');
return [beg,end];
}
@geeliano
Copy link

When trying this script I get the error "TypeError: second argument to Function.prototype.apply must be an array "

Tried it with the CIDR 192.168.20.1/24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment