1 |
3a515b92
|
cagy
|
# default-gateway
|
2 |
|
|
[](https://www.npmjs.org/package/default-gateway) [](https://www.npmjs.org/package/default-gateway) [](https://travis-ci.org/silverwind/default-gateway)
|
3 |
|
|
|
4 |
|
|
> Get the default network gateway, cross-platform.
|
5 |
|
|
|
6 |
|
|
Obtains the machine's default gateway through `exec` calls to OS routing interfaces. On Linux and Android, the `ip` command must be available (usually provided by the `iproute2` package). On IBM i, the `db2util` command must be available (provided by the `db2util` package).
|
7 |
|
|
|
8 |
|
|
## Installation
|
9 |
|
|
|
10 |
|
|
```
|
11 |
|
|
$ npm install default-gateway
|
12 |
|
|
```
|
13 |
|
|
|
14 |
|
|
## Example
|
15 |
|
|
|
16 |
|
|
```js
|
17 |
|
|
const defaultGateway = require('default-gateway');
|
18 |
|
|
|
19 |
|
|
defaultGateway.v4().then(result => {
|
20 |
|
|
// result = {gateway: '1.2.3.4', interface: 'en1'}
|
21 |
|
|
});
|
22 |
|
|
|
23 |
|
|
defaultGateway.v6().then(result => {
|
24 |
|
|
// result = {gateway: '2001:db8::1', interface: 'en2'}
|
25 |
|
|
});
|
26 |
|
|
|
27 |
|
|
const result = defaultGateway.v4.sync();
|
28 |
|
|
// result = {gateway: '1.2.3.4', interface: 'en1'}
|
29 |
|
|
|
30 |
|
|
const result = defaultGateway.v6.sync();
|
31 |
|
|
// result = {gateway: '2001:db8::1', interface: 'en2'}
|
32 |
|
|
```
|
33 |
|
|
|
34 |
|
|
## API
|
35 |
|
|
### defaultGateway.v4()
|
36 |
|
|
### defaultGateway.v6()
|
37 |
|
|
### defaultGateway.v4.sync()
|
38 |
|
|
### defaultGateway.v6.sync()
|
39 |
|
|
|
40 |
|
|
Returns: `result` *Object*
|
41 |
|
|
- `gateway`: The IP address of the default gateway.
|
42 |
|
|
- `interface`: The name of the interface. On Windows, this is the network adapter name.
|
43 |
|
|
|
44 |
|
|
The `.v{4,6}()` methods return a Promise while the `.v{4,6}.sync()` variants will return the result synchronously.
|
45 |
|
|
|
46 |
|
|
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.
|
47 |
|
|
|
48 |
|
|
## License
|
49 |
|
|
|
50 |
|
|
© [silverwind](https://github.com/silverwind), distributed under BSD licence
|