e9027474b2597a9aee8248c8007d850fca6f7e563da9bd9fe810993e50479e74d4361b370d27a72b2d9283391a1842891a27ffd3485c08390195f0833e29e8 1.9 KB

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