Javascript calculate IPv6 range from CIDR prefix

你。 提交于 2019-12-14 03:59:09

问题


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:ffff:ffff:ffff:ffff:ffff:ffff.

How can I do this? Thanks in advance!


回答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:0
2001: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).




回答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:0 32
{"start":"2001:0280:0000:0000:0000:0000:0000:0000","end":"2001:0280:ffff:ffff:ffff:ffff:ffff:ffff","size":7.922816251426434e+28}


来源:https://stackoverflow.com/questions/32296203/javascript-calculate-ipv6-range-from-cidr-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!