ip
//npm
npm install ip
// npm readme
https://www.npmjs.com/package/ip
// github
git clone https://github.com/indutny/node-ip.git
let ip =require('ip');
ip.address()// my ip address
ip.isPrivate('127.0.0.1')// Check whether an IP is within a private IP address range.
ip.isV4Format('127.0.0.1');// Check whether an IP is a IPv4 address.
ip.isV6Format('::ffff:127.0.0.1');// Check whether an IP is a IPv6 address.
address
//npm
npm install address
// npm readme
https://www.npmjs.com/package/address
// github
git clone https://github.com/node-modules/address
let address =require('address');
address.ip();// 192.168.0.125
address.ipv6();// 192.168.0.125// 获取默认mac地址
address.mac(function(err, addr){
console.log(addr);// '78:ca:39:b0:e6:7d'});// 指定网卡获取mac地址
address.mac('enp1s0',function(err, addr){
console.log(addr);// '0a:00:27:00:00:00'});// dns是通过读取/etc/resolv.conf获取的,建议用nmcli获取
address.dns((err, addrs)=>{
console.log(addrs);// ['10.13.2.1', '10.13.2.6']});address((err, addrs)=>{
console.log(addrs.ip, addrs.ipv6, addrs.mac);// '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d'});address('enp1s0',(err, addrs)=>{
console.log(addrs.ip, addrs.ipv6, addrs.mac);// '192.168.56.1', null, '0a:00:27:00:00:00'});
address.interface('IPv4','eth1');// { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' }
netmask
//npm
npm install netmask
// npm readme
https://www.npmjs.com/package/netmask
// github
git clone https://github.com/rs/node-netmask
var Netmask =require('netmask').Netmask
var block =newNetmask('10.0.0.0/12');
block.base;// 10.0.0.0
block.mask;// 255.240.0.0
block.bitmask;// 12
block.hostmask;// 0.15.255.255
block.broadcast;// 10.15.255.255
block.size;// 1048576
block.first;// 10.0.0.1
block.last;// 10.15.255.254
block.contains('10.0.8.10');// true
block.contains('10.8.0.10');// true
block.contains('192.168.1.20');// false
block.forEach(function(ip, long, index));
block.next()// Netmask('10.16.0.0/12')
ipaddr.js
//npm
npm install ipaddr.js
// npm readme
https://www.npmjs.com/package/ipaddr.js
// github
git clone https://github.com/whitequark/ipaddr.js
// 可以用来校验和格式化let ipaddr =require('ipaddr.js');// 可以获取掩码的bitmask 也可以校验掩码格式
ipaddr.IPv4.parse('255.255.255.240').prefixLengthFromSubnetMask()==28
ipaddr.IPv4.parse('255.192.164.0').prefixLengthFromSubnetMask()==null// bitmask转掩码
ipaddr.IPv4.subnetMaskFromPrefixLength(24)=='255.255.255.0'
ipaddr.IPv4.subnetMaskFromPrefixLength(29)=='255.255.255.248'
版权归原作者 金厢白玉圭 所有, 如有侵权,请联系我们删除。