Skip to content

Latest commit

 

History

History

generate-ip

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

generate-ip

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


💡 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

⚡ 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

🔌 Importing the APIs

Node.js

ECMAScript*:

import { ipv4, ipv6, mac } from 'generate-ip';

CommonJS:

const { ipv4, ipv6, mac } = require('generate-ip');
*Node.js version 14 or higher required

Web

<> HTML script tag:

<script src="https://cdn.jsdelivr.net/npm/generate-ip@2.3.1/dist/generate-ip.min.js"></script>

ES6:

(async () => {
    await import('https://cdn.jsdelivr.net/npm/generate-ip@2.3.1/dist/generate-ip.min.js');
    // Your code here...
})();

Greasemonkey

...
// @require https://cdn.jsdelivr.net/npm/generate-ip@2.3.1/dist/generate-ip.min.js
// ==/UserScript==

// Your code here...

📝 Note: To always import the latest version (not recommended in production!) remove the @2.3.1 version tag from the jsDelivr URL: https://cdn.jsdelivr.net/npm/generate-ip/dist/generate-ip.min.js


📋 API usage

ipv4 methods

💡 Use the ipv4 methods to generate and validate IPv4 addresses.

ipv4.generate([options])

Generates one IPv4 address if qty option is not given, returning a string:

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:

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

ipv4.validate(address[, options])

Checks if given address is a valid IPv4 address:

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, format, and validate IPv6 addresses.

ipv6.generate([options])

Generates one IPv6 address if qty option is not given, returning a string:

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:

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

ipv6.format(ipv6address[, options])

Formats an IPv6 address according to options passed, returning a string:

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:

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 and validate MAC addresses.

mac.generate([options])

Generates one MAC address if qty option is not given, returning a string:

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:

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

mac.validate(address[, options])

Checks if given address is a valid MAC address:

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

💻 Command line usage

When installed globally, generate-ip can also be used from the command line. The basic command is:

$ generate-ip

Sample output:

📝 Note: To generate multiple IP addresses, pass --qty=n where n is the number of IPs to generate.

Command line options

Parameter options:
 --qty=n                     Generate n IP address(es).

Boolean options:
 -q, --quiet                 Suppress all logging except errors.

Info commands:
 -h, --help                  Display help screen.
 -v, --version               Show version number.

🏛️ MIT License

Copyright © 2024 Adam Lui & 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.


🛠️ Related utilities

Randomly generate, strengthen, and validate cryptographically-secure passwords.
Install / Readme / API usage / CLI usage / Discuss

Fetch IP geolocation data from the CLI.
Install / Readme / CLI usage / API usage / Discuss


More JavaScript utilities / Discuss / Back to top ↑