javascript - Spawn process on remote host in NodeJS -


i have simple node server serves page button. on click button needs execute command on remote host. when command finishes, output should given response of function , shown in html.

currently server code:

app.use(logger("combined")); app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json());  app.get('/index.html', function (req, res) {     res.sendfile( __dirname + "/" + "index.html" ); })  app.post('/connect', function (req, res) {     var spawn = require('cross-spawn-async');     var mpiproc = spawn('ssh','pi@raspi2','"ls"', { stdio: ['pipe','pipe',2,'pipe'] });      var grid = "...8.1..........435............7.8........1...2..3....6......75..34........2..6..";     str = "";     mpiproc.stdout.on('data', function(data) {         console.log(data);         str = data.tostring();         res.write('data: ' + json.stringify({ msg : str }) + '\n\n');         // output            });      mpiproc.on('close', function(code) {         res.end(str);         // script exit code     });      mpiproc.on('error', function(code) {         res.end('stderr: ' + code);         // script exit code     }); })      require('http').createserver(app).listen(3000, function(){     console.log('listening on 3000'); }); 

as can see following call tries execute 'ls' command on remote host:

var mpiproc = spawn('ssh','pi@raspi2','"ls"', { stdio: ['pipe','pipe',2,'pipe'] }); 

but doesn't return anything, while executed server directly terminal, returns list of directory content. test if command works way spawn tried doing this:

var mpiproc = spawn('ls', { stdio: ['pipe','pipe',2,'pipe'] }); 

which returns expected, list of directory content on server. how can create pipe remote host execute command , store result of command on server?

the second argument passed spawn() array of arguments. also, don't need manually quote arguments. should work:

spawn('ssh', ['pi@raspi2', 'ls'], { stdio: ['pipe', 'pipe', 2, 'pipe'] }); 

lastly, if want more programmatic (and lightweight) control on ssh connection, there ssh2 module (or of modules build on top of it), not use child processes.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -