[英]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.