0

I'm attempting to make a small node executable which has the ability to open a local shell and send and receive commands to a HTTP server.

Seems simple enough logically but what would be the best practice in implementing this?

  1. Open a command line using Node API
  2. Listen for commands from server URL /xmlrpc-urlhere/
  3. When a command is received via Ajax or WebRTC? Execute it in command line
  4. POST the command line response back to the user

This shell will be for administrative purposes.

I can't think of a good way of doing this, I've checked npm and it would seem there are some basic command line modules for node js but nothing that has this functionality.

mscreeni
  • 21
  • 4

1 Answers1

0

The npm modules commander and request ought to do the trick. To get you started, here's a simple command line tool that issues a search to google and dumps the raw HTML into the console.

Eg:

./search.js puppies

#!/usr/bin/env node

var program = require('commander');
var request = require('request');
var search;

program
    .version('0.0.1')
    .arguments('<query>')
    .action(function(query) {
        search = query;
    });

program.parse(process.argv);

if (typeof search === 'undefined') {
    console.error('no query given!');
    process.exit(1);
}

var url = 'http://google.com/search?q=' + encodeURIComponent(search);
request(url, function(err, res, body) {
    if (err) {
        console.error(err);
        process.exit(1);
    }

    console.log('Search result:');
    console.log(body);
    process.exit(0);
});
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50
  • 1
    Would this method be able to keep command / shell sessions open until user input is given? A scenario where this is important is if a command requires user confirmation (Yes/No verification). If I am not mistaken this would send a response then close the process. Thanks for your suggestion so far btw. – mscreeni Apr 26 '15 at 10:46