allow meshctrl.js to reply with output from runcommands (#5932)

Signed-off-by: si458 <simonsmith5521@gmail.com>

Simon Smith committed Mar 14, 2024 at 14:33 UTC 4f11d7fdbc135398b2f13c8fbbf335db27b492b5
3 files changed +37 -7
agents/meshcore.js
+28 -4
@@ -1519,7 +1519,7 @@ function handleServerCommand(data) {
1519 }
1520 case 'runcommands': {
1521 if (mesh.cmdchild != null) { sendConsoleText("Run commands can't execute, already busy."); break; }
1522 - sendConsoleText("Run commands (" + data.runAsUser + "): " + data.cmds);
1522 + if (!data.reply) sendConsoleText("Run commands (" + data.runAsUser + "): " + data.cmds);
1523
1524 // data.runAsUser: 0=Agent,1=UserOrAgent,2=UserOnly
1525 var options = {};
@@ -1540,7 +1540,15 @@ function handleServerCommand(data) {
1540 mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
1541 mesh.cmdchild.stderr.on('data', function (c) { replydata += c.toString(); });
1542 mesh.cmdchild.stdin.write(data.cmds + '\r\nexit\r\n');
1543 - mesh.cmdchild.on('exit', function () { sendConsoleText(replydata); sendConsoleText("Run commands completed."); delete mesh.cmdchild; });
1543 + mesh.cmdchild.on('exit', function () {
1544 + if (data.reply) {
1545 + mesh.SendCommand({ action: 'msg', type: 'runcommands', result: replydata, sessionid: data.sessionid, responseid: data.responseid });
1546 + } else {
1547 + sendConsoleText(replydata);
1548 + sendConsoleText("Run commands completed.");
1549 + }
1550 + delete mesh.cmdchild;
1551 + });
1552 } else if (data.type == 2) {
1553 // Windows Powershell
1554 mesh.cmdchild = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], options);
@@ -1548,7 +1556,15 @@ function handleServerCommand(data) {
1556 mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
1557 mesh.cmdchild.stderr.on('data', function (c) { replydata += c.toString(); });
1558 mesh.cmdchild.stdin.write(data.cmds + '\r\nexit\r\n');
1551 - mesh.cmdchild.on('exit', function () { sendConsoleText(replydata); sendConsoleText("Run commands completed."); delete mesh.cmdchild; });
1559 + mesh.cmdchild.on('exit', function () {
1560 + if (data.reply) {
1561 + mesh.SendCommand({ action: 'msg', type: 'runcommands', result: replydata, sessionid: data.sessionid, responseid: data.responseid });
1562 + } else {
1563 + sendConsoleText(replydata);
1564 + sendConsoleText("Run commands completed.");
1565 + }
1566 + delete mesh.cmdchild;
1567 + });
1568 }
1569 } else if (data.type == 3) {
1570 // Linux shell
@@ -1557,7 +1573,15 @@ function handleServerCommand(data) {
1573 mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
1574 mesh.cmdchild.stderr.on('data', function (c) { replydata + c.toString(); });
1575 mesh.cmdchild.stdin.write(data.cmds.split('\r').join('') + '\nexit\n');
1560 - mesh.cmdchild.on('exit', function () { sendConsoleText(replydata); sendConsoleText("Run commands completed."); delete mesh.cmdchild; });
1576 + mesh.cmdchild.on('exit', function () {
1577 + if (data.reply) {
1578 + mesh.SendCommand({ action: 'msg', type: 'runcommands', result: replydata, sessionid: data.sessionid, responseid: data.responseid });
1579 + } else {
1580 + sendConsoleText(replydata);
1581 + sendConsoleText("Run commands completed.");
1582 + }
1583 + delete mesh.cmdchild;
1584 + });
1585 }
1586 break;
1587 }
meshctrl.js
+5 -1
@@ -820,6 +820,7 @@ if (args['_'].length == 0) {
820 console.log("Run a shell command on a remote device, Example usages:\r\n");
821 console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\""));
822 console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --powershell"));
823 + console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --reply"));
824 console.log("\r\nRequired arguments:\r\n");
825 if (process.platform == 'win32') {
826 console.log(" --id [deviceid] - The device identifier.");
@@ -831,6 +832,7 @@ if (args['_'].length == 0) {
832 console.log(" --powershell - Run in Windows PowerShell.");
833 console.log(" --runasuser - Attempt to run the command as logged in user.");
834 console.log(" --runasuseronly - Only run the command as the logged in user.");
835 + console.log(" --reply - Return with the output from running the command.");
836 break;
837 }
838 case 'shell': {
@@ -1621,7 +1623,9 @@ function serverConnect() {
1623 case 'runcommand': {
1624 var runAsUser = 0;
1625 if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; }
1624 - ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser }));
1626 + var reply = false;
1627 + if (args.reply) { reply = true; }
1628 + ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser, reply: reply }));
1629 break;
1630 }
1631 case 'shell':
meshuser.js
+4 -2
@@ -2977,8 +2977,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2977 }
2978 if (commandsOk == true) {
2979 // Send the commands to the agent
2980 - try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser })); } catch (ex) { }
2981 - if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2980 + if (typeof command.reply != 'boolean') command.reply = false;
2981 + if (typeof command.responseid != 'string') command.responseid = null;
2982 + try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser, reply: command.reply, responseid: command.responseid })); } catch (ex) { }
2983 + if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2984
2985 // Send out an event that these commands where run on this device
2986 var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);