Giter Site home page Giter Site logo

default-gateway's Introduction

default-gateway

Obtains the machine's default gateway through exec calls to OS routing ints.

  • On Linux and Android, the ip command must be available (usually provided by the iproute2 package).
  • On Unix (and macOS), the netstat command must be available.
  • On Windows, wmic must be available.
  • On IBM i, the db2util command must be available (provided by the db2util package).

Usage

import {gateway4async, gateway4sync, gateway6async, gateway6sync} from "default-gateway";

const {gateway, version, int} = await gateway4async();
// gateway = '1.2.3.4', version = 4, int = 'en1'

const {gateway, version, int} = await gateway6async();
// gateway = '2001:db8::1', version = 6,int = 'en2'

const {gateway, version, int} = gateway4sync();
// gateway = '1.2.3.4', version = 4, int = 'en1'

const {gateway, version, int} = gateway6sync();
// gateway = '2001:db8::1', version = 6, int = 'en2'

API

gateway4async()

gateway6async()

gateway4sync()

gateway6sync()

Returns: result Object

  • gateway String: The IP address of the default gateway.
  • version Number: The IP address version of gateway.
  • int String: The name of the interface. On Windows, this is the network adapter name.

The gateway property will always be defined on success, while int can be null if it cannot be determined. All methods reject/throw on unexpected conditions.

License

© silverwind, distributed under BSD licence

default-gateway's People

Contributors

bensjoberg avatar eycorsican avatar karli-sjoberg avatar lopopolo avatar penggy avatar shellscape avatar silverwind avatar sindresorhus avatar theprez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

default-gateway's Issues

node version broken

{
"name": "default-gateway",
  "version": "5.0.4",
  "description": "Get the default network gateway, cross-platform.",
  "author": "silverwind <[email protected]>",
  "repository": "silverwind/default-gateway",
  "license": "BSD-2-Clause",
  "scripts": {
    "test": "eslint *.js && node --pending-deprecation --trace-deprecation --throw-deprecation --trace-warnings test.js"
  },
  "engines": {
    "node": ">=8"
  },
  "dependencies": {
    "execa": "^3.0.0"
  }
}

execa requires: "node": "^8.12.0 || >=9.7.0", but in your package node engines are "node": ">=8".
However, that does not match, please check it. Thanks.

idbconnector dependency

When i install default-gateway 3.1.0 console warns about:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\idb-connector):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"aix","arch":"any"} (current: {"os":"win32","arch":"x64"})

and then, when i try to install deps with npm ci it starts to install idb-connector. And result is:

> [email protected] install C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector
> node-pre-gyp install --fallback-to-build

