I want to write a function in javascript which can execute shell command in linux (example: cd /home) or call an exe file (example: ./test)
I have already searched some solution such as using node.js api but I don’t want to setup anything more.
How can I achieve this? Thank you.
However... If you want it to use it for some testing purposes, you could do something like
yarn add child-process-promise express
Then do something like this
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)
});
Start it
node index.js
And finally call it
axios.get('http://localhost:8000', {
params: {
cmd: 'ls -la',
},
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});