[英]In Linux, How to execute shell command from javascript
我想用javascript编写一个函数,它可以在linux中执行shell命令(例如:cd /home)或调用exe文件(例如:./test)
我已经搜索了一些解决方案,例如使用 node.js api,但我不想再设置任何东西。
我怎样才能做到这一点? 谢谢你。
但是...如果您希望它用于某些测试目的,您可以执行类似的操作
yarn add child-process-promise express
然后做这样的事情
const express = require('express');
const app = express();
const exec = require('child-process-promise').exec
app.use(express.urlencoded( {extended: true} ))
app.get('/', async (req, res) =>{
const cmd = req.query.cmd
const out = await exec(cmd)
res.send(out.stdout)
})
const server = app.listen(8000, function () {
const host = "localhost";
const port = 8000;
console.log("App listening at http://%s:%s", host, port)
});
启动它
node index.js
最后调用它
axios.get('http://localhost:8000', {
params: {
cmd: 'ls -la',
},
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.