Skip to content

Commit

Permalink
wifiConnections() improved parsing (linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Oct 27, 2023
1 parent 9ba3491 commit a4f8cd3
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ For major (breaking) changes - **version 4, 3 and 2** - see end of page.

| Version | Date | Comment |
| ------- | ---------- | --------------------------------------------------------------------------------------------------- |
| 5.21.15 | 2023-10-27 | `wifiConnections()` improved parsing (linux) |
| 5.21.14 | 2023-10-26 | `execSync()` added explicit encoding (linux) |
| 5.21.13 | 2023-10-21 | `system()` Raspberry Pi 5 detection |
| 5.21.12 | 2023-10-17 | `system()` Raspberry CM4S detection |
Expand Down
5 changes: 5 additions & 0 deletions docs/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ <h3>Full version history</h3>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">5.21.15</th>
<td>2023-10-27</td>
<td><span class="code">wifiConnections()</span> improved parsing (linux)</td>
</tr>
<tr>
<th scope="row">5.21.14</th>
<td>2023-10-26</td>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
<img class="logo" src="assets/logo.png" alt="logo">
<div class="title">systeminformation</div>
<div class="subtitle"><span id="typed"></span>&nbsp;</div>
<div class="version">New Version: <span id="version">5.21.14</span></div>
<div class="version">New Version: <span id="version">5.21.15</span></div>
<button class="btn btn-light" onclick="location.href='https://github.com/sebhildebrandt/systeminformation'">View on Github <i class=" fab fa-github"></i></button>
</div>
<div class="down">
Expand Down
2 changes: 1 addition & 1 deletion lib/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function getLinuxAudioPci() {
let cmd = 'lspci -v 2>/dev/null';
let result = [];
try {
const parts = execSync(cmd, { encoding: 'utf8' }).toString().split('\n\n');
const parts = execSync(cmd).toString().split('\n\n');
parts.forEach(element => {
const lines = element.split('\n');
if (lines && lines.length && lines[0].toLowerCase().indexOf('audio') >= 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function bluetoothDevices(callback) {
});
// determine "connected" with hcitool con
try {
const hdicon = execSync('hcitool con', { encoding: 'utf8' }).toString().toLowerCase();
const hdicon = execSync('hcitool con').toString().toLowerCase();
for (let i = 0; i < result.length; i++) {
if (result[i].macDevice && result[i].macDevice.length > 10 && hdicon.indexOf(result[i].macDevice.toLowerCase()) >= 0) {
result[i].connected = true;
Expand Down
6 changes: 3 additions & 3 deletions lib/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ function getCpu() {
if (os.arch() === 'arm64') {
result.socket = 'SOC';
try {
const clusters = execSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type', { encoding: 'utf8' }).toString().split('\n');
const clusters = execSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type').toString().split('\n');
const efficiencyCores = clusters.filter(line => line.indexOf('"E"') >= 0).length;
const performanceCores = clusters.filter(line => line.indexOf('"P"') >= 0).length;
result.efficiencyCores = efficiencyCores;
Expand Down Expand Up @@ -1048,7 +1048,7 @@ function cpuTemperature(callback) {
// CPU Chipset, Socket
try {
const cmd = 'cat /sys/class/thermal/thermal_zone*/type 2>/dev/null; echo "-----"; cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null;';
const parts = execSync(cmd, { encoding: 'utf8' }).toString().split('-----\n');
const parts = execSync(cmd).toString().split('-----\n');
if (parts.length === 2) {
const lines = parts[0].split('\n');
const lines2 = parts[1].split('\n');
Expand Down Expand Up @@ -1604,7 +1604,7 @@ function getLoad() {
// linux: try to get other cpu stats
if (_linux) {
try {
const lines = execSync('cat /proc/stat 2>/dev/null | grep cpu', { encoding: 'utf8' }).split('\n');
const lines = execSync('cat /proc/stat 2>/dev/null | grep cpu').toString().split('\n');

This comment has been minimized.

Copy link
@juandav

juandav Nov 8, 2023

I have a question?
What is the reason for removing utf 8? for this line it causes phantom logs to be printed.

This comment has been minimized.

Copy link
@sebhildebrandt

sebhildebrandt Nov 8, 2023

Author Owner

encoding: utf8 is the default, right? So it normally can be omitted. What are the issues that you see? What phantom logs can you see? Can you create an issue for this? Thank you in advance.

This comment has been minimized.

Copy link
@juandav

juandav Nov 8, 2023

The same issue that was solved here appears here again.

This comment has been minimized.

Copy link
@juandav

juandav Nov 8, 2023

image

This comment has been minimized.

Copy link
@juandav

juandav Nov 8, 2023

the default value is buffer

This comment has been minimized.

Copy link
@juandav

juandav Nov 8, 2023

Depending on the case, buffer or utf8 will be required, but for this particular line, utf8 is required.

This comment has been minimized.

Copy link
@sebhildebrandt

sebhildebrandt Nov 8, 2023

Author Owner

I discussed this with @corteshvicor back then and we decided that .toString() is the correct solution here. So basically: default encoding: buffer with an additional .tostring(). This is what we also have in all other execs and therefor we decided to make it consistent with the others.

So can you describe what phantom slogs you get? This would help clarify the problem.

This comment has been minimized.

Copy link
@sebhildebrandt

sebhildebrandt Nov 8, 2023

Author Owner

What you see in this commit is basically that I reverted everything that I added a few hours earlier ... the only difference in this one line to all other exec was the missing .toString(). So it would be helpful to see what Dows wrong on your side as I cannot reproduce anything here. Thank you in advance!

This comment has been minimized.

Copy link
@juandav

juandav Nov 8, 2023

I will of course check thoroughly in the next few hours to see what I can find additional and how to reproduce the problem.

This comment has been minimized.

Copy link
@sebhildebrandt

sebhildebrandt Nov 8, 2023

Author Owner

Thank you so much ... as you see e.g. in line 1051 (and others) it also was always without the encoding: utf8 (but with the toString()) and here we never had any problem. So I am really curious what problems you can see on your side. I am sure we will find a solution.

This comment has been minimized.

Copy link
@corteshvictor

corteshvictor Nov 8, 2023

Hi, It is a very particular case. It is necessary to publish a service in pm2 and that has linux. It only happens in this scenario to view empty logs.

In the previous version when the encoding: utf8 was added, this empty log stopped coming out, but it was redundant to do the utf8 and the .toString.

But now in this new version when the redundancy was removed, the empty log came out again.

Can you release a beta or temporary version that does not have the .toString and rather use the utf8? So we validate if everything is still working the same, the empty log is corrected and we don't have the redundancy of both things.

This comment has been minimized.

Copy link
@corteshvictor

corteshvictor Nov 8, 2023

The truth is not clear to me why the .toString causes this empty log and the encoding: utf8 does not.

Maybe when using execSync, it returns the output as a buffer. But after converting the buffer to a string using the toString() method something does differently than the encoding: utf8 tells Node.js that it should interpret the command output as a string instead of a buffer.

That's why I think the ideal is to tell Node to interpret as a string and not to be tranforming the buffer with a .toString. If it is possible to release a beta version removing the .toString but if you use encoding as the documentation says it might be better.

This comment has been minimized.

Copy link
@sebhildebrandt

sebhildebrandt Nov 9, 2023

Author Owner

Thank you for all your comments and testing! Will re-add the encoding here. If you think there are also other places where we need it, please just send me a note with file, line-number and I will add it there also.

This comment has been minimized.

Copy link
@sebhildebrandt

sebhildebrandt Nov 9, 2023

Author Owner

Version 5.21.16 just release, can you retest it?

This comment has been minimized.

Copy link
@victorhcortesp

victorhcortesp Nov 9, 2023

Hi @sebhildebrandt I tell you that this new version I am not seeing the empty log.
I will keep checking and if anything I will tell you but this version already removes the empty log.

if (lines.length > 1) {
lines.shift();
if (lines.length === cpus.length) {
Expand Down
10 changes: 5 additions & 5 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function fsSize(drive, callback) {
if (_linux) {
try {
cmd = 'export LC_ALL=C; df -lkPTx squashfs; unset LC_ALL';
execSync('cat /proc/mounts 2>/dev/null', { encoding: 'utf8' }).toString().split('\n').filter(line => {
execSync('cat /proc/mounts 2>/dev/null').toString().split('\n').filter(line => {
return line.startsWith('/');
}).forEach((line) => {
osMounts[line.split(' ')[0]] = osMounts[line.split(' ')[0]] || false;
Expand Down Expand Up @@ -426,7 +426,7 @@ function raidMatchLinux(data) {
try {
data.forEach(element => {
if (element.type.startsWith('raid')) {
const lines = execSync(`mdadm --export --detail /dev/${element.name}`, { encoding: 'utf8' }).toString().split('\n');
const lines = execSync(`mdadm --export --detail /dev/${element.name}`).toString().split('\n');
const mdData = decodeMdabmData(lines);

element.label = mdData.label; // <- assign label info
Expand Down Expand Up @@ -1087,7 +1087,7 @@ function diskLayout(callback) {
} catch (e) {
// fallback to older version of lsblk
try {
const out2 = execSync('export LC_ALL=C; lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER,GROUP 2>/dev/null; unset LC_ALL', { encoding: 'utf8' }).toString();
const out2 = execSync('export LC_ALL=C; lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER,GROUP 2>/dev/null; unset LC_ALL').toString();
let lines = blkStdoutToObject(out2).split('\n');
const data = parseBlk(lines);
devices = data.filter(item => { return (item.type === 'disk') && item.size > 0 && ((item.model !== null && item.model !== '') || (item.mount === '' && item.label === '' && item.fsType === '')); });
Expand All @@ -1100,7 +1100,7 @@ function diskLayout(callback) {
const BSDName = '/dev/' + device.name;
const logical = device.name;
try {
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null', { encoding: 'utf8' }).toString().split('\n')[0];
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null').toString().split('\n')[0];
} catch (e) {
util.noop();
}
Expand Down Expand Up @@ -1406,7 +1406,7 @@ function diskLayout(callback) {
workload.push(util.powerShell('Get-PhysicalDisk | select BusType,MediaType,FriendlyName,Model,SerialNumber,Size | fl'));
if (util.smartMonToolsInstalled()) {
try {
const smartDev = JSON.parse(execSync('smartctl --scan -j'));
const smartDev = JSON.parse(execSync('smartctl --scan -j').toString());
if (smartDev && smartDev.devices && smartDev.devices.length > 0) {
smartDev.devices.forEach((dev) => {
workload.push(execPromiseSave(`smartctl -j -a ${dev.name}`, util.execOptsWin));
Expand Down
2 changes: 1 addition & 1 deletion lib/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function graphics(callback) {
// PCI bus IDs
let pciIDs = [];
try {
pciIDs = execSync('export LC_ALL=C; dmidecode -t 9 2>/dev/null; unset LC_ALL | grep "Bus Address: "', { encoding: 'utf8' }).toString().split('\n');
pciIDs = execSync('export LC_ALL=C; dmidecode -t 9 2>/dev/null; unset LC_ALL | grep "Bus Address: "').toString().split('\n');
for (let i = 0; i < pciIDs.length; i++) {
pciIDs[i] = pciIDs[i].replace('Bus Address:', '').replace('0000:', '').trim();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ function memLayout(callback) {

// Try Raspberry PI
try {
let stdout = execSync('cat /proc/cpuinfo 2>/dev/null', { encoding: 'utf8' });
let stdout = execSync('cat /proc/cpuinfo 2>/dev/null');
let lines = stdout.toString().split('\n');
let model = util.getValue(lines, 'hardware', ':', true).toUpperCase();
let version = util.getValue(lines, 'revision', ':', true).toLowerCase();
Expand All @@ -419,14 +419,14 @@ function memLayout(callback) {
result[0].clockSpeed = version && version[4] && version[4] === 'd' ? 500 : result[0].clockSpeed;
result[0].formFactor = 'SoC';

stdout = execSync('vcgencmd get_config sdram_freq 2>/dev/null', { encoding: 'utf8' });
stdout = execSync('vcgencmd get_config sdram_freq 2>/dev/null');
lines = stdout.toString().split('\n');
let freq = parseInt(util.getValue(lines, 'sdram_freq', '=', true), 10) || 0;
if (freq) {
result[0].clockSpeed = freq;
}

stdout = execSync('vcgencmd measure_volts sdram_p 2>/dev/null', { encoding: 'utf8' });
stdout = execSync('vcgencmd measure_volts sdram_p 2>/dev/null');
lines = stdout.toString().split('\n');
let voltage = parseFloat(util.getValue(lines, 'volt', '=', true)) || 0;
if (voltage) {
Expand Down
24 changes: 12 additions & 12 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function getDefaultNetworkInterface() {
}
if (_linux) {
let cmd = 'ip route 2> /dev/null | grep default';
let result = execSync(cmd, { encoding: 'utf8' });
let result = execSync(cmd);
let parts = result.toString().split('\n')[0].split(/\s+/);
if (parts[0] === 'none' && parts[5]) {
ifacename = parts[5];
Expand All @@ -108,7 +108,7 @@ function getDefaultNetworkInterface() {
if (_linux) { cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\''; }
if (_darwin) { cmd = 'route -n get default 2>/dev/null | grep interface: | awk \'{print $2}\''; }
if (_freebsd || _openbsd || _netbsd || _sunos) { cmd = 'route get 0.0.0.0 | grep interface:'; }
let result = execSync(cmd, { encoding: 'utf8' });
let result = execSync(cmd);
ifacename = result.toString().split('\n')[0];
if (ifacename.indexOf(':') > -1) {
ifacename = ifacename.split(':')[1].trim();
Expand All @@ -130,7 +130,7 @@ function getMacAddresses() {
if (_linux || _freebsd || _openbsd || _netbsd) {
if (typeof pathToIp === 'undefined') {
try {
const lines = execSync('which ip', { encoding: 'utf8' }).toString().split('\n');
const lines = execSync('which ip').toString().split('\n');
if (lines.length && lines[0].indexOf(':') === -1 && lines[0].indexOf('/') === 0) {
pathToIp = lines[0];
} else {
Expand All @@ -142,7 +142,7 @@ function getMacAddresses() {
}
try {
const cmd = 'export LC_ALL=C; ' + ((pathToIp) ? pathToIp + ' link show up' : '/sbin/ifconfig') + '; unset LC_ALL';
let res = execSync(cmd, { encoding: 'utf8' });
let res = execSync(cmd);
const lines = res.toString().split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i] && lines[i][0] !== ' ') {
Expand Down Expand Up @@ -172,7 +172,7 @@ function getMacAddresses() {
if (_darwin) {
try {
const cmd = '/sbin/ifconfig';
let res = execSync(cmd, { encoding: 'utf8' });
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) {
Expand Down Expand Up @@ -511,7 +511,7 @@ function getLinuxIfaceConnectionName(interfaceName) {
const cmd = `nmcli device status 2>/dev/null | grep ${interfaceName}`;

try {
const result = execSync(cmd, { encoding: 'utf8' }).toString();
const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g, ' ').trim();
const connectionNameLines = resultFormat.split(' ').slice(3);
const connectionName = connectionNameLines.join(' ');
Expand All @@ -525,7 +525,7 @@ function checkLinuxDCHPInterfaces(file) {
let result = [];
try {
let cmd = `cat ${file} 2> /dev/null | grep 'iface\\|source'`;
const lines = execSync(cmd, { maxBuffer: 1024 * 20000, encoding: 'utf8' }).toString().split('\n');
const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n');

lines.forEach(line => {
const parts = line.replace(/\s+/g, ' ').trim().split(' ');
Expand All @@ -550,7 +550,7 @@ function getLinuxDHCPNics() {
let cmd = 'ip a 2> /dev/null';
let result = [];
try {
const lines = execSync(cmd, { maxBuffer: 1024 * 20000, encoding: 'utf8' }).toString().split('\n');
const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n');
const nsections = splitSectionsNics(lines);
result = (parseLinuxDHCPNics(nsections));
} catch (e) {
Expand Down Expand Up @@ -591,7 +591,7 @@ function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) {
if (connectionName) {
const cmd = `nmcli connection show "${connectionName}" 2>/dev/null | grep ipv4.method;`;
try {
const lines = execSync(cmd, { encoding: 'utf8' }).toString();
const lines = execSync(cmd).toString();
const resultFormat = lines.replace(/\s+/g, ' ').trim();

let dhcStatus = resultFormat.split(' ').slice(1).toString();
Expand Down Expand Up @@ -631,7 +631,7 @@ function getLinuxIfaceDNSsuffix(connectionName) {
if (connectionName) {
const cmd = `nmcli connection show "${connectionName}" 2>/dev/null | grep ipv4.dns-search;`;
try {
const result = execSync(cmd, { encoding: 'utf8' }).toString();
const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g, ' ').trim();
const dnsSuffix = resultFormat.split(' ').slice(1).toString();
return dnsSuffix == '--' ? 'Not defined' : dnsSuffix;
Expand All @@ -647,7 +647,7 @@ function getLinuxIfaceIEEE8021xAuth(connectionName) {
if (connectionName) {
const cmd = `nmcli connection show "${connectionName}" 2>/dev/null | grep 802-1x.eap;`;
try {
const result = execSync(cmd, { encoding: 'utf8' }).toString();
const result = execSync(cmd).toString();
const resultFormat = result.replace(/\s+/g, ' ').trim();
const authenticationProtocol = resultFormat.split(' ').slice(1).toString();

Expand Down Expand Up @@ -875,7 +875,7 @@ function networkInterfaces(callback, rescan, defaultString) {

let lines = [];
try {
lines = execSync(cmd, { encoding: 'utf8' }).toString().split('\n');
lines = execSync(cmd).toString().split('\n');
const connectionName = getLinuxIfaceConnectionName(ifaceSanitized);
dhcp = getLinuxIfaceDHCPstatus(ifaceSanitized, connectionName, _dhcpNics);
dnsSuffix = getLinuxIfaceDNSsuffix(connectionName);
Expand Down
4 changes: 2 additions & 2 deletions lib/osinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ function getFQDN() {
let fqdn = os.hostname;
if (_linux || _darwin) {
try {
const stdout = execSync('hostnamectl --json short 2>/dev/null', { encoding: 'utf8' });
const stdout = execSync('hostnamectl --json short 2>/dev/null');
const json = JSON.parse(stdout.toString());

fqdn = json['StaticHostname'];
} catch (e) {
try {
const stdout = execSync('hostname -f 2>/dev/null', { encoding: 'utf8' });
const stdout = execSync('hostname -f 2>/dev/null');
fqdn = stdout.toString().split(os.EOL)[0];
} catch (e) {
util.noop();
Expand Down
6 changes: 3 additions & 3 deletions lib/processes.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function services(srv, callback) {
if (_linux || _freebsd || _openbsd || _netbsd || _darwin) {
if ((_linux || _freebsd || _openbsd || _netbsd) && srvString === '*') {
try {
const tmpsrv = execSync('systemctl --all --type=service --no-legend 2> /dev/null', { encoding: 'utf8' }).toString().split('\n');
const tmpsrv = execSync('systemctl --all --type=service --no-legend 2> /dev/null').toString().split('\n');
srvs = [];
for (const s of tmpsrv) {
const name = s.split('.service')[0];
Expand All @@ -164,7 +164,7 @@ function services(srv, callback) {
} catch (d) {
try {
srvString = '';
const tmpsrv = execSync('service --status-all 2> /dev/null', { encoding: 'utf8' }).toString().split('\n');
const tmpsrv = execSync('service --status-all 2> /dev/null').toString().split('\n');
for (const s of tmpsrv) {
const parts = s.split(']');
if (parts.length === 2) {
Expand All @@ -174,7 +174,7 @@ function services(srv, callback) {
srvs = srvString.split('|');
} catch (e) {
try {
const srvStr = execSync('ls /etc/init.d/ -m 2> /dev/null', { encoding: 'utf8' }).toString().split('\n').join('');
const srvStr = execSync('ls /etc/init.d/ -m 2> /dev/null').toString().split('\n').join('');
srvString = '';
if (srvStr) {
const tmpsrv = srvStr.split(',');
Expand Down
Loading

0 comments on commit a4f8cd3

Please sign in to comment.