-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwho.js
50 lines (44 loc) · 1.18 KB
/
who.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var exec = require('child_process').exec;
// Returns an array of JSON from raw w output
function read (stdout) {
if (!stdout) {
return { 'users': [] };
}
var lines = stdout.split('\n');
var names = lines[1].split(/\s+/);
// 5 minute load is the second to last thing
var load = lines[0].split(/\s+/).reverse()[1].slice(0, -1);
var users = lines.slice(2, -1).map(
function (line) {
var who = {};
line.split(/\s+/).forEach(
// Each value is named by its column
function(value, index) {
who[names[index]] = value;
}
);
return who;
}
);
return { 'users': users, 'loadAverage': load };
}
// Runs callback on the array from running w on each server
function ssh (username, server, callback) {
if (!(username && server)) {
throw new Error('Invalid ssh operands');
}
// Build and exec a command if we have the stuff
exec('ssh ' + username + '@' + server + ' w',
function (err, stdout, stderr) {
if (err) {
console.log(err);
console.log(stderr);
return false;
}
callback(read(stdout));
});
}
module.exports = {
read: read,
ssh: ssh
};