What is default-gateway?
The default-gateway npm package provides a simple and efficient way to find the default gateway of the local machine's network interfaces. It supports multiple operating systems, including Windows, macOS, and Linux, making it a versatile tool for network-related programming tasks. This package can be particularly useful for applications that need to interact with the network layer of the operating system, such as determining the best route for network traffic or configuring network settings programmatically.
What are default-gateway's main functionalities?
Finding the default gateway for IPv4
This feature allows you to asynchronously find the default IPv4 gateway of the local machine. The result is an object containing the gateway's IP address and the associated network interface.
const defaultGateway = require('default-gateway');
defaultGateway.v4().then(result => console.log(result));
Finding the default gateway for IPv6
Similar to the IPv4 feature, this allows for the asynchronous discovery of the default IPv6 gateway. The result includes the IPv6 gateway address and the network interface it is associated with.
const defaultGateway = require('default-gateway');
defaultGateway.v6().then(result => console.log(result));
Other packages similar to default-gateway
ip
The 'ip' package provides utilities for handling IP addresses, including the ability to parse and format them. While it doesn't offer direct functionality to find the default gateway, it complements 'default-gateway' by providing tools to work with the IP addresses that 'default-gateway' might return.
network
The 'network' package offers a broader range of network-related functionalities, including getting the user's IP address, gateway, and active network interfaces. It serves a similar purpose to 'default-gateway' but with additional features that might be useful for more comprehensive network programming tasks.
os
While not a third-party package but a core Node.js module, 'os' provides basic operating system-related utility functions. It includes methods to retrieve network interface details. However, it does not directly provide the default gateway information, making 'default-gateway' a necessary complement for specific gateway-related tasks.
default-gateway
Get the default network gateway and the default interface.
This module depends on following commands by using chid_process:
- win32
wmic path Win32_NetworkAdapterConfiguration get *
wmic path Win32_NetworkAdapter get *
- darwin
netstat -rn -f inet
netstat -rn -f inet6
- linux
netstat -rn -A inet
netstat -rn -A inet6
This module is based on previous work from mh61503891.
Installation
npm install default-gateway
API
defaultGateway(callback)
Asynchronously collects a object which contains interface names, families (IPv4 or IPv6), and addresses. The callback
gets two arguments (error, data)
.
Example 1
How to get default gateway and default interface.
An example code: example1.js
var defaultGateway = require('default-gateway');
defaultGateway(function(error, data) {
console.log(data);
});
An example output:
{ en4:
[ { family: 'IPv4', address: '192.168.1.1' },
{ family: 'IPv6', address: 'fe80::20b:a2ff:fede:2886%en4' } ],
en0:
[ { family: 'IPv4', address: '192.168.1.1' },
{ family: 'IPv6', address: 'fe80::20b:a2ff:fede:2886%en0' } ],
en8:
[ { family: 'IPv4', address: '192.168.1.1' },
{ family: 'IPv6', address: 'fe80::20b:a2ff:fede:2886%en8' } ] }
Example 2
How to get default interface from os.networkInterfaces()
.
An example code: example2.js
var os = require('os');
var defaultGateway = require('default-gateway');
defaultGateway(function(error, data) {
names = Object.keys(data);
console.log(os.networkInterfaces()[names[0]]);
});
An example output:
[ { address: 'fe80::6a5b:35ff:fe96:cc05',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 14,
internal: false },
{ address: '2001:a0ae:7c22:0:6a5b:35ff:fe96:cc05',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: false },
{ address: '2001:a0ae:7c22:0:a8d1:fef3:2917:cd87',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: false },
{ address: '192.168.1.10',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: false } ]
License
The MIT License