| 1 | # default-gateway
|
|---|
| 2 | [](https://www.npmjs.org/package/default-gateway) [](https://www.npmjs.org/package/default-gateway)
|
|---|
| 3 |
|
|---|
| 4 | Obtains 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
|
|---|
| 20 | const defaultGateway = require('default-gateway');
|
|---|
| 21 |
|
|---|
| 22 | const {gateway, interface} = await defaultGateway.v4();
|
|---|
| 23 | // gateway = '1.2.3.4', interface = 'en1'
|
|---|
| 24 |
|
|---|
| 25 | const {gateway, interface} = await defaultGateway.v6();
|
|---|
| 26 | // gateway = '2001:db8::1', interface = 'en2'
|
|---|
| 27 |
|
|---|
| 28 | const {gateway, interface} = defaultGateway.v4.sync();
|
|---|
| 29 | // gateway = '1.2.3.4', interface = 'en1'
|
|---|
| 30 |
|
|---|
| 31 | const {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 |
|
|---|
| 41 | Returns: `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 |
|
|---|
| 45 | The `.v{4,6}()` methods return a Promise while the `.v{4,6}.sync()` variants will return the result synchronously.
|
|---|
| 46 |
|
|---|
| 47 | The `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
|
|---|