UNPKG

generate-ip

Version:

Randomly generate, format, and validate IPv4 + IPv6 + MAC addresses.

447 lines (304 loc) 20.1 kB
<a id="top"></a> <div align="center"> <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/logos/generate-ip/white-logo.png"><img width=700 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/logos/generate-ip/black-logo.png"></picture> ### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/icons/node-graph/white/icon55x49.png"><img width=23 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/icons/node-graph/black/icon55x49.png"></picture> &nbsp;Randomly generate, format, and validate IPv4 + IPv6 + MAC addresses. <br> <a href="https://npm-compare.com/generate-ip/#timeRange=ALL"> <img height=31 src="https://img.shields.io/npm/dm/generate-ip?logo=npm&color=af68ff&logoColor=white&labelColor=464646&style=for-the-badge"></a> <a href="https://github.com/adamlui/js-utils/releases/tag/generate-ip-2.9.4"> <img height=31 src="https://img.shields.io/badge/Latest_Build-2.9.4-44cc11.svg?logo=icinga&logoColor=white&labelColor=464646&style=for-the-badge"></a> <a href="#%EF%B8%8F-mit-license"> <img height=31 src="https://img.shields.io/badge/License-MIT-orange.svg?logo=internetarchive&logoColor=white&labelColor=464646&style=for-the-badge"></a> <a href="https://www.npmjs.com/package/generate-ip?activeTab=code"> <img height=31 src="https://img.shields.io/npm/unpacked-size/generate-ip?style=for-the-badge&logo=ebox&logoColor=white&labelColor=464646&color=blue"></a> <a href="https://sonarcloud.io/component_measures?metric=vulnerabilities&selected=adamlui_js-utils%3Agenerate-ip&id=adamlui_js-utils"> <img height=31 src="https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fsonarcloud.io%2Fapi%2Fmeasures%2Fcomponent%3Fcomponent%3Dadamlui_js-utils%3Agenerate-ip%2Fsrc%2Fgenerate-ip.js%26metricKeys%3Dvulnerabilities&query=%24.component.measures.0.value&style=for-the-badge&logo=sonar&logoColor=white&labelColor=464646&label=Vulnerabilities&color=ffef00"></a> <a href="https://github.com/toolleeo/cli-apps/#networking"> <img height=31 src="https://img.shields.io/badge/Mentioned_in-Awesome-ff69b4?logo=awesomelists&logoColor=white&labelColor=464646&style=for-the-badge"></a> <a href="https://www.jsdelivr.com/package/npm/generate-ip?tab=stats"> <img height=31 src="https://img.shields.io/jsdelivr/npm/hm/generate-ip?style=for-the-badge&logo=jsdelivr&logoColor=white&label=jsDelivr%20Hits&labelColor=464646&color=49c5d8"></a> </div> <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## 💡 About **generate-ip** is a lightweight, easy-to-use library that allows you to randomly generate, format & validate IP address(es). - **No external dependencies —** Only built-in crypto methods used for secure randomization - **Multi-protocol support —** IPv4 + IPv6 + MAC addresses supported - **Multi-environment support —** Use in Node.js or the web browser - **Command line usable —** Just type `generate-ip`, that's it <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## ⚡ Installation As a **global utility**: ``` $ npm install -g generate-ip ``` As a **dev dependency**, from your project root: ``` $ npm install -D generate-ip ``` As a **runtime dependency**, from your project root: ``` $ npm install generate-ip ``` <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## 🔌 Importing the APIs #### <a href="#-es-modules-esm"><img height=13 width="auto" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@dbdea4b/assets/images/icons/module-systems/esm/icon32.png"></a> ES Modules (ESM): ```js import { ipv4, ipv6, mac } from 'generate-ip' ``` #### <a href="#-commonjs-cjs"><img height=13 width="auto" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@dbdea4b/assets/images/icons/module-systems/cjs/icon32.png"></a> CommonJS (CJS): ```js const { ipv4, ipv6, mac } = require('generate-ip') ``` #### [<img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/chrome/icon16.png" title="Chrome">][web-usage][<img height=13.5 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/edge/icon16.png" title="Edge">][web-usage][<img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/firefox/icon16.png" title="Firefox">][web-usage][<img height=14 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/safari/icon16.png" title="Safari">][web-usage][<img height=13 src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@c226de5/assets/images/icons/browsers/qq/3d/icon-32x33.png" title="QQ Browser">][web-usage] Web: [web-usage]: #-web ```html <script src="https://cdn.jsdelivr.net/npm/generate-ip@2/dist/generate-ip.min.js"></script> ``` #### [<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/tampermonkey/icon28.png" title="Tampermonkey">][greasemonkey-usage][<img height="15" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/violentmonkey/icon25.png" title="Violentmonkey">][greasemonkey-usage][<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/scriptcat/icon32.png" title="ScriptCat">][greasemonkey-usage][<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/orangemonkey/icon16.png" title="OrangeMonkey">][greasemonkey-usage][<img height="14" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/stay/icon32.png" title="Stay">][greasemonkey-usage][<img height="13" src="https://cdn.jsdelivr.net/gh/adamlui/userscripts@2793398/assets/images/icons/userscript-managers/userscripts/icon32.png" title="Userscripts">][greasemonkey-usage] Greasemonkey: [greasemonkey-usage]: #-greasemonkey-1 ```js ... // @require https://cdn.jsdelivr.net/npm/generate-ip@2/dist/generate-ip.min.js // ==/UserScript== ... ``` <br><a href="https://github.com/sponsors/KudoAI"><img src="https://cdn.jsdelivr.net/gh/adamlui/ai-web-extensions@e53b001/assets/images/banners/sponsor/$10/banner1660x260.png"></a> <hr> <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## 📋 API usage ### `ipv4` methods 💡 Use the `ipv4` methods to [**generate**](#ipv4generateoptions) and [**validate**](#ipv4validateaddress-options) IPv4 addresses. # #### `ipv4.generate([options])` Generates **one** IPv4 address if `qty` option is not given, returning a string: ```js const ip = ipv4.generate() console.log(ip) // sample output: '36.42.224.208' ``` ...or **multiple** IPv4 addresses if `qty` option is given, returning an array of strings: ```js const ips = ipv4.generate({ qty: 3 }) console.log(ips) /* sample output: ipv4.generate() » Generating IPv4 addresses... ipv4.generate() » IPv4 addresses generated! ipv4.generate() » 194.84.176.172, 192.186.53.120, 50.191.111.87 [ '194.84.176.172', '192.186.53.120', '50.191.111.87' ] */ ``` Available options: Name | Type | Description | Default Value -------------|---------|----------------------------------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` `qty` | Integer | Number of IP addresses to generate. | `1` `sequential` | Boolean | Generate addresses in sequence. | `false` `network` | String | Starting network address (required for sequential mode). | `null` # #### `ipv4.validate(address[, options])` Checks if given `address` is a valid IPv4 address: ```js const ipIsValid = ipv4.validate('36.42.224.208') console.log(ipIsValid) /* outputs: ipv4.validate() » Validating 36.42.224.208... ipv4.validate() » IP is valid IPv4 address! true */ ``` Available options (passed as object properties): Name | Type | Description | Default Value ------------|---------|-----------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` # ### `ipv6` methods 💡 Use the `ipv6` methods to [**generate**](#ipv6generateoptions), [**format**](#ipv6formatipv6address-options), and [**validate**](#ipv6validateaddress-options) IPv6 addresses. # #### `ipv6.generate([options])` Generates **one** IPv6 address if `qty` option is not given, returning a string: ```js const ip = ipv6.generate() console.log(ip) // sample output: '1379:6748:810c:5e16:b6c9:ae2:939f:8f2a' ``` ...or **multiple** IPv6 addresses if `qty` option is given, returning an array of strings: ```js const ips = ipv4.generate({ qty: 5 }) console.log(ips) /* sample output: ipv6.generate() » Generating IPv6 addresses... ipv6.generate() » IPv6 addresses generated! ipv6.generate() » 8218:19b9:7709:4282:65e1:7ee:319e:32ef, e940:754d:ae46:ae18:94dd:b43c:583:68c2, b570:b4f8:68f:62e2:99cb:ad0f:6237:9d51, 98a7:f4e5:2f4e:8a2d:56bb:dc28:f94a:46a8, ca59:590a:9b6c:ea25:94fa:37d6:9bac:7ff6 [ '8218:19b9:7709:4282:65e1:7ee:319e:32ef', 'e940:754d:ae46:ae18:94dd:b43c:583:68c2', 'b570:b4f8:68f:62e2:99cb:ad0f:6237:9d51', '98a7:f4e5:2f4e:8a2d:56bb:dc28:f94a:46a8', 'ca59:590a:9b6c:ea25:94fa:37d6:9bac:7ff6' ] */ ``` Available options: Name | Type | Description | Default Value ---------------|---------|----------------------------------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` `qty` | Integer | Number of IP addresses to generate. | `1` `leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false` `doubleColon` | Boolean | Replace series of zeros w/ `::` | `true` `sequential` | Boolean | Generate addresses in sequence. | `false` `network` | String | Starting network address (required for sequential mode). | `null` # #### `ipv6.format(ipv6address[, options])` Formats an IPv6 address according to `options` passed, returning a string: ```js const ipv6address = '0d::ffff:192.1.56.10/96', formattedAddress = ipv6.format(ipv6address, { leadingZeros: true, doubleColon: false }) console.log(formattedAddress) /* outputs: ipv6.format() » Expanding '::' into zero series... ipv6.format() » Adding leading zeros... ipv6.format() » IP formatted successfully! ipv6.format() » 000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96 '000d:0000:0000:0000:0000:0000:ffff:192.1.56.10/96' */ ``` Available options: Name | Type | Description | Default Value ---------------|---------|--------------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` `leadingZeros` | Boolean | Include leading zeros in hex pieces. | `false` `doubleColon` | Boolean | Replace series of zeros w/ `::` | `true` # #### `ipv6.validate(address[, options])` Checks if given `address` is a valid IPv6 address: ```js const ipIsValid = ipv6.validate('0:0:0:0:0:ffff:192.1.56.10/96') console.log(ipIsValid) /* outputs: ipv6.validate() » Validating 0:0:0:0:0:ffff:192.1.56.10/96... ipv6.validate() » IP is valid IPv6 address! true */ ``` Available options (passed as object properties): Name | Type | Description | Default Value ------------|---------|-----------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` # ### `mac` methods 💡 Use the `mac` methods to [**generate**](#macgenerateoptions) and [**validate**](#macvalidateaddress-options) MAC addresses. # #### `mac.generate([options])` Generates **one** MAC address if `qty` option is not given, returning a string: ```js const macAddress = mac.generate() console.log(macAddress) // sample output: '1d:3a:af:21:b1:8c' ``` ...or **multiple** MAC addresses if `qty` option is given, returning an array of strings: ```js const macAddresses = mac.generate({ qty: 2 }) console.log(macAddresses) /* sample output: mac.generate() » Generating MAC addresses... mac.generate() » MAC addresses generated! mac.generate() » 1d:3a:af:21:b1:8c, af:fb:6f:b6:1b:8a [ '1d:3a:af:21:b1:8c', 'af:fb:6f:b6:1b:8a' ] */ ``` Available options: Name | Type | Description | Default Value ---------------|---------|----------------------------------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` `qty` | Integer | Number of IP addresses to generate. | `1` `sequential` | Boolean | Generate addresses in sequence. | `false` `network` | String | Starting network address (required for sequential mode). | `null` # #### `mac.validate(address[, options])` Checks if given `address` is a valid MAC address: ```js const addressIsValid = mac.validate('1d:3a:af:21:b1:8c') console.log(addressIsValid) /* outputs: mac.validate() » Validating 1d:3a:af:21:b1:8c... mac.validate() » Address is valid MAC address! true */ ``` Available options (passed as object properties): Name | Type | Description | Default Value ------------|---------|-----------------------------------|--------------- `verbose` | Boolean | Show logging in console/terminal. | `true` <br> <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## 💻 Command line usage **generate-ip** can also be used directly from the command line. The basic command is: ``` $ generate-ip ``` Sample output: <img src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/generate-ip/assets/images/screenshots/cli/generate-ip-cmd-output.png"> 📝 **Note:** To generate other address types, type `generate-ipv6` or `generate-mac` # ### Command line options ``` Parameter options: --qty=n Generate n IP address(es). --ui-lang="code" ISO 639-1 code of language to display UI in. --config="path/to/file" Load custom config file. --network="address" Starting network address (required for sequential mode). Boolean options: -6, --ipv6 Generate IPv6 address. -m, --mac Generate MAC address. -s, --sequential Generate addresses in sequence. -q, --quiet Suppress all logging except errors. Commands: -i, --init Create config file (in project root). -h, --help Display help screen. -v, --version Show version number. --stats Show npm stats. --debug [targetKey] Show debug logs. ``` # ### Configuration file **generate-ip** can be customized using a `generate-ip.config.mjs` or `generate-ip.config.js` placed in your project root. Example defaults: ```js export default { qty: 5, // # of IPs to generate ipv6mode: false, // Generate IPv6 address(es) macMode: false, // Generate MAC address(es) quietMode: true // Suppress all logging except errors } ``` 💡 Run `generate-ip init` to generate a template `generate-ip.config.mjs` in your project root. <br> <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## 🏛️ MIT License **Copyright © 2024–2026 [Adam Lui](https://github.com/adamlui) & contributors.** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. <br> <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> ## 🛠️ Related utilities ### [🔒 generate-pw](https://github.com/adamlui/js-utils/tree/main/generate-pw/#readme) &nbsp;<a href="https://github.com/toolleeo/cli-apps/#password-managers"><img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/badges/awesome/badge.svg"></a> > Randomly generate, strengthen, and validate cryptographically-secure passwords. <br>[Install](https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/#-installation) / [Readme](https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/#readme) / [API usage](https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/#-api-usage) / [CLI usage](https://github.com/adamlui/js-utils/tree/main/generate-pw/docs/#-command-line-usage) / [Discuss](https://github.com/adamlui/js-utils/discussions) ### <picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/geolocate/assets/images/icons/wire-globe/white/icon32.png"><img height=22 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/geolocate/assets/images/icons/wire-globe/black/icon32.png"></picture> [geolocate](https://github.com/adamlui/js-utils/tree/main/geolocate/#readme) &nbsp;<a href="https://github.com/toolleeo/cli-apps/#networking"><img height=18 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/badges/awesome/badge.svg"></a> > Fetch IP geolocation data from the CLI. <br>[Install](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#-installation) / [Readme](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#readme) / [CLI usage](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#-command-line-usage) / [API usage](https://github.com/adamlui/js-utils/tree/main/geolocate/docs/#-api-usage) / [Discuss](https://github.com/adamlui/js-utils/discussions) <br> <img height=6px width="100%" src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@6b0d399/assets/images/separators/aqua-gradient.png"> [<picture><source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/adamlui/js-utils@21bf981/assets/images/icons/home/white/icon32x27.png"><img height=13 src="https://cdn.jsdelivr.net/gh/adamlui/js-utils@21bf981/assets/images/icons/home/dark-gray/icon32x27.png"></picture>][js-utils] [**More JavaScript utilities**][js-utils] / <a href="https://github.com/adamlui/js-utils/discussions">Discuss</a> / <a href="https://github.com/adamlui/js-utils/issues">Report bug</a> / <a href="mailto:security@tidelift.com">Report vulnerability</a> / <a href="#top">Back to top ↑</a> [js-utils]: https://github.com/adamlui/js-utils/#readme