bugfix users() - parsing values on windows
This commit is contained in:
+79
-19
@@ -26,7 +26,7 @@ const NOT_SUPPORTED = 'not supported';
|
||||
// --------------------------
|
||||
// array of users online = sessions
|
||||
|
||||
function parseUsers1(lines) {
|
||||
function parseUsersLinux(lines) {
|
||||
let result = [];
|
||||
let result_who = [];
|
||||
let result_w = {};
|
||||
@@ -88,7 +88,7 @@ function parseUsers1(lines) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseUsers2(lines) {
|
||||
function parseUsersDarwin(lines) {
|
||||
let result = [];
|
||||
let result_who = [];
|
||||
let result_w = {};
|
||||
@@ -136,6 +136,79 @@ function parseUsers2(lines) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseUsersWin(lines) {
|
||||
|
||||
function parseDateTime(dt) {
|
||||
const result = {
|
||||
date: '',
|
||||
time: ''
|
||||
}
|
||||
const parts = dt.split(' ');
|
||||
if (parts[0]) {
|
||||
if (parts[0].indexOf('/') >= 0) {
|
||||
// Dateformat: mm/dd/yyyy
|
||||
const dtparts = parts[0].split('/');
|
||||
if (dtparts.length === 3) {
|
||||
result.date = dtparts[2] + '-' + ('0' + dtparts[0]).substr(-2) + '-' + ('0' + dtparts[1]).substr(-2)
|
||||
}
|
||||
}
|
||||
if (parts[0].indexOf('.') >= 0) {
|
||||
// Dateformat: dd.mm.yyyy
|
||||
const dtparts = parts[0].split('.');
|
||||
if (dtparts.length === 3) {
|
||||
result.date = dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2)
|
||||
}
|
||||
}
|
||||
if (parts[0].indexOf('-') >= 0) {
|
||||
// Dateformat: yyyy-mm-dd
|
||||
const dtparts = parts[0].split('-');
|
||||
if (dtparts.length === 3) {
|
||||
result.date = dtparts[0] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[2]).substr(-2)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (parts[1]) {
|
||||
result.time = parts[1];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
let result = [];
|
||||
const header = lines[0];
|
||||
const headerDelimiter = [];
|
||||
if (header) {
|
||||
const start = (header[0] === ' ') ? 1 : 0;
|
||||
headerDelimiter.push(start-1);
|
||||
let nextSpace = 0
|
||||
for (let i = start+1; i < header.length; i++) {
|
||||
if (header[i] === ' ' && header[i-1] === ' ') {
|
||||
nextSpace = i;
|
||||
} else {
|
||||
if (nextSpace) {
|
||||
headerDelimiter.push(nextSpace);
|
||||
nextSpace = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
if (lines[i].trim()) {
|
||||
const user = lines[i].substring(headerDelimiter[0]+1, headerDelimiter[1]).trim() || '';
|
||||
const tty = lines[i].substring(headerDelimiter[1]+1, headerDelimiter[2] - 2).trim() || '';
|
||||
const dateTime = parseDateTime(lines[i].substring(headerDelimiter[5]+1, 2000).trim()) || '';
|
||||
result.push({
|
||||
user: user,
|
||||
tty: tty,
|
||||
date: dateTime.date,
|
||||
time: dateTime.time,
|
||||
ip: '',
|
||||
command: ''
|
||||
})
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function users(callback) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -148,13 +221,13 @@ function users(callback) {
|
||||
if (!error) {
|
||||
// lines / split
|
||||
let lines = stdout.toString().split('\n');
|
||||
result = parseUsers1(lines);
|
||||
result = parseUsersLinux(lines);
|
||||
if (result.length === 0) {
|
||||
exec("who; echo '---'; w | tail -n +2", function (error, stdout) {
|
||||
if (!error) {
|
||||
// lines / split
|
||||
lines = stdout.toString().split('\n');
|
||||
result = parseUsers1(lines);
|
||||
result = parseUsersLinux(lines);
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
@@ -175,7 +248,7 @@ function users(callback) {
|
||||
if (!error) {
|
||||
// lines / split
|
||||
let lines = stdout.toString().split('\n');
|
||||
result = parseUsers2(lines);
|
||||
result = parseUsersDarwin(lines);
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
@@ -186,20 +259,7 @@ function users(callback) {
|
||||
if (stdout) {
|
||||
// lines / split
|
||||
let lines = stdout.toString().split('\r\n');
|
||||
lines.shift();
|
||||
lines.forEach(function (line) {
|
||||
let l = line.replace(/ +/g, " ").split(' ');
|
||||
if (l.length >= 7) {
|
||||
result.push({
|
||||
user: l[0].replace(/>/g, " "),
|
||||
tty: l[1],
|
||||
date: l[5].substr(6,4) + '-' + l[5].substr(3,2) + '-' + l[5].substr(0,2),
|
||||
time: l[4],
|
||||
ip: '',
|
||||
command: ''
|
||||
})
|
||||
}
|
||||
})
|
||||
result = parseUsersWin(lines);
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
|
||||
Reference in New Issue
Block a user