You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.8 KiB

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