ssh2-pool

1.0.3 • Public • Published

SSH2Pool Build Status

A library to run multiple ssh commands across multiple machines and get stream or output. It also helps to deal with file transfer across multiple hosts.


Install

npm i ssh2-pool --save


API


### new ServerPool(servers)

ServerPool constructor.

Arguments

  • servers - An object of server pool.

Examples

    var SSH2Pool = require('ssh2-pool');
    
    var servers = 
     {
      ':pool1':['machine1'],
      'machine1':{
        'ssh':{host:'localhost', port:2222, userName:'vagrant',password:'vagrant'}
      }
     };
    
    var pool = new SSH2Pool(servers);
### ServerPool.env(name)

Select a pool of machines.

Arguments

  • name - Name of an environment or a machine.

Returns

  • ServerList - A ServerList object.

Examples

    var SSH2Pool = require('ssh2-pool');
    
    var servers = 
     {
      ':pool1':['machine1'],
      'machine1':{
        'ssh':{host:'localhost', port:2222, userName:'vagrant',password:'vagrant'}
      }
     };
    
    var pool = new SSH2Pool(servers);
    
    pool.env('machine1'); // is a list containing 'machine1' properties
    pool.env(':pool1'); // is also a list containing 'machine1' properties

### ServerList.exec(cmds, onHostComplete, onDone) ##### ServerList.exec(cmds, onDone)

Execute an Array of commands on server pool and return their output.

Arguments

  • cmds - An Array of commands to execute on server pool.
  • onHostComplete(sessionText, server) - A callback called on command line completion.
    • err an Error.
    • sessionText the completed command line response including the command line.
    • server An ssh server credentials object.
  • onDone(sessionText) - A callback called on session completion.
    • sessionErr an Error.
    • sessionText the completed session response.

Examples

    var SSH2Pool = require('ssh2-pool');
    
    var servers = 
     {
      ':pool1':['machine1'],
      'machine1':{
        'ssh':{host:'localhost', port:2222, userName:'vagrant',password:'vagrant'}
      }
     };
    
    var pool = new SSH2Pool(servers);
    
    pool.env(':pool1').exec(['ls','time'], function(sessionErr, sessionText){
        console.log(sessionText);
    });
### ServerList.run(cmd, hostReady)

A command to execute on a server pool and return their streams.

Arguments

  • cmd - A command to execute on server pool.
  • hostReady(err,stdout,stderr,server,conn) - A callback called on command line sent.
    • err isa Boolean.
    • stdout stderr are Streams.
    • server An ssh server credentials object.
    • conn An ssh Client object.

Examples

    var SSH2Pool = require('ssh2-pool');
    
    var servers = 
     {
      ':pool1':['machine1'],
      'machine1':{
        'ssh':{host:'localhost', port:2222, userName:'vagrant',password:'vagrant'}
      }
     };
    
    var pool = new SSH2Pool(servers);
    
    pool.env(':pool1').run('ls', function(err,stdout,stderr){
        if(err) console.log(err);
        stdout.on('data', function(){
            console.log(''+data);
        });
        stderr.on('data', function(){
            console.log(''+data);
        });
        stdout.on('close',function(){
            conn.end();
        });
    });
### ServerList.putFile(localFile, remotePath, then)

Put local file on a remote hosts of a server pool.

Arguments

  • localFile - A local file path to read.
  • remotePath - A remote file path to write.
  • then(err) - A callback called once all files sent.
    • err is an Error.

Examples

    var SSH2Pool = require('ssh2-pool');
    
    var servers = 
     {
      ':pool1':['machine1'],
      'machine1':{
        'ssh':{host:'localhost', port:2222, userName:'vagrant',password:'vagrant'}
      }
     };
    
    var pool = new SSH2Pool(servers);
        
    var localFile = '/tmp/from_local_path';
    var remotePath = '/tmp/to_remote_path';
    
    pool.env(':pool1').putFile(localFile, remotePath, function(err){
        if(err) console.log(err);
        console.log('done');
    });
### ServerList.putDir(localFile, remotePath, then)

Put local directory on a remote hosts of a server pool.

Arguments

  • localDirectoryPath - A local directory path to read.
  • remotePath - A remote file path to write.
  • then(err) - A callback called once all files sent.
    • err is an Error.

Examples

    var SSH2Pool = require('ssh2-pool');
    
    var servers = 
     {
      ':pool1':['machine1'],
      'machine1':{
        'ssh':{host:'localhost', port:2222, userName:'vagrant',password:'vagrant'}
      }
     };
    
    var pool = new SSH2Pool(servers);
        
    var localDirectoryPath = '/tmp/from_local_path';
    var remotePath = '/tmp/to_remote_path';
    
    pool.env(':pool1').putDir(localDirectoryPath, remotePath, function(err){
        if(err) console.log(err);
        console.log('done');
    });

Suggestions

On linux you may want to edit /etc/ssh/ssh_config and append

Host 127.0.0.1
   CheckHostIP no
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null

This will help to have multiple vagrant box installed on the same machine.


Status

In development. It needs some tests. It misses putFile and readDir implementations.

Package Sidebar

Install

npm i ssh2-pool

Weekly Downloads

0

Version

1.0.3

License

WTF

Last publish

Collaborators

  • maboiteaspam