Skip to content

Commit

Permalink
fix execSync encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Oct 26, 2023
1 parent bf589ba commit ad8b942
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 44 deletions.
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).toString().split('\n\n');
const parts = execSync(cmd, { encoding: 'utf8' }).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').toString().toLowerCase();
const hdicon = execSync('hcitool con', { encoding: 'utf8' }).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').toString().split('\n');
const clusters = execSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type', { encoding: 'utf8' }).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).toString().split('-----\n');
const parts = execSync(cmd, { encoding: 'utf8' }).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').split('\n');
const lines = execSync('cat /proc/stat 2>/dev/null | grep cpu', { encoding: 'utf8' }).split('\n');
if (lines.length > 1) {
lines.shift();
if (lines.length === cpus.length) {
Expand Down
8 changes: 4 additions & 4 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').toString().split('\n').filter(line => {
execSync('cat /proc/mounts 2>/dev/null', { encoding: 'utf8' }).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}`).toString().split('\n');
const lines = execSync(`mdadm --export --detail /dev/${element.name}`, { encoding: 'utf8' }).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').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', { encoding: 'utf8' }).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').toString().split('\n')[0];
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational 2>/dev/null', { encoding: 'utf8' }).toString().split('\n')[0];
} catch (e) {
util.noop();
}
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: "').toString().split('\n');
pciIDs = execSync('export LC_ALL=C; dmidecode -t 9 2>/dev/null; unset LC_ALL | grep "Bus Address: "', { encoding: 'utf8' }).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');
let stdout = execSync('cat /proc/cpuinfo 2>/dev/null', { encoding: 'utf8' });
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');
stdout = execSync('vcgencmd get_config sdram_freq 2>/dev/null', { encoding: 'utf8' });
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');
stdout = execSync('vcgencmd measure_volts sdram_p 2>/dev/null', { encoding: 'utf8' });
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);
let result = execSync(cmd, { encoding: 'utf8' });
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);
let result = execSync(cmd, { encoding: 'utf8' });
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').toString().split('\n');
const lines = execSync('which ip', { encoding: 'utf8' }).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);
let res = execSync(cmd, { encoding: 'utf8' });
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);
let res = execSync(cmd, { encoding: 'utf8' });
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).toString();
const result = execSync(cmd, { encoding: 'utf8' }).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 }).toString().split('\n');
const lines = execSync(cmd, { maxBuffer: 1024 * 20000, encoding: 'utf8' }).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 }).toString().split('\n');
const lines = execSync(cmd, { maxBuffer: 1024 * 20000, encoding: 'utf8' }).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).toString();
const lines = execSync(cmd, { encoding: 'utf8' }).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).toString();
const result = execSync(cmd, { encoding: 'utf8' }).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).toString();
const result = execSync(cmd, { encoding: 'utf8' }).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).toString().split('\n');
lines = execSync(cmd, { encoding: 'utf8' }).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');
const stdout = execSync('hostnamectl --json short 2>/dev/null', { encoding: 'utf8' });
const json = JSON.parse(stdout.toString());

fqdn = json['StaticHostname'];
} catch (e) {
try {
const stdout = execSync('hostname -f 2>/dev/null');
const stdout = execSync('hostname -f 2>/dev/null', { encoding: 'utf8' });
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').toString().split('\n');
const tmpsrv = execSync('systemctl --all --type=service --no-legend 2> /dev/null', { encoding: 'utf8' }).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').toString().split('\n');
const tmpsrv = execSync('service --status-all 2> /dev/null', { encoding: 'utf8' }).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').toString().split('\n').join('');
const srvStr = execSync('ls /etc/init.d/ -m 2> /dev/null', { encoding: 'utf8' }).toString().split('\n').join('');
srvString = '';
if (srvStr) {
const tmpsrv = srvStr.split(',');
Expand Down
12 changes: 6 additions & 6 deletions lib/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function system(callback) {
echo -n "product_version: "; cat /sys/devices/virtual/dmi/id/product_version 2>/dev/null; echo;
echo -n "sys_vendor: "; cat /sys/devices/virtual/dmi/id/sys_vendor 2>/dev/null; echo;`;
try {
lines = execSync(cmd).toString().split('\n');
lines = execSync(cmd, { encoding: 'utf8' }).toString().split('\n');
result.manufacturer = result.manufacturer === '' ? util.getValue(lines, 'sys_vendor') : result.manufacturer;
result.model = result.model === '' ? util.getValue(lines, 'product_name') : result.model;
result.version = result.version === '' ? util.getValue(lines, 'product_version') : result.version;
Expand Down Expand Up @@ -107,7 +107,7 @@ function system(callback) {
}
if (!result.virtual) {
try {
const disksById = execSync('ls -1 /dev/disk/by-id/ 2>/dev/null').toString();
const disksById = execSync('ls -1 /dev/disk/by-id/ 2>/dev/null', { encoding: 'utf8' }).toString();
if (disksById.indexOf('_QEMU_') >= 0) {
result.virtual = true;
result.virtualHost = 'QEMU';
Expand All @@ -129,7 +129,7 @@ function system(callback) {
}
if ((_freebsd || _openbsd || _netbsd) && !result.virtualHost) {
try {
const procInfo = execSync('dmidecode -t 4');
const procInfo = execSync('dmidecode -t 4', { encoding: 'utf8' });
const procLines = procInfo.toString().split('\n');
const procManufacturer = util.getValue(procLines, 'manufacturer', ':', true);
switch (procManufacturer.toLowerCase()) {
Expand All @@ -155,7 +155,7 @@ function system(callback) {
result.model = 'Docker Container';
}
try {
const stdout = execSync('dmesg 2>/dev/null | grep -iE "virtual|hypervisor" | grep -iE "vmware|qemu|kvm|xen" | grep -viE "Nested Virtualization|/virtual/"');
const stdout = execSync('dmesg 2>/dev/null | grep -iE "virtual|hypervisor" | grep -iE "vmware|qemu|kvm|xen" | grep -viE "Nested Virtualization|/virtual/"', { encoding: 'utf8' });
// detect virtual machines
let lines = stdout.toString().split('\n');
if (lines.length > 0) {
Expand Down Expand Up @@ -369,7 +369,7 @@ function bios(callback) {
echo -n "bios_vendor: "; cat /sys/devices/virtual/dmi/id/bios_vendor 2>/dev/null; echo;
echo -n "bios_version: "; cat /sys/devices/virtual/dmi/id/bios_version 2>/dev/null; echo;`;
try {
lines = execSync(cmd).toString().split('\n');
lines = execSync(cmd, { encoding: 'utf8' }).toString().split('\n');
result.vendor = !result.vendor ? util.getValue(lines, 'bios_vendor') : result.vendor;
result.version = !result.version ? util.getValue(lines, 'bios_version') : result.version;
datetime = util.getValue(lines, 'bios_date');
Expand Down Expand Up @@ -483,7 +483,7 @@ function baseboard(callback) {
echo -n "board_vendor: "; cat /sys/devices/virtual/dmi/id/board_vendor 2>/dev/null; echo;
echo -n "board_version: "; cat /sys/devices/virtual/dmi/id/board_version 2>/dev/null; echo;`;
try {
lines = execSync(cmd).toString().split('\n');
lines = execSync(cmd, { encoding: 'utf8' }).toString().split('\n');
result.manufacturer = !result.manufacturer ? util.getValue(lines, 'board_vendor') : result.manufacturer;
result.model = !result.model ? util.getValue(lines, 'board_name') : result.model;
result.version = !result.version ? util.getValue(lines, 'board_version') : result.version;
Expand Down
Loading

0 comments on commit ad8b942

Please sign in to comment.