GithubHelp home page GithubHelp logo

7c / node-default-network Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mh61503891/node-default-network

0.0 1.0 0.0 25 KB

A Node.js module to get default gateway and default interface.

License: MIT License

CoffeeScript 98.99% JavaScript 1.01%

node-default-network's Introduction

default-network

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 inet
    • netstat -rn -f inet6
  • linux
    • netstat -rn -A inet
    • netstat -rn -A inet6

Installation

npm install default-network

API

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 route = require('default-network');
route.collect(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 route = require('default-network');
route.collect(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 test

or

./node_modules/.bin/gulp test

or

./node_modules/.bin/mocha

Author

Masayuki Higashino

License

The MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.