Package Exports
- default-gateway
- default-gateway/win32
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (default-gateway) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
default-gateway
A Node.js module to get default gateway and default interface.
100% Pure JavaScript😽
This module depends on following commands by using chid_process provided by Node.js.
- win32
wmic path Win32_NetworkAdapterConfiguration get *wmic path Win32_NetworkAdapter get *
- darwin
netstat -rn -f inetnetstat -rn -f inet6
- linux
netstat -rn -A inetnetstat -rn -A inet6
Installation
npm install default-gatewayAPI
collect(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);
// names[0] is 'en4'
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 } ]Development
Test
npm testor
./node_modules/.bin/gulp testor
./node_modules/.bin/mochaLicense
The MIT License