ssh-pool
Run remote commands over a pool of server using SSH.
npm install ssh-pool
Usage
const pool = 'user@server1' 'user@server2' { const results = await pool console // 'server1' console // 'server2'}
new Connection(options)
Create a new connection to run command on a remote server.
Parameters:
@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
@param {boolean} [options.asUser] Use a custom user to run command
@param {number} [options.verbosityLevel] SSH verbosity level: 0 (none), 1 (-v), 2 (-vv), 3+ (-vvv)
The remote can use the shorthand syntax or an object:
// You specify user and host remote: 'user@localhost' // You can specify a custom SSH port remote: 'user@localhost:4000' // You can also define remote using an object remote: user: 'user' host: 'localhost' port: 4000 // When defined as an object you can add extra ssh parameters remote: user: 'user' host: 'localhost' port: 4000 extraSshOptions: ServerAliveInterval: '30'
The log method is used to log output directly:
const connection = remote: 'localhost' console connection // Will output:// Running "pwd" on host "localhost".// @localhost /my/directory
connection.run(command, [options])
Run a command on the remote server, you can specify custom childProcess.exec
options.
Parameters:
@param {string} command Command to run
@param {object} [options] Options
@param {boolean} [options.tty] Force a TTY allocation.
@returns {ExecResult}
@throws {ExecError}
// Run "ls" command on a remote serverconnection
connection.copyToRemote(src, dest, [options])
Copy a file or a directory from local to a remote server, you can specify custom childProcess.exec
options. It uses rsync under the hood.
Parameters:
* @param {string} src Source
* @param {string} dest Destination
* @param {object} [options] Options
* @param {string[]} [options.ignores] Specify a list of files to ignore.
* @param {string[]|string} [options.rsync] Specify a set of rsync arguments.
* @returns {ExecResult}
* @throws {ExecError}
// Copy a local file to a remote file using Rsyncconnection
connection.copyFromRemote(src, dest, [options])
Copy a file or a directory from a remote server to local, you can specify custom childProcess.exec
options. It uses rsync under the hood.
Parameters:
* @param {string} src Source
* @param {string} dest Destination
* @param {object} [options] Options
* @param {string[]} [options.ignores] Specify a list of files to ignore.
* @param {string[]|string} [options.rsync] Specify a set of rsync arguments.
* @returns {ExecResult}
* @throws {ExecError}
// Copy a remote file to a local file using Rsyncconnection
new ConnectionPool(connections, [options])
Create a new pool of connections and custom options for all connections. You can use either short syntax or connections to create a pool.
// Use shorthand.const pool = 'server1' 'server2' // Use previously created connections.const connection1 = remote: 'server1' const connection2 = remote: 'server2' const pool = connection1 connection2
Connection Pool accepts exactly the same methods as Connection. It runs commands in parallel on each server defined in the pool. You get an array of results.
isRsyncSupported()
Test if rsync is supported on the local machine.
exec(cmd, options, childModifier)
Execute a command and return an object containing { child, stdout, stderr }
.
License
MIT