diff --git a/lib/network.js b/lib/network.js index 5bf73e9..dbb3812 100644 --- a/lib/network.js +++ b/lib/network.js @@ -32,6 +32,7 @@ const _sunos = (_platform === 'sunos'); let _network = {}; let _default_iface = ''; let _ifaces = []; +let _dhcpNics = []; let _networkInterfaces = []; let _mac = {}; let pathToIp; @@ -389,7 +390,7 @@ function splitSectionsNics(lines) { const result = []; let section = []; lines.forEach(function (line) { - if (!line.startsWith('\t')) { + if (!line.startsWith('\t') && !line.startsWith(' ')) { if (section.length) { result.push(section); section = []; @@ -463,7 +464,7 @@ function parseLinesDarwinNics(sections) { function getDarwinNics() { const cmd = '/sbin/ifconfig -v'; try { - const lines = execSync(cmd, util.execOptsWin).toString().split('\n'); + const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n'); const nsections = splitSectionsNics(lines); return (parseLinesDarwinNics(nsections)); } catch (e) { @@ -485,7 +486,57 @@ function getLinuxIfaceConnectionName(interfaceName) { } } -function getLinuxIfaceDHCPstatus(connectionName) { +function getLinuxDHCPnNics() { + // alternate methods getting interfaces using DHCP + let cmd = 'ip a'; + let result = []; + try { + const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n'); + const nsections = splitSectionsNics(lines); + result = (parseLinuxDHCPNics(nsections)); + } catch (e) { + util.noop(); + } + try { + cmd = 'cat /etc/network/interfaces | grep iface'; + 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('dynamic') >= 0) { + result.push(parts[1]); + } + } + }); + } catch (e) { + util.noop(); + } + return result; +} + +function parseLinuxDHCPNics(sections) { + const result = []; + if (sections && sections.length) { + sections.forEach(lines => { + if (lines && lines.length) { + const parts = lines[0].split(':'); + if (parts.length > 2) { + for (let line of lines) { + if (line.indexOf(' inet ') >= 0 && line.indexOf(' dynamic ') >= 0) { + const parts2 = line.split(' '); + const nic = parts2[parts2.length - 1].trim(); + result.push(nic); + break; + } + } + } + } + }); + } + return result; +} + +function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) { let result = false; if (connectionName) { const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep ipv4.method;`; @@ -505,7 +556,7 @@ function getLinuxIfaceDHCPstatus(connectionName) { } return result; } catch (e) { - return result; + return (DHCPNics.indexOf(iface) >= 0); } } else { return result; @@ -623,6 +674,9 @@ function networkInterfaces(callback) { nics = getWindowsNics(); dnsSuffixes = getWindowsDNSsuffixes(); } + if (_linux) { + _dhcpNics = getLinuxDHCPnNics(); + } for (let dev in ifaces) { let ip4 = ''; let ip6 = ''; @@ -688,14 +742,12 @@ function networkInterfaces(callback) { let lines = []; try { + lines = execSync(cmd).toString().split('\n'); const connectionName = getLinuxIfaceConnectionName(iface); - dhcp = getLinuxIfaceDHCPstatus(connectionName); + dhcp = getLinuxIfaceDHCPstatus(iface, connectionName, _dhcpNics); dnsSuffix = getLinuxIfaceDNSsuffix(connectionName); ieee8021xAuth = getLinuxIfaceIEEE8021xAuth(connectionName); ieee8021xState = getLinuxIfaceIEEE8021xState(ieee8021xAuth); - lines = execSync(cmd).toString().split('\n'); - - } catch (e) { util.noop(); } diff --git a/lib/util.js b/lib/util.js index 131914e..e0a9998 100644 --- a/lib/util.js +++ b/lib/util.js @@ -40,7 +40,6 @@ const execOptsWin = { env: util._extend({}, process.env, { LANG: 'en_US.UTF-8' }) }; - function toInt(value) { let result = parseInt(value, 10); if (isNaN(result)) {