Skip to content Skip to sidebar Skip to footer

Javascript Calculate Ipv6 Range From Cidr Prefix

Using Javascript (without JQuery) I'm looking to get the minimum and maximum IPs in a IPv6 CIDR prefix. For example, 2001:280::/32 would output 2001:280:0:0:0:0:0:0 and 2001:280:ff

Solution 1:

Assuming you have Node and NPM installed:

$ touch index.js
$ npm init
$ npm i --save ip-address
$ vim index.js

var v6 = require('ip-address').v6;

var addr = new v6.Address('2001:280::/32');

console.log(addr.parsedAddress.join(':'));
console.log(addr.endAddress().address);

$ <esc>:wq$ node index.js
2001:280:0:0:0:0:0:02001:0280:ffff:ffff:ffff:ffff:ffff:ffff

There doesn't seem to be a browser facing package so I'd suggest using Browserify (http://browserify.org/) to get this to work or fork the project and stuff everything into one file so you can run it in your browser (leaving out the node-specific code, of course).

Solution 2:

Try the ip6 npm package: https://www.npmjs.com/package/ip6

ip6 helps to normalize, abbreviate, divide subnets, generate random subnets/hosts and calculate range of size of an IPv6 subnet.

let ip6 = require('ip6');
console.log(ip6.range('2001:280:0:0:0:0:0:0', 32));
{ start: '2001:0280:0000:0000:0000:0000:0000:0000',
  end: '2001:0280:ffff:ffff:ffff:ffff:ffff:ffff',
  size: 7.922816251426434e+28 }

Or in command line:

ip6 -R 2001:280:0:0:0:0:0:032
{"start":"2001:0280:0000:0000:0000:0000:0000:0000","end":"2001:0280:ffff:ffff:ffff:ffff:ffff:ffff","size":7.922816251426434e+28}

Post a Comment for "Javascript Calculate Ipv6 Range From Cidr Prefix"