From 3d61fe987ce01258d05459c311d8dd53d920a165 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Sun, 26 Apr 2020 18:51:01 +0200 Subject: [PATCH] getMacAddresses() fix added try catch --- CHANGELOG.md | 1 + docs/history.html | 5 +++ docs/index.html | 2 +- lib/network.js | 102 ++++++++++++++++++++++++++-------------------- 4 files changed, 64 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ae9784..49894d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ For major (breaking) changes - version 3 and 2 see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 4.23.8 | 2020-04-26 | `getMacAddresses()` fix added try catch | | 4.23.7 | 2020-04-26 | `getCpuCurrentSpeedSync()` workarround fix | | 4.23.6 | 2020-04-25 | `networkGatewayDefault()` bug fix no interfaces | | 4.23.5 | 2020-04-20 | updated docs | diff --git a/docs/history.html b/docs/history.html index 9ce68b4..9fbc1a7 100644 --- a/docs/history.html +++ b/docs/history.html @@ -83,6 +83,11 @@ + + 4.23.8 + 2020-04-2& + getMacAddresses() fix added try catch + 4.23.7 2020-04-2& diff --git a/docs/index.html b/docs/index.html index 4a94af0..b75c294 100644 --- a/docs/index.html +++ b/docs/index.html @@ -168,7 +168,7 @@
systeminformation
-
Current Version: 4.23.7
+
Current Version: 4.23.8
diff --git a/lib/network.js b/lib/network.js index e2f8e1d..fd1bf7e 100644 --- a/lib/network.js +++ b/lib/network.js @@ -64,10 +64,10 @@ function getDefaultNetworkInterface() { try { if (_windows) { // https://www.inetdaemon.com/tutorials/internet/ip/routing/default_route.shtml + let defaultIp = ''; const cmd = 'netstat -r'; const result = execSync(cmd); const lines = result.toString().split(os.EOL); - let defaultIp = ''; 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))) { @@ -126,46 +126,54 @@ function getMacAddresses() { pathToIp = ''; } } - const cmd = 'export LC_ALL=C; ' + ((pathToIp) ? pathToIp + ' link show up' : '/sbin/ifconfig') + '; unset LC_ALL'; - let res = execSync(cmd); - const lines = res.toString().split('\n'); - for (let i = 0; i < lines.length; i++) { - if (lines[i] && lines[i][0] !== ' ') { - if (pathToIp) { - let nextline = lines[i + 1].trim().split(' '); - if (nextline[0] === 'link/ether') { - iface = lines[i].split(' ')[1]; - iface = iface.slice(0, iface.length - 1); - mac = nextline[1]; + try { + const cmd = 'export LC_ALL=C; ' + ((pathToIp) ? pathToIp + ' link show up' : '/sbin/ifconfig') + '; unset LC_ALL'; + let res = execSync(cmd); + const lines = res.toString().split('\n'); + for (let i = 0; i < lines.length; i++) { + if (lines[i] && lines[i][0] !== ' ') { + if (pathToIp) { + let nextline = lines[i + 1].trim().split(' '); + if (nextline[0] === 'link/ether') { + iface = lines[i].split(' ')[1]; + iface = iface.slice(0, iface.length - 1); + mac = nextline[1]; + } + } else { + iface = lines[i].split(' ')[0]; + mac = lines[i].split('HWaddr ')[1]; } - } else { - iface = lines[i].split(' ')[0]; - mac = lines[i].split('HWaddr ')[1]; - } - if (iface && mac) { - result[iface] = mac.trim(); - iface = ''; - mac = ''; + if (iface && mac) { + result[iface] = mac.trim(); + iface = ''; + mac = ''; + } } } + } catch (e) { + util.noop(); } } if (_darwin) { - const cmd = '/sbin/ifconfig'; - let res = execSync(cmd); - const lines = res.toString().split('\n'); - for (let i = 0; i < lines.length; i++) { - if (lines[i] && lines[i][0] !== '\t' && lines[i].indexOf(':') > 0) { - iface = lines[i].split(':')[0]; - } else if (lines[i].indexOf('\tether ') === 0) { - mac = lines[i].split('\tether ')[1]; - if (iface && mac) { - result[iface] = mac.trim(); - iface = ''; - mac = ''; + try { + const cmd = '/sbin/ifconfig'; + let res = execSync(cmd); + const lines = res.toString().split('\n'); + for (let i = 0; i < lines.length; i++) { + if (lines[i] && lines[i][0] !== '\t' && lines[i].indexOf(':') > 0) { + iface = lines[i].split(':')[0]; + } else if (lines[i].indexOf('\tether ') === 0) { + mac = lines[i].split('\tether ')[1]; + if (iface && mac) { + result[iface] = mac.trim(); + iface = ''; + mac = ''; + } } } + } catch (e) { + util.noop(); } } return result; @@ -491,21 +499,25 @@ function getLinuxIfaceConnectionName(interfaceName) { function checkLinuxDCHPInterfaces(file) { let result = []; - let cmd = `cat ${file} 2> /dev/null | grep 'iface\\|source'`; - const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n'); + try { + let cmd = `cat ${file} 2> /dev/null | grep 'iface\\|source'`; + const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n'); - lines.forEach(line => { - const parts = line.replace(/\s+/g, ' ').trim().split(' '); - if (parts.length >= 4) { - if (line.toLowerCase().indexOf(' inet ') >= 0 && line.toLowerCase().indexOf('dhcp') >= 0) { - result.push(parts[1]); + lines.forEach(line => { + const parts = line.replace(/\s+/g, ' ').trim().split(' '); + if (parts.length >= 4) { + if (line.toLowerCase().indexOf(' inet ') >= 0 && line.toLowerCase().indexOf('dhcp') >= 0) { + result.push(parts[1]); + } } - } - if (line.toLowerCase().includes('source')) { - let file = line.split(' ')[1]; - result = result.concat(checkLinuxDCHPInterfaces(file)); - } - }); + if (line.toLowerCase().includes('source')) { + let file = line.split(' ')[1]; + result = result.concat(checkLinuxDCHPInterfaces(file)); + } + }); + } catch (e) { + util.noop(); + } return result; }