diff --git a/lib/network.js b/lib/network.js index f930555..bce8694 100644 --- a/lib/network.js +++ b/lib/network.js @@ -1536,18 +1536,24 @@ function networkConnections(callback) { } if (_darwin) { let cmd = 'netstat -natvln | grep "tcp4\\|tcp6\\|udp4\\|udp6"'; - const states = 'ESTABLISHED|SYN_SENT|SYN_RECV|FIN_WAIT1|FIN_WAIT_1|FIN_WAIT2|FIN_WAIT_2|TIME_WAIT|CLOSE|CLOSE_WAIT|LAST_ACK|LISTEN|CLOSING|UNKNOWN'; + const states = 'ESTABLISHED|SYN_SENT|SYN_RECV|FIN_WAIT1|FIN_WAIT_1|FIN_WAIT2|FIN_WAIT_2|TIME_WAIT|CLOSE|CLOSE_WAIT|LAST_ACK|LISTEN|CLOSING|UNKNOWN'.split('|'); exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) { if (!error) { exec('ps -axo pid,command', { maxBuffer: 1024 * 20000 }, function (err2, stdout2) { let processes = stdout2.toString().split('\n'); processes = processes.map((line => { return line.trim().replace(/ +/g, ' '); })); let lines = stdout.toString().split('\n'); - - + let pidPos = 8; + if (lines[0] !== '') { + const lineParts = lines[0].replace(/ +/g, ' ').split(' '); + for (let i = 0; i < lineParts.length; i++) { + if (states.indexOf(lineParts[i]) >= 0) { + pidPos = i + 3; + } + }; + } lines.forEach(function (line) { line = line.replace(/ +/g, ' ').split(' '); - const hasTransferred = line.length >= 17; if (line.length >= 8) { let localip = line[3]; let localport = ''; @@ -1567,7 +1573,7 @@ function networkConnections(callback) { } const hasState = states.indexOf(line[5]) >= 0; let connstate = hasState ? line[5] : 'UNKNOWN'; - let pid = parseInt(line[6 + (hasState ? 0 : -1) + (hasTransferred ? 2 : 0)], 10); + let pid = parseInt(line[pidPos + (hasState ? 0 : -1)], 10); if (connstate) { result.push({ protocol: line[0],