1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-26 05:03:15 +00:00

Updated consoleUid() and getXInfo() for Amazon EC2 (Amazon Linux)

This commit is contained in:
Bryan Roe
2021-12-11 23:45:59 -08:00
parent 235fcbd3ba
commit 4f43c68b56
3 changed files with 60 additions and 5 deletions

View File

@@ -516,13 +516,27 @@ function monitorinfo()
var child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = '';
child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("ps " + (process.platform == 'freebsd'?"-ax ":"") + "-e -o user" + (process.platform=='linux'?":999":"") + " -o tty -o command | grep X | awk '{ split($0, a, \"-auth\"); split(a[2], b, \" \"); if($1==\"" + uname + "\" && b[1]!=\"\") { printf \"%s,%s,%s\",$1,$2,b[1] } }'\nexit\n");
child.stdin.write("ps " + "-e -o user" + (":999") + " -o tty -o command | grep X | ");
child.stdin.write("awk '{ ");
child.stdin.write(' display="";');
child.stdin.write(' if($4~/^:/)');
child.stdin.write(' {');
child.stdin.write(' display=$4;');
child.stdin.write(' }');
child.stdin.write(' split($0, a, "-auth");');
child.stdin.write(' split(a[2], b, " ");');
child.stdin.write(' if($1=="' + uname + '" && b[1]!="")');
child.stdin.write(" {");
child.stdin.write(" printf \"%s,%s,%s,%s\",$1,$2,b[1],display;");
child.stdin.write(" }");
child.stdin.write(" }'\nexit\n");
child.waitExit();
var tokens = child.stdout.str.trim().split(',');
console.info1(JSON.stringify(tokens));
if (tokens.length == 3)
if (tokens.length == 4)
{
ret = { tty: tokens[1], xauthority: tokens[2], exportEnv: exportEnv };
ret = { tty: tokens[1], xauthority: tokens[2], display: tokens[3], exportEnv: exportEnv };
console.info1('ret => ' + JSON.stringify(ret));
}
@@ -754,6 +768,7 @@ function monitorinfo()
{
if (ps[psx] == 0)
{
if (psx == 0) { continue; }
v = ps.slice(vs, psx).toString().split('=');
if (v[0] == 'DISPLAY')
{

View File

@@ -767,6 +767,7 @@ function UserSessions()
var sids = [];
var i;
for (i = 0; i < info1.length; ++i) { sids.push(info1[i].sid); }
console.info1('SIDS => ' + JSON.stringify(sids));
child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
@@ -792,6 +793,45 @@ function UserSessions()
var info = require('monitor-info').getXInfo(gdm);
if (info == null || !info.xauthority || !info.display)
{
if (gdm == 0)
{
// Before we give up, lets see if there is an X session somewhere, probably from VNC
child = require('child_process').execFile('/bin/sh', ['sh']);
child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
child.stdin.write("loginctl list-sessions | tr '\\n' '`' | awk '{");
child.stdin.write('printf "[";');
child.stdin.write('del="";');
child.stdin.write('n=split($0, lines, "`");');
child.stdin.write('for(i=1;i<n;++i)');
child.stdin.write('{');
child.stdin.write(' split(lines[i], tok, " ");');
child.stdin.write(' if((tok[2]+0)>=' + min + ')');
child.stdin.write(' {');
child.stdin.write(' printf "%s{\\"uid\\": \\"%s\\", \\"sid\\": \\"%s\\"}", del, tok[2], tok[1];');
child.stdin.write(' del=",";');
child.stdin.write(' }');
child.stdin.write('}');
child.stdin.write('printf "]";');
child.stdin.write("}'\nexit\n");
child.waitExit();
var info1 = JSON.parse(child.stdout.str);
var uids = [];
var i;
for (i = 0; i < info1.length; ++i) { uids.push(info1[i].uid); }
console.info1('UIDS => ' + JSON.stringify(sids));
while(uids.length>0)
{
var id = uids.pop();
info = require('monitor-info').getXInfo(id);
if(info!=null && info.xauthority != null && info.display != null)
{
return (parseInt(id));
}
}
}
throw ('nobody logged into console');
}
else