From 3c3017935e0253bd1caa1077b0b745811f9b5d48 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Fri, 31 Jan 2020 17:33:58 +0100 Subject: [PATCH] networkGatewayDefault() fixed windows 7 issue --- CHANGELOG.md | 3 +++ docs/history.html | 5 ++++ docs/index.html | 4 +-- docs/processes.html | 2 +- lib/network.js | 64 +++++++++++++++++++++++++++++---------------- 5 files changed, 52 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14fa8d6..72b8522 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,9 @@ For major (breaking) changes - version 3 and 2 see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 4.21.1 | 2020-01-31 | `networkGatewayDefault()` fixed windows 7 issue | +| 4.21.0 | 2020-01-27 | `npx` compatibility | +| 4.20.1 | 2020-01-26 | `battery()` code refactoring, cleanup, updated docs | | 4.20.1 | 2020-01-26 | `battery()` code refactoring, cleanup, updated docs | | 4.20.0 | 2020-01-25 | `battery()` added designcapacity, voltage, unit | | 4.19.4 | 2020-01-24 | `mem()` prevent log messages, `memgetDefaultNetworkInterface()` catch errors | diff --git a/docs/history.html b/docs/history.html index 6b103de..8eab5a7 100644 --- a/docs/history.html +++ b/docs/history.html @@ -83,6 +83,11 @@ + + 4.21.1 + 2020-01-31 + networkGatewayDefault() fixed windows 7 issue + 4.21.0 2020-01-27 diff --git a/docs/index.html b/docs/index.html index 61184c3..f4d271d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -168,7 +168,7 @@
systeminformation
-
Current Version: 4.21.0
+
Current Version: 4.21.1
@@ -206,7 +206,7 @@
Downloads last month
-
242
+
244
Dependends
diff --git a/docs/processes.html b/docs/processes.html index 4f3150a..d5c734c 100644 --- a/docs/processes.html +++ b/docs/processes.html @@ -564,7 +564,7 @@ It is determined by calculating the difference of cpu ticks between two calls of the function.

The first time you are calling one of this functions, you will get the load since cpu uptime. - The second time, you should then get statistics based cpu ticks between the two calls ...

+ The second time, you should then get statistics based on cpu ticks between the two calls ...

So basically, your code should look like this:

diff --git a/lib/network.js b/lib/network.js index f25556f..0ebc31d 100644 --- a/lib/network.js +++ b/lib/network.js @@ -1402,32 +1402,50 @@ function networkGatewayDefault(callback) { } if (_windows) { try { - util.powerShell('Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.Destination -eq \'0.0.0.0\' -and $_.Mask -eq \'0.0.0.0\' }') - .then(data => { - let lines = data.toString().split('\r\n'); - if (lines.length > 1 && !result) { - result = util.getValue(lines, 'NextHop'); - // result = lines && lines[0] ? lines[0] : ''; - if (callback) { - callback(result); + exec('netstat -r', util.execOptsWin, function (error, stdout) { + const lines = stdout.toString().split(os.EOL); + lines.forEach(line => { + line = line.replace(/\s+/g, ' ').trim(); + if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) { + const parts = line.split(' '); + if (parts.length >= 5 && (parts[parts.length - 3]).indexOf('.') > -1) { + result = parts[parts.length - 3]; } - resolve(result); - } else { - exec('ipconfig', util.execOptsWin, function (error, stdout) { - let lines = stdout.toString().split('\r\n'); - lines.forEach(function (line) { - line = line.trim().replace(/\. /g, ''); - line = line.trim().replace(/ +/g, ''); - const parts = line.split(':'); - if (parts[0].toLowerCase().startsWith('standardgate') && parts[1]) { - result = parts[1]; - } - }); - if (callback) { callback(result); } - resolve(result); - }); } }); + if (!result) { + util.powerShell('Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.Destination -eq \'0.0.0.0\' -and $_.Mask -eq \'0.0.0.0\' }') + .then(data => { + let lines = data.toString().split('\r\n'); + if (lines.length > 1 && !result) { + result = util.getValue(lines, 'NextHop'); + if (callback) { + callback(result); + } + resolve(result); + // } else { + // exec('ipconfig', util.execOptsWin, function (error, stdout) { + // let lines = stdout.toString().split('\r\n'); + // lines.forEach(function (line) { + // line = line.trim().replace(/\. /g, ''); + // line = line.trim().replace(/ +/g, ''); + // const parts = line.split(':'); + // if ((parts[0].toLowerCase().startsWith('standardgate') || parts[0].toLowerCase().indexOf('gateway') > -1 || parts[0].toLowerCase().indexOf('enlace') > -1) && parts[1]) { + // result = parts[1]; + // } + // }); + // if (callback) { callback(result); } + // resolve(result); + // }); + } + }); + } else { + if (callback) { + callback(result); + } + resolve(result); + } + }); } catch (e) { if (callback) { callback(result); } resolve(result);