diff --git a/agents/meshcore.js b/agents/meshcore.js index a84300a3..07759f39 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -1519,7 +1519,7 @@ function handleServerCommand(data) { } case 'runcommands': { if (mesh.cmdchild != null) { sendConsoleText("Run commands can't execute, already busy."); break; } - sendConsoleText("Run commands (" + data.runAsUser + "): " + data.cmds); + if (!data.reply) sendConsoleText("Run commands (" + data.runAsUser + "): " + data.cmds); // data.runAsUser: 0=Agent,1=UserOrAgent,2=UserOnly var options = {}; @@ -1540,7 +1540,15 @@ function handleServerCommand(data) { mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); }); mesh.cmdchild.stderr.on('data', function (c) { replydata += c.toString(); }); mesh.cmdchild.stdin.write(data.cmds + '\r\nexit\r\n'); - mesh.cmdchild.on('exit', function () { sendConsoleText(replydata); sendConsoleText("Run commands completed."); delete mesh.cmdchild; }); + mesh.cmdchild.on('exit', function () { + if (data.reply) { + mesh.SendCommand({ action: 'msg', type: 'runcommands', result: replydata, sessionid: data.sessionid, responseid: data.responseid }); + } else { + sendConsoleText(replydata); + sendConsoleText("Run commands completed."); + } + delete mesh.cmdchild; + }); } else if (data.type == 2) { // Windows Powershell 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) { mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); }); mesh.cmdchild.stderr.on('data', function (c) { replydata += c.toString(); }); mesh.cmdchild.stdin.write(data.cmds + '\r\nexit\r\n'); - mesh.cmdchild.on('exit', function () { sendConsoleText(replydata); sendConsoleText("Run commands completed."); delete mesh.cmdchild; }); + mesh.cmdchild.on('exit', function () { + if (data.reply) { + mesh.SendCommand({ action: 'msg', type: 'runcommands', result: replydata, sessionid: data.sessionid, responseid: data.responseid }); + } else { + sendConsoleText(replydata); + sendConsoleText("Run commands completed."); + } + delete mesh.cmdchild; + }); } } else if (data.type == 3) { // Linux shell @@ -1557,7 +1573,15 @@ function handleServerCommand(data) { mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); }); mesh.cmdchild.stderr.on('data', function (c) { replydata + c.toString(); }); mesh.cmdchild.stdin.write(data.cmds.split('\r').join('') + '\nexit\n'); - mesh.cmdchild.on('exit', function () { sendConsoleText(replydata); sendConsoleText("Run commands completed."); delete mesh.cmdchild; }); + mesh.cmdchild.on('exit', function () { + if (data.reply) { + mesh.SendCommand({ action: 'msg', type: 'runcommands', result: replydata, sessionid: data.sessionid, responseid: data.responseid }); + } else { + sendConsoleText(replydata); + sendConsoleText("Run commands completed."); + } + delete mesh.cmdchild; + }); } break; } diff --git a/meshctrl.js b/meshctrl.js index 494a1264..f7bb1e86 100644 --- a/meshctrl.js +++ b/meshctrl.js @@ -820,6 +820,7 @@ if (args['_'].length == 0) { console.log("Run a shell command on a remote device, Example usages:\r\n"); console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\"")); console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --powershell")); + console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --reply")); console.log("\r\nRequired arguments:\r\n"); if (process.platform == 'win32') { console.log(" --id [deviceid] - The device identifier."); @@ -831,6 +832,7 @@ if (args['_'].length == 0) { console.log(" --powershell - Run in Windows PowerShell."); console.log(" --runasuser - Attempt to run the command as logged in user."); console.log(" --runasuseronly - Only run the command as the logged in user."); + console.log(" --reply - Return with the output from running the command."); break; } case 'shell': { @@ -1621,7 +1623,9 @@ function serverConnect() { case 'runcommand': { var runAsUser = 0; if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; } - ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser })); + var reply = false; + if (args.reply) { reply = true; } + ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser, reply: reply })); break; } case 'shell': diff --git a/meshuser.js b/meshuser.js index c20f3a1a..ea353846 100644 --- a/meshuser.js +++ b/meshuser.js @@ -2977,8 +2977,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } if (commandsOk == true) { // Send the commands to the agent - try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser })); } catch (ex) { } - if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } } + if (typeof command.reply != 'boolean') command.reply = false; + if (typeof command.responseid != 'string') command.responseid = null; + try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser, reply: command.reply, responseid: command.responseid })); } catch (ex) { } + if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } } // Send out an event that these commands where run on this device var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);