mirror of
				https://github.com/Ylianst/MeshCentral.git
				synced 2025-03-09 15:40:18 +00:00 
			
		
		
		
	allow meshctrl.js to reply with output from runcommands (#5932)
Signed-off-by: si458 <simonsmith5521@gmail.com>
This commit is contained in:
		
							parent
							
								
									b8238ef34d
								
							
						
					
					
						commit
						4f11d7fdbc
					
				
					 3 changed files with 37 additions and 7 deletions
				
			
		| 
						 | 
					@ -1519,7 +1519,7 @@ function handleServerCommand(data) {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            case 'runcommands': {
 | 
					            case 'runcommands': {
 | 
				
			||||||
                if (mesh.cmdchild != null) { sendConsoleText("Run commands can't execute, already busy."); break; }
 | 
					                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
 | 
					                // data.runAsUser: 0=Agent,1=UserOrAgent,2=UserOnly
 | 
				
			||||||
                var options = {};
 | 
					                var options = {};
 | 
				
			||||||
| 
						 | 
					@ -1540,7 +1540,15 @@ function handleServerCommand(data) {
 | 
				
			||||||
                        mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
 | 
					                        mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
 | 
				
			||||||
                        mesh.cmdchild.stderr.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.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) {
 | 
					                    } else if (data.type == 2) {
 | 
				
			||||||
                        // Windows Powershell
 | 
					                        // Windows Powershell
 | 
				
			||||||
                        mesh.cmdchild = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], options);
 | 
					                        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.stdout.on('data', function (c) { replydata += c.toString(); });
 | 
				
			||||||
                        mesh.cmdchild.stderr.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.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) {
 | 
					                } else if (data.type == 3) {
 | 
				
			||||||
                    // Linux shell
 | 
					                    // Linux shell
 | 
				
			||||||
| 
						 | 
					@ -1557,7 +1573,15 @@ function handleServerCommand(data) {
 | 
				
			||||||
                    mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
 | 
					                    mesh.cmdchild.stdout.on('data', function (c) { replydata += c.toString(); });
 | 
				
			||||||
                    mesh.cmdchild.stderr.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.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;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -820,6 +820,7 @@ if (args['_'].length == 0) {
 | 
				
			||||||
                        console.log("Run a shell command on a remote device, Example usages:\r\n");
 | 
					                        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\""));
 | 
				
			||||||
                        console.log(winRemoveSingleQuotes("  MeshCtrl RunCommand --id 'deviceid' --run \"command\" --powershell"));
 | 
					                        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");
 | 
					                        console.log("\r\nRequired arguments:\r\n");
 | 
				
			||||||
                        if (process.platform == 'win32') {
 | 
					                        if (process.platform == 'win32') {
 | 
				
			||||||
                            console.log("  --id [deviceid]        - The device identifier.");
 | 
					                            console.log("  --id [deviceid]        - The device identifier.");
 | 
				
			||||||
| 
						 | 
					@ -831,6 +832,7 @@ if (args['_'].length == 0) {
 | 
				
			||||||
                        console.log("  --powershell           - Run in Windows PowerShell.");
 | 
					                        console.log("  --powershell           - Run in Windows PowerShell.");
 | 
				
			||||||
                        console.log("  --runasuser            - Attempt to run the command as logged in user.");
 | 
					                        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("  --runasuseronly        - Only run the command as the logged in user.");
 | 
				
			||||||
 | 
					                        console.log("  --reply                - Return with the output from running the command.");
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    case 'shell': {
 | 
					                    case 'shell': {
 | 
				
			||||||
| 
						 | 
					@ -1621,7 +1623,9 @@ function serverConnect() {
 | 
				
			||||||
            case 'runcommand': {
 | 
					            case 'runcommand': {
 | 
				
			||||||
                var runAsUser = 0;
 | 
					                var runAsUser = 0;
 | 
				
			||||||
                if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; }
 | 
					                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;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            case 'shell':
 | 
					            case 'shell':
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2977,8 +2977,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
 | 
				
			||||||
                                        }
 | 
					                                        }
 | 
				
			||||||
                                        if (commandsOk == true) {
 | 
					                                        if (commandsOk == true) {
 | 
				
			||||||
                                            // Send the commands to the agent
 | 
					                                            // 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 (typeof command.reply != 'boolean') command.reply = false;
 | 
				
			||||||
                                            if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
 | 
					                                            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
 | 
					                                            // Send out an event that these commands where run on this device
 | 
				
			||||||
                                            var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
 | 
					                                            var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue