Skip to content

Get at least one local IP that can be used to bind services to

License

Notifications You must be signed in to change notification settings

hschulz/get-primary-ip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Get primary IP

This package proivdes a way to get the primary IP address of the machine based on some platform specific commands.

This will work on most setups, but there are some edge cases where the primary IP address may not be detected correctly; especially on machines with multiple network interfaces that are all connected to separate gateways.

Currently only Windows and Linux are supported.

Installation

npm i @hschulz/get-primary-ip

Requirements

Windows

The package requires the wmic command to be available.

Further information can be found here and here.

Linux

The package requires the ip command to be available.

Further information can be found here.

Usage

The getPrimaryIp returns a promise that resolves to an array of at least one object that should contain the primary IP address of the machine.

Running the following code may return an object containing the primary IP address of the machine.

import { getPrimaryIp } from "@hschulz/get-primary-ip"

getPrimaryIp()
.then((result) => {
    // do something with the result
    console.log(result)
})
.catch((error) => console.error(error))

The result should contain at least one object with the following properties:

{
    mac: "00:00:00:00:00:00",
    name: "eth0",
    ips: [ "192.168.0.123" ],
    gateways: [ "192.168.0.1", "fe80::abcd:ef01:2345:f2bb" ]
}

License

See the LICENSE file for license rights and limitations (MIT).

About

Get at least one local IP that can be used to bind services to

Resources

License

Stars

Watchers

Forks

Packages