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 work with CIDR (v4, v6)
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 if options are empty
.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) {
get array of all ip in CIDR range
you can set maximum of array elements with options.limit
if the limit is reached, you will receive additional information in options.limitResult
arrayAction(fn, options)
run fn for every element of CIDR range