Saturday, 22 April 2017

How to execute loacl server linux command on terminal using Node.js?

I am trying to execute a Node.js script for Motion.ai chatbot. The script should execute a certain command on local or remote Linux server from user queries on the bot and parse the result to reply. Being a noob with Node.js, I am looking for a good point to start. The child_process might help, but pursing remote server data seems a bit challenging. The following script returns the following:

// http://nodejs.org/api.html#_child_processes

var sys = require('sys')

var exec = require('child_process').exec;

var child;

// executes `pwd`

child = exec("pwd", function (error, stdout, stderr) {

  sys.print('stdout: ' + stdout);

  sys.print('stderr: ' + stderr);

  if (error !== null) {

    console.log('exec error: ' + error);

  }

});

// or more concisely

var sys = require('sys')

var exec = require('child_process').exec;

function puts(error, stdout, stderr) { sys.puts(stdout) }

exec("ls -la", puts);



via nocturnal_abu

No comments:

Post a Comment