简体   繁体   English

Javascript从CIDR前缀计算IPv6范围

[英]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. 我正在使用Javascript(不使用JQuery)来获取IPv6 CIDR前缀中的最小和最大IP。

For example, 2001:280::/32 would output 2001:280:0:0:0:0:0:0 and 2001:280:ffff:ffff:ffff:ffff:ffff:ffff . 例如2001:280::/32将输出2001:280:0:0:0:0:0:02001:280:ffff:ffff:ffff:ffff:ffff:ffff

How can I do this? 我怎样才能做到这一点? Thanks in advance! 提前致谢!

Assuming you have Node and NPM installed: 假设您已安装Node和NPM:

$ 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). 似乎没有面向浏览器的软件包,所以我建议使用Browserify( http://browserify.org/ )使它工作将项目分叉并将所有内容填充到一个文件中,以便您可以在自己的文件中运行它浏览器(当然,省略了特定于节点的代码)。

Try the ip6 npm package: https://www.npmjs.com/package/ip6 尝试ip6 npm软件包: 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. ip6有助于规范化,缩写,划分子网,生成随机子网/主机以及计算IPv6子网的大小范围。

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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM