improved default network interface
This commit is contained in:
parent
528a843588
commit
09e4518bb9
@ -66,6 +66,7 @@ Here all changes more detailed:
|
|||||||
|
|
||||||
New Functions
|
New Functions
|
||||||
|
|
||||||
|
- `networkInterfaceDefault`: returns default network interface (new in version 3.4)
|
||||||
- `processes`: now returns also a process list with all process details (new in version 3.3)
|
- `processes`: now returns also a process list with all process details (new in version 3.3)
|
||||||
- `battery`: retrieves battery status and charging level (new in version 3.2)
|
- `battery`: retrieves battery status and charging level (new in version 3.2)
|
||||||
- `dockerContainers`: returns a list of all docker containers (new in version 3.1)
|
- `dockerContainers`: returns a list of all docker containers (new in version 3.1)
|
||||||
@ -239,6 +240,7 @@ This library is splitted in several sections:
|
|||||||
| - [0].ip4 | X | X | ip4 address |
|
| - [0].ip4 | X | X | ip4 address |
|
||||||
| - [0].ip6 | X | X | ip6 address |
|
| - [0].ip6 | X | X | ip6 address |
|
||||||
| - [0].internal | X | X | true if internal interface |
|
| - [0].internal | X | X | true if internal interface |
|
||||||
|
| si.networkInterfaceDefault(cb) | X | X | get name of default network interface |
|
||||||
| si.networkStats(iface,cb) | X | X | current network stats of given interface<br>iface parameter is optional<br>defaults to first external network interface|
|
| si.networkStats(iface,cb) | X | X | current network stats of given interface<br>iface parameter is optional<br>defaults to first external network interface|
|
||||||
| - iface | X | X | interface |
|
| - iface | X | X | interface |
|
||||||
| - operstate | X | X | up / down |
|
| - operstate | X | X | up / down |
|
||||||
@ -373,6 +375,7 @@ I am happy to discuss any comments and suggestions. Please feel free to contact
|
|||||||
|
|
||||||
| Version | Date | Comment |
|
| Version | Date | Comment |
|
||||||
| -------------- | -------------- | -------- |
|
| -------------- | -------------- | -------- |
|
||||||
|
| 3.4.2 | 2016-09-01 | improved default network interface |
|
||||||
| 3.4.1 | 2016-08-30 | updated docs |
|
| 3.4.1 | 2016-08-30 | updated docs |
|
||||||
| 3.4.0 | 2016-08-30 | rewritten processes current cpu usage |
|
| 3.4.0 | 2016-08-30 | rewritten processes current cpu usage |
|
||||||
| 3.3.0 | 2016-08-24 | process list added to processes |
|
| 3.3.0 | 2016-08-24 | process list added to processes |
|
||||||
|
|||||||
52
lib/index.js
52
lib/index.js
@ -80,6 +80,7 @@
|
|||||||
// --------------------------------
|
// --------------------------------
|
||||||
//
|
//
|
||||||
// version date comment
|
// version date comment
|
||||||
|
// 3.4.2 2016-09-01 improved default network interface
|
||||||
// 3.4.1 2016-08-30 updated docs
|
// 3.4.1 2016-08-30 updated docs
|
||||||
// 3.4.0 2016-08-30 rewritten current process cpu usage (linux)
|
// 3.4.0 2016-08-30 rewritten current process cpu usage (linux)
|
||||||
// 3.3.0 2016-08-24 added process list
|
// 3.3.0 2016-08-24 added process list
|
||||||
@ -115,6 +116,7 @@
|
|||||||
|
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
, exec = require('child_process').exec
|
, exec = require('child_process').exec
|
||||||
|
, execSync = require('child_process').execSync
|
||||||
, fs = require('fs')
|
, fs = require('fs')
|
||||||
, lib_version = require('../package.json').version;
|
, lib_version = require('../package.json').version;
|
||||||
|
|
||||||
@ -1162,23 +1164,51 @@ exports.disksIO = disksIO;
|
|||||||
// 8. Network
|
// 8. Network
|
||||||
// ----------------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
function getFirstExternalNetworkInterface() {
|
function getDefaultNetworkInterface() {
|
||||||
|
|
||||||
let ifacename = '';
|
let ifacename = '';
|
||||||
let ifaces = os.networkInterfaces();
|
let cmd = (_linux ? "route | grep default | awk '{print $8}'" : "route get 0.0.0.0 2>/dev/null | grep interface: | awk '{print $2}'");
|
||||||
|
let result = execSync(cmd);
|
||||||
|
ifacename = result.toString().split('\n')[0];
|
||||||
|
|
||||||
for (let dev in ifaces) {
|
if (!ifacename) { // fallback - "first" external interface
|
||||||
if (ifaces.hasOwnProperty(dev)) {
|
const sortObject = o => Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
|
||||||
ifaces[dev].forEach(function (details) {
|
|
||||||
if (details && details.internal == false) {
|
let ifaces = sortObject(os.networkInterfaces());
|
||||||
ifacename = ifacename || dev;
|
|
||||||
}
|
for (let dev in ifaces) {
|
||||||
})
|
if (ifaces.hasOwnProperty(dev)) {
|
||||||
|
ifaces[dev].forEach(function (details) {
|
||||||
|
if (details && details.internal == false) {
|
||||||
|
ifacename = ifacename || dev;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (ifacename) _default_iface = ifacename;
|
||||||
return ifacename;
|
return ifacename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function networkInterfaceDefault(callback) {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
process.nextTick(() => {
|
||||||
|
if (_windows) {
|
||||||
|
let error = new Error(NOT_SUPPORTED);
|
||||||
|
if (callback) { callback(NOT_SUPPORTED) }
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = getDefaultNetworkInterface();
|
||||||
|
if (callback) { callback(result) }
|
||||||
|
resolve(result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.networkInterfaceDefault = networkInterfaceDefault;
|
||||||
|
|
||||||
// --------------------------
|
// --------------------------
|
||||||
// NET - interfaces
|
// NET - interfaces
|
||||||
|
|
||||||
@ -1258,7 +1288,7 @@ function networkStats(iface, callback) {
|
|||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
_default_iface = _default_iface || getFirstExternalNetworkInterface();
|
_default_iface = _default_iface || getDefaultNetworkInterface();
|
||||||
iface = iface || _default_iface; // (_darwin ? 'en0' : 'eth0');
|
iface = iface || _default_iface; // (_darwin ? 'en0' : 'eth0');
|
||||||
|
|
||||||
let result = {
|
let result = {
|
||||||
@ -2647,7 +2677,7 @@ function getDynamicData(srv, iface, callback) {
|
|||||||
reject(error);
|
reject(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
iface = iface || getFirstExternalNetworkInterface();
|
iface = iface || getDefaultNetworkInterface();
|
||||||
srv = srv || '';
|
srv = srv || '';
|
||||||
|
|
||||||
// use closure to track ƒ completion
|
// use closure to track ƒ completion
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user