GithubHelp home page GithubHelp logo

ssh-pool's Introduction

ssh-pool

Build Status Dependency Status devDependency Status

Run remote commands over a pool of server using SSH.

Install

npm install ssh-pool

Usage

var sshPool = require('ssh-pool');

var pool = new sshPool.ConnectionPool(['user@server1', 'user@server2']);

pool.run('hostname')
.then(function (results) {
  console.log(results[0].stdout); // 'server1'
  console.log(results[1].stdout); // 'server2'
});

new Connection(options)

Create a new connection to run command on a remote server.

Arguments:

@param {object} options Options
@param {string|object} options.remote Remote
@param {Stream} [options.stdout] Stdout stream
@param {Stream} [options.stderr] Stderr stream
@param {string} [options.key] SSH key
@param {function} [options.log] Log method

The remote can use the shorthand syntax or an object:

// Default user will be deploy and ssh default port.
new Connection({remote: 'localhost'});

// Default ssh port will be used.
new Connection({remote: 'user@localhost'});

// Custom user and custom port.
new Connection({remote: 'user@localhost:22'});

// Object syntax.
new Connection({remote: {user: 'user', host: 'localhost', port: 22}});

The log method is used to log output directly:

var connection = new Connection({
  remote: 'localhost',
  log: console.log.bind(console)
});

connection.run('pwd');

// Will output:
// Running "pwd" on host "localhost".
// @localhost /my/directory

connection.run(command, [options], [cb])

Run a command on the remote server, you can specify custom childProcess.exec options. A callback or a promise can be used.

Arguments:

@param {string} command Command
@param {object} [options] Exec options
@param {function} [cb] Callback
@returns {Promise}
connection.run('ls', {env: {NODE_ENV: 'test'}})
.then(function (result) {
  result.stdout; // stdout output
  result.stderr; // stderr output
  result.child; // child object
});

connection.copy(src, dest, [options], [cb])

Copy a file or a directory to a remote server, you can specify custom childProcess.exec options. A callback or a promise can be used.

Arguments:

@param {string} src Source
@param {string} dest Destination
@param {object} [options] Exec Options
@param {function} [cb] Callback
@returns {Promise}
connection.copy('./localfile', '/remote-file', {env: {NODE_ENV: 'test'}})
.then(function (result) {
  result.stdout; // stdout output
  result.stderr; // stderr output
  result.child; // child object
});

new ConnectionPool(connections, [options])

Create a new pool of connections and custom options for all connections.

If you use the short syntax, connections will be automatically created, else you can use previous created connections.

// Use shorthand.
var pool = new ConnectionPool(['server1', 'server2']);

// Use previously created connections.
var connection1 = new Connection({remote: 'server1'});
var connection2 = new Connection({remote: 'server2'});
var pool = new ConnectionPool([connection1, connection2]);

pool.run(command, [options], [cb])

Same as connection.run, except that the command is executed in parallel on each server of the pool.

Arguments:

@param {string} command Command
@param {object} [options] Options
@param {function} [cb] Callback
@returns {Promise}
pool.run('hostname')
.then(function (results) {
  // ...
});

pool.copy(src, dest, [options], [cb])

Same as connection.copy, except that the copy is done in parallel on each server of the pool.

Options:

@param {object} [options.direction] Direction of copy

Also all exec options are supported.

Arguments:

@param {string} src Source
@param {string} dest Destination
@param {object} options Options
@param {function} [cb] Callback
@returns {Promise}
pool.copy('./localfile', '/remote-file')
.then(function (results) {
  // ...
});

License

MIT

ssh-pool's People

Contributors

goz avatar gregberge avatar ryantemple avatar sgtlambda avatar timkelty avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.