在 node.js 结束后将 exec 的输出发送给邮递员

send the output of exec in node.js to postman after it ends

在邮递员中,我正在向我的快递服务器发送一个请求,它应该执行一个系统命令并将输出存储在一个变量中,只有在子进程完成后我才想接收包含输出的响应postman 中的 exec 函数。

app.post('/exploit', function(request, response) {            
  var script = request.body.script;
  var command = " msfconsole -q -r ~/Desktop/automation/meterpreter.rc ;                    
  python "+script;
   var child = exec(command);
        child.stdout.setEncoding('utf8');
        child.stdout.on('data', function(data) {

            console.log('stdout: ' + data);
            data=data.toString();
            scriptOutput+=data;

    });
    function finaloutput() {
        response.end(scriptOutput);
      }
    setTimeout(finaloutput, 180000);
     });

问题是无论我尝试什么,邮递员要么阻塞,要么只打印输出的第一行,要么显示一条错误消息,表明它无法从服务器获得任何响应。

首先,您的请求处理程序很危险。您允许将未经过滤的用户输入直接放在命令行上。

其次,您使用 exec() 的方式与使用 spawn() 的方式不同,而不是您使用 exec() 的方式。 Exec 缓冲所有输出,等到程序完成,然后像这样一次性给你所有输出:

app.post('/exploit', function(request, response) {
    // note this is dangerous to put unsanitized user input directly onto the command line
    var script = request.body.script;
    var command = " msfconsole -q -r ~/Desktop/automation/meterpreter.rc ; python " + script;
    exec(command, function(error, stdout, stderr) {
        if (error) {
            console.log(error);
            response.sendStatus(500);
        } else {
            response.send(stdout);
        }
    });
});