Run given command remotely via SSH

Run given command remotely via SSH

remotely = require('remotely')
remotely('user@hostname', 'echo $HOME').stdout.pipe(process.stdout)
// => /home/username 
$ npm install remotely

If callbacks work better than piping in your case, pass a function as last parameter:

remotely = require('remotely')
remotely('user@hostname', 'echo $HOME', function (errorstdoutstderr) {
  // => /home/username 

You can pipe commands to call:

remotely = require('remotely')('')

See example.js for a working example

Remotely has a similar interface to child_process.exec. You can pass child options as third parameter:

remotely('user@hostname', 'echo $HOME', { timeout: 2000 }).stdout.pipe(process.stdout)

See test.js and example.js for more information.

To specify SSH options like UserKnownHostsFile:

remotely('user@host', 'pwd', { options: { UserKnownHostsFile: '/dev/null' } }).stdout.pipe(process.stdout)

You can specify a custom identity file:

remotely('user@host', 'pwd', { identity: '/home/azer/ssh-foo/.id_rsa' }).stdout.pipe(process.stdout)
DEBUG=remotely:* node yourscript.js

Test module requires a hostname as third parameter.

$ node test.js user@hostname