Skip to content

Instantly share code, notes, and snippets.

@dca
Last active December 22, 2015 16:08
Show Gist options
  • Select an option

  • Save dca/6496691 to your computer and use it in GitHub Desktop.

Select an option

Save dca/6496691 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var exec = require('child_process').exec,
child;
app.use(express.bodyParser());
app.post('/', function(req, res){
var cmd = req.body.cmd || '';
switch (cmd){
case 'ls':
execCmd('ls -al ~/');
break;
case 'ps':
execCmd('ps aux | grep node');
break;
default:
execCmd('w');
break;
}
execCmd(cmd);
res.send('');
});
app.listen(5001);
function execCmd (argument) {
child = exec(argument,
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment