结束后将node.js中的exec输出发送给postman

结束后将node.js中的exec输出发送给postman,node.js,express,postman,child-process,Node.js,Express,Postman,Child Process,在postman中,我向我的express服务器发送一个请求,该服务器应执行一个系统命令并将输出存储在一个变量中,并且只有在子进程完成后,我才希望接收包含postman中exec函数输出的响应 app.post('/exploit', function(request, response) { var script = request.body.script; var command = " msfconsole -q -r ~/Desktop/automatio

在postman中,我向我的express服务器发送一个请求,该服务器应执行一个系统命令并将输出存储在一个变量中,并且只有在子进程完成后,我才希望接收包含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);
     });

问题是,无论我尝试什么,postman都会阻塞、或只打印输出的第一行,或显示错误消息,表明无法从服务器获得任何响应。

首先,您的请求处理程序是危险的。您允许将未初始化的用户输入直接放在命令行上

其次,您使用的是
exec()
,而不是使用
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);
        }
    });
});

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

其次,您使用的是
exec()
,而不是使用
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);
        }
    });
});

请显示整个请求处理程序的代码。这不是在node.js中使用
exec()
的正确方法。请显示整个请求处理程序的代码。这不是在node.js中使用
exec()
的正确方法。