UNPKG

1.81 kBMarkdownView Raw
1# default-gateway
2[![](https://img.shields.io/npm/v/default-gateway.svg?style=flat)](https://www.npmjs.org/package/default-gateway) [![](https://img.shields.io/npm/dm/default-gateway.svg)](https://www.npmjs.org/package/default-gateway)
3
4Obtains the machine's default gateway through `exec` calls to OS routing interfaces.
5
6- On Linux and Android, the `ip` command must be available (usually provided by the `iproute2` package).
7- On Windows, `wmic` must be available.
8- On IBM i, the `db2util` command must be available (provided by the `db2util` package).
9- On Unix (and macOS), the `netstat` command must be available.
10
11## Installation
12
13```
14$ npm i default-gateway
15```
16
17## Example
18
19```js
20const defaultGateway = require('default-gateway');
21
22const {gateway, interface} = await defaultGateway.v4();
23// gateway = '1.2.3.4', interface = 'en1'
24
25const {gateway, interface} = await defaultGateway.v6();
26// gateway = '2001:db8::1', interface = 'en2'
27
28const {gateway, interface} = defaultGateway.v4.sync();
29// gateway = '1.2.3.4', interface = 'en1'
30
31const {gateway, interface} = defaultGateway.v6.sync();
32// gateway = '2001:db8::1', interface = 'en2'
33```
34
35## API
36### defaultGateway.v4()
37### defaultGateway.v6()
38### defaultGateway.v4.sync()
39### defaultGateway.v6.sync()
40
41Returns: `result` *Object*
42 - `gateway`: The IP address of the default gateway.
43 - `interface`: The name of the interface. On Windows, this is the network adapter name.
44
45The `.v{4,6}()` methods return a Promise while the `.v{4,6}.sync()` variants will return the result synchronously.
46
47The `gateway` property will always be defined on success, while `interface` can be `null` if it cannot be determined. All methods reject/throw on unexpected conditions.
48
49## License
50
51© [silverwind](https://github.com/silverwind), distributed under BSD licence