npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generate-ip

v2.4.2

Published

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

Downloads

1,251

Readme

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/[email protected]/dist/generate-ip.min.js"></script>

ES6:

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

Greasemonkey

...
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/generate-ip.min.js
// ==/UserScript==

// Your code here...

📝 Note: To always import the latest version (not recommended in production!) remove the @2.4.2 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 other address types, type generate-ipv6 or generate-mac

Command line options

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

Boolean options:
 -6, --ipv6                  Generate IPv6 address.
 -m, --mac                   Generate MAC address.
 -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

🔒 generate-pw  

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

geolocate  

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

More JavaScript utilities / Discuss / Back to top ↑