What is ip-cidr?
The ip-cidr npm package provides utilities for working with IP addresses and CIDR (Classless Inter-Domain Routing) notations. It allows you to perform various operations such as checking if an IP address is within a CIDR range, converting CIDR notations to IP ranges, and more.
What are ip-cidr's main functionalities?
Check if an IP is within a CIDR range
This feature allows you to check if a given IP address is within a specified CIDR range. In this example, '192.168.1.5' is checked against the CIDR range '192.168.1.0/24'.
const IPCIDR = require('ip-cidr');
const cidr = new IPCIDR('192.168.1.0/24');
const isInRange = cidr.contains('192.168.1.5');
console.log(isInRange); // true
Convert CIDR to IP range
This feature converts a CIDR notation to its corresponding IP range. In this example, '192.168.1.0/24' is converted to the range ['192.168.1.0', '192.168.1.255'].
const IPCIDR = require('ip-cidr');
const cidr = new IPCIDR('192.168.1.0/24');
const range = cidr.toRange();
console.log(range); // ['192.168.1.0', '192.168.1.255']
Get the first and last IP addresses in a CIDR range
This feature retrieves the first and last IP addresses within a given CIDR range. In this example, '192.168.1.0/24' is used to get the first and last IP addresses.
const IPCIDR = require('ip-cidr');
const cidr = new IPCIDR('192.168.1.0/24');
const firstIp = cidr.start({ type: 'addressObject' });
const lastIp = cidr.end({ type: 'addressObject' });
console.log(firstIp); // { address: '192.168.1.0', subnetMask: 24 }
console.log(lastIp); // { address: '192.168.1.255', subnetMask: 24 }
Other packages similar to ip-cidr
ip
The 'ip' package provides a set of utilities for working with IP addresses, including parsing, formatting, and converting between different representations. It also supports CIDR notation but is more general-purpose compared to ip-cidr.
cidr-js
The 'cidr-js' package offers similar functionalities to ip-cidr, such as checking if an IP is within a CIDR range and converting CIDR to IP ranges. It is a lightweight alternative with a focus on CIDR operations.
netmask
The 'netmask' package is another utility for working with IP addresses and subnet masks. It provides methods for parsing and manipulating IP ranges and CIDR notations, similar to ip-cidr.
Install
npm install ip-cidr
About
Module for working with CIDR (v4, v6). Based on ip-address
Example
const CIDR = require("ip-cidr");
let cidr = new CIDR("50.165.190.0/23");
if(!cidr.isValid()) {
}
cidr.start();
cidr.end({ asBigInteger: true });
let data = [];
let fn = (ip) => {
data.push(ip)
}
cidr.arrayAction(fn, { asAddressObject: true });
cidr.toArray({ asBigInteger: true });
cidr.toRange()
FULL API
.formatIP(address, options)
returns the "ip-address" module object ip address in the specified in options format
options are the same in all of the library functions and may include asBigInteger, asAddressObject or asString (by default)
.start(options)
get start ip
.end(options)
get end ip
.toString()
get string cidr as "50.165.190.0/23"
.toRange(options)
get array of start and end ip [startIp, endIp]
.toObject(options)
get object of start and end ip {start: startIp, end: endIp}
.toArray(options, results)
get array of all ip in CIDR range
you can get information by chunks using options.from and options.limit
this options can be integer or big integer("jsbn" instance)
you can send results argument(object) to get all chunk information inside
pagination starts from 0, all results values are instance of "jsbn"
.arrayAction(fn, options, results)
run fn for every element of CIDR range
you can use the same chunk options as in .toArray