您需要这样的快速路线:
...
var exec = require('child_process').exec;
app.post('/exec', function(req, res) {
var cmd = req.body.command;
exec(cmd, function(error, stdout, stderr) {
if (stderr || error) {
res.json({
success: false,
error: stderr || error,
command: cmd,
result: null
})
} else {
res.json({
success: true,
error: null,
command: cmd,
result: stdout
})
}
})
})
...
note: stderr 和 stdout 是buffers https://nodejs.org/api/buffer.html.
然后你需要POST
您的命令(使用 AJAX 或表单)/exec
。这会给你一个回复,例如:
Success:
{
success: true,
error: null,
command: "ls",
result: "app.js bin node_modules package.json public routes views "
}
Failure:
{
success: false,
error: "/bin/sh: foobar: command not found ",
command: "foobar",
result: null
}
你需要成为极其不过,当您开放对系统控制台的访问时,请注意安全性。