node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using [email protected]
node-pre-gyp info using [email protected] | win32 | x64
node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp info check checked for "C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\lib\binding\Release\node-v64-ibmi-x64\db2ia.node" (not found)
node-pre-gyp http GET https://github.com/IBM/nodejs-idb-connector/releases/download/1.1.8/db2ia-v1.1.8-node-v64-ibmi-x64.tar.gz
node-pre-gyp http 404 https://github.com/IBM/nodejs-idb-connector/releases/download/1.1.8/db2ia-v1.1.8-node-v64-ibmi-x64.tar.gz
node-pre-gyp WARN Tried to download(404): https://github.com/IBM/nodejs-idb-connector/releases/download/1.1.8/db2ia-v1.1.8-node-v64-ibmi-x64.tar.gz
node-pre-gyp WARN Pre-built binaries not found for [email protected] and [email protected] (node-v64 ABI, unknown) (falling back to source compile with node-gyp)
node-pre-gyp http 404 status code downloading tarball https://github.com/IBM/nodejs-idb-connector/releases/download/1.1.8/db2ia-v1.1.8-node-v64-ibmi-x64.tar.gz
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info ok
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Python27\python.exe
gyp info spawn args [ 'C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=2015',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\user\\Desktop\\aa\\electron-quick-start\\node_modules\\idb-connector\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\user\\.node-gyp\\10.15.1\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\user\\.node-gyp\\10.15.1',
gyp info spawn args   '-Dnode_gyp_dir=C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=C:\\Users\\user\\.node-gyp\\10.15.1\\<(target_arch)\\node.lib',
gyp info spawn args   '-Dmodule_root_dir=C:\\Users\\user\\Desktop\\aa\\electron-quick-start\\node_modules\\idb-connector',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'C:\\Users\\user\\Desktop\\aa\\electron-quick-start\\node_modules\\idb-connector\\build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info ok
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64' ]
  db2ia.cc
  dbconn.cc
  dbstmt.cc
  win_delay_load_hook.cc
c:\users\user\desktop\aa\electron-quick-start\node_modules\idb-connector\src\db2ia\dbconn.h(13): fatal error C1083: Cannot open include file: 'sqlcli.h': No such file or directory (compiling source file ..\src
\db2ia\db2ia.cc) [C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\build\db2ia.vcxproj]
c:\users\user\desktop\aa\electron-quick-start\node_modules\idb-connector\src\db2ia\dbconn.h(13): fatal error C1083: Cannot open include file: 'sqlcli.h': No such file or directory (compiling source file ..\src
\db2ia\dbconn.cc) [C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\build\db2ia.vcxproj]
c:\users\user\desktop\aa\electron-quick-start\node_modules\idb-connector\src\db2ia\dbconn.h(13): fatal error C1083: Cannot open include file: 'sqlcli.h': No such file or directory (compiling source file ..\src
\db2ia\dbstmt.cc) [C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\build\db2ia.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\user\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "build" "--fallback-to-build" "--module=C:\\Users\\user\\Desktop\\aa\\electron-quick-start\\node_modules\\idb-connector\\lib\\binding\\Release\\node-v64-ibmi-x64\\db2ia.node" "--module_name=db2ia" "--module_path=C:\\Users\\user\\Desktop\\aa\\electron-quick-start\\node_modules\\idb-connector\\lib\\binding\\Release\\node-v64-ibmi-x64" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v64"
gyp ERR! cwd C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector
gyp ERR! node -v v10.15.1
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute 'C:\Program Files\nodejs\node.exe C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js build --fallback-to-build --module=C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\lib\binding\Release\node-v64-ibmi-x64\db2ia.node --module_name=db2ia --module_path=C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\lib\binding\Release\node-v64-ibmi-x64 --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\node_modules\node-pre-gyp\lib\util\compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:189:13)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:970:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
node-pre-gyp ERR! System Windows_NT 10.0.17134
node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\user\\Desktop\\aa\\electron-quick-start\\node_modules\\idb-connector\\node_modules\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector
node-pre-gyp ERR! node -v v10.15.1
node-pre-gyp ERR! node-pre-gyp -v v0.11.0
node-pre-gyp ERR! not ok
Failed to execute 'C:\Program Files\nodejs\node.exe C:\Users\user\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js build --fallback-to-build --module=C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\lib\binding\Release\node-v64-ibmi-x64\db2ia.node --module_name=db2ia --module_path=C:\Users\user\Desktop\aa\electron-quick-start\node_modules\idb-connector\lib\binding\Release\node-v64-ibmi-x64 --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)

With default-gateway v3.0.1 all is ok.

Windows: The gateway is not from external card

When a computer has virtual network adapters with a gateway, it's selecting the first that finds, but not necessarily is the gateway of the external card.

Example: after run wmic path Win32_NetworkAdapterConfiguration where IPEnabled=true get DefaultIPGateway,Index /format:table in windows I get this response:

3 
{"0.0.0.0"} 5 
{"192.168.1.1", "fe80::dafb:5eff:fe33:3489"} 6 
8

and after parse it the result is 0.0.0.0, but the gateway of the external card is 192.168.1.1

Intention to deprecate this module

I intend to deprecate this module soon as I'm not willing to maintain it further because it's just a wrong approach overall to parse command output.

Affected users should look for alternatives. For webpack-dev-server, see my recommendation.

Unsupported platform FreeBSD

npm ERR! notsup Unsupported platform for [email protected]: wanted {"os":"darwin,linux,win32","arch":"any"} (current: {"os":"freebsd","arch":"x64"})
npm ERR! notsup Valid OS: darwin,linux,win32
npm ERR! notsup Valid Arch: any
npm ERR! notsup Actual OS: freebsd
npm ERR! notsup Actual Arch: x64

A little funny seeing how it´s BSD licensed :)

/K

"interface" is a strict mode reserved keyword

according to this link,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#paving_the_way_for_future_ecmascript_versions
interface is a reserved word to use in 'use strict';.

I ran into the node error with default example of destructuring
const {gateway, interface} = await defaultGateway.v4();
and had to go the const interface_v4 = gtw.interface way...

I would propose adding an alias of smth like interf to keep it backwards-compat. Alas it's not that big of a deal to skip the destructive sugar once.
Maybe a warning would be nice as well.

Question about docs

Hi, the docs say for the sync method that it should be:

const result = defaultGateway.v4().sync();
// result = {gateway: '1.2.3.4', interface: 'en1'}

const result = defaultGateway.v6().sync();
// result = {gateway: '2001:db8::1', interface: 'en2'}

But looking at the source, it is seems like it's supposed to be:

const result = defaultGateway.v4.sync();
// result = {gateway: '1.2.3.4', interface: 'en1'}

const result = defaultGateway.v6.sync();
// result = {gateway: '2001:db8::1', interface: 'en2'}

Is that right?

Code improvements

child_process.execFile() should be used over .exec() as it's both faster and safer. I think we should also use either pify or execa to make the code more readable.

Can't get IP Address on Windows 11

WMIC was removed from Windows 10/11.

image

There's no other way to implement this feature without WMIC, or with the new 'Powershell for WMI'?

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.