Skip to main content

Asynchronous parallel SSH library

Project description

Asynchronous parallel SSH client library.

Run SSH commands over many - hundreds/hundreds of thousands - number of servers asynchronously and with minimal system load on the client host.

Native code based client with extremely high performance - based on libssh2 C library.

License Latest Version https://travis-ci.org/ParallelSSH/parallel-ssh.svg?branch=master https://ci.appveyor.com/api/projects/status/github/parallelssh/parallel-ssh?svg=true&branch=master https://codecov.io/gh/ParallelSSH/parallel-ssh/branch/master/graph/badge.svg https://img.shields.io/pypi/wheel/parallel-ssh.svg Latest documentation

Installation

pip install parallel-ssh

Usage Example

See documentation on read the docs for more complete examples.

Run uname on two remote hosts in parallel with sudo.

from __future__ import print_function

from pssh.clients import ParallelSSHClient

hosts = ['myhost1', 'myhost2']
client = ParallelSSHClient(hosts)

output = client.run_command('uname')
for host, host_output in output.items():
    for line in host_output.stdout:
        print(line)
Output:
Linux
Linux

Native client

Starting from version 1.2.0, a new client is supported in parallel-ssh which offers much greater performance and reduced overhead than the current default client.

The new client is based on libssh2 via the ssh2-python extension library and supports non-blocking mode natively. Binary wheel packages with libssh2 included are provided for Linux, OSX and Windows platforms and all supported Python versions.

See this post for a performance comparison of the available clients.

To make use of this new client, ParallelSSHClient can be imported from pssh.clients.native instead. Their respective APIs are almost identical.

The new client will become the default and will replace the current pssh.pssh_client in a new major version of the library - 2.0.0.

The paramiko based client will become an optional install via pip extras, available under pssh.clients.miko.

For example:

from pprint import pprint
from pssh.clients.native import ParallelSSHClient

hosts = ['myhost1', 'myhost2']
client = ParallelSSHClient(hosts)

output = client.run_command('uname')
for host, host_output in output.items():
    for line in host_output.stdout:
        print(line)

See documentation for a feature comparison of the two clients.

Native Code Client Features

  • Highest performance and least overhead of any Python SSH libraries

  • Thread safe - makes use of native threads for blocking calls like authentication

  • Natively non-blocking utilising libssh2 via ssh2-python - no monkey patching of the Python standard library

  • Significantly reduced overhead in CPU and memory usage

Exit codes

Once either standard output is iterated on to completion, or client.join(output) is called, exit codes become available in host output. Iteration ends only when remote command has completed, though it may be interrupted and resumed at any point.

for host in output:
    print(output[host].exit_code)
Output:
0
0

The client’s join function can be used to wait for all commands in output object to finish:

client.join(output)

Similarly, output and exit codes are available after client.join is called:

from pprint import pprint

output = client.run_command('exit 0')

# Wait for commands to complete and gather exit codes.
# Output is updated in-place.
client.join(output)
pprint(output.values()[0].exit_code)

# Output remains available in output generators
for host, host_output in output.items():
    for line in host_output.stdout:
        pprint(line)
Output:
0
<..stdout..>

There is also a built in host logger that can be enabled to log output from remote hosts. The helper function pssh.utils.enable_host_logger will enable host logging to stdout.

To log output without having to iterate over output generators, the consume_output flag must be enabled - for example:

from pssh.utils import enable_host_logger

enable_host_logger()
client.join(client.run_command('uname'), consume_output=True)
Output:
[localhost]       Linux

SFTP

SFTP is supported natively.

To copy a local file to remote hosts in parallel:

from pssh.clients import ParallelSSHClient
from pssh.utils import enable_logger, logger
from gevent import joinall

enable_logger(logger)
hosts = ['myhost1', 'myhost2']
client = ParallelSSHClient(hosts)
cmds = client.copy_file('../test', 'test_dir/test')
joinall(cmds, raise_error=True)
Output:
Copied local file ../test to remote destination myhost1:test_dir/test
Copied local file ../test to remote destination myhost2:test_dir/test

There is similar capability to copy remote files to local ones suffixed with the host’s name with the copy_remote_file function.

Directory recursion is supported in both cases via the recurse parameter - defaults to off.

See SFTP documentation for more examples.

Design And Goals

parallel-ssh’s design goals and motivation are to provide a library for running non-blocking asynchronous SSH commands in parallel with little to no load induced on the system by doing so with the intended usage being completely programmatic and non-interactive.

To meet these goals, API driven solutions are preferred first and foremost. This frees up developers to drive the library via any method desired, be that environment variables, CI driven tasks, command line tools, existing OpenSSH or new configuration files, from within an application et al.

Comparison With Alternatives

There are not many alternatives for SSH libraries in Python. Of the few that do exist, here is how they compare with parallel-ssh.

As always, it is best to use a tool that is suited to the task at hand. parallel-ssh is a library for programmatic and non-interactive use - see Design And Goals. If requirements do not match what it provides then it best not be used. Same applies for the tools described below.

Paramiko

The default SSH client library in parallel-ssh 1.x.x series.

Pure Python code, while having native extensions as dependencies, with poor performance and numerous bugs compared to both OpenSSH binaries and the libssh2 based native clients in parallel-ssh 1.2.x and above. Recent versions have regressed in performance and have blocker issues.

It does not support non-blocking mode, so to make it non-blocking monkey patching must be used which affects all other uses of the Python standard library. However, some functionality like Kerberos (GSS-API) authentication is not provided by other libraries.

asyncssh

Python 3 only asyncio framework using client library. License (EPL) is not compatible with GPL, BSD or other open source licenses and combined works cannot be distributed.

Therefore unsuitable for use in many projects, including parallel-ssh.

Fabric

Port of Capistrano from Ruby to Python. Intended for command line use and is heavily systems administration oriented rather than non-interactive library. Same maintainer as Paramiko.

Uses Paramiko and suffers from the same limitations. More over, uses threads for parallelisation, while not being thread safe, and exhibits very poor performance and extremely high CPU usage even for limited number of hosts - 1 to 10 - with scaling limited to one core.

Library API is non-standard, poorly documented and with numerous issues as API use is not intended.

Ansible

A configuration management and automation tool that makes use of SSH remote commands. Uses, in parts, both Paramiko and OpenSSH binaries.

Similarly to Fabric, uses threads for parallelisation and suffers from the poor scaling that this model offers.

See The State of Python SSH Libraries for what to expect from scaling SSH with threads, as compared to non-blocking I/O with parallel-ssh.

Again similar to Fabric, its intended and documented use is interactive via command line rather than library API based. It may, however, be an option if Ansible is already being used for automation purposes with existing playbooks, the number of hosts is small, and when the use case is interactive via command line.

parallel-ssh is, on the other hand, a suitable option for Ansible as an SSH client that would improve its parallel SSH performance significantly.

ssh2-python

Wrapper to libssh2 C library. Used by parallel-ssh as of 1.2.0 and is by same author.

Does not do parallelisation out of the box but can be made parallel via Python’s threading library relatively easily and as it is a wrapper to a native library that releases Python’s GIL, can scale to multiple cores.

parallel-ssh uses ssh2-python in its native non-blocking mode with event loop and co-operative sockets provided by gevent for an extremely high performance library without the side-effects of monkey patching - see benchmarks.

In addition, parallel-ssh uses native threads to offload CPU blocked tasks like authentication in order to scale to multiple cores while still remaining non-blocking for network I/O.

pssh.clients.native.SSHClient is a single host natively non-blocking client for users that do not need parallel capabilities but still want a non-blocking client with native code performance.

Out of all the available Python SSH libraries, libssh2 and ssh2-python have been shown, see benchmarks above, to perform the best with the least resource utilisation and ironically for a native code extension the least amount of dependencies. Only libssh2 C library and its dependencies which are included in binary wheels.

However, it lacks support for some SSH features present elsewhere like ECDSA keys (PR pending), agent forwarding (PR also pending) and Kerberos authentication - see feature comparison.

Scaling

Some guide lines on scaling parallel-ssh and pool size numbers.

In general, long lived commands with little or no output gathering will scale better. Pool sizes in the multiple thousands have been used successfully with little CPU overhead in the single thread running them in these use cases.

Conversely, many short lived commands with output gathering will not scale as well. In this use case, smaller pool sizes in the hundreds are likely to perform better with regards to CPU overhead in the event loop.

Multiple Python native threads, each of which can get its own event loop, may be used to scale this use case further as number of CPU cores allows. Note that parallel-ssh imports must be done within the target function of the newly started thread for it to receive its own event loop. gevent.get_hub() may be used to confirm that the worker thread event loop differs from the main thread.

Gathering is highlighted here as output generation does not affect scaling. Only when output is gathered either over multiple still running commands, or while more commands are being triggered, is overhead increased.

Technical Details

To understand why this is, consider that in co-operative multi tasking, which is being used in this project via the gevent library, a co-routine (greenlet) needs to yield the event loop to allow others to execute - co-operation. When one co-routine is constantly grabbing the event loop in order to gather output, or when co-routines are constantly trying to start new short-lived commands, it causes contention with other co-routines that also want to use the event loop.

This manifests itself as increased CPU usage in the process running the event loop and reduced performance with regards to scaling improvements from increasing pool size.

On the other end of the spectrum, long lived remote commands that generate no output only need the event loop at the start, when they are establishing connections, and at the end, when they are finished and need to gather exit codes, which results in practically zero CPU overhead at any time other than start or end of command execution.

Output generation is done remotely and has no effect on the event loop until output is gathered - output buffers are iterated on. Only at that point does the event loop need to be held.

User’s group

There is a public ParallelSSH Google group setup for this purpose - both posting and viewing are open to the public.

https://ga-beacon.appspot.com/UA-9132694-7/parallel-ssh/README.rst?pixel

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parallel-ssh-1.6.2.tar.gz (98.6 kB view details)

Uploaded Source

Built Distributions

parallel_ssh-1.6.2-cp36-cp36m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.6m Windows x86-64

parallel_ssh-1.6.2-cp36-cp36m-win32.whl (955.4 kB view details)

Uploaded CPython 3.6m Windows x86

parallel_ssh-1.6.2-cp36-cp36m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m

parallel_ssh-1.6.2-cp35-cp35m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.5m Windows x86-64

parallel_ssh-1.6.2-cp35-cp35m-win32.whl (955.0 kB view details)

Uploaded CPython 3.5m Windows x86

parallel_ssh-1.6.2-cp35-cp35m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.5m

parallel_ssh-1.6.2-cp34-cp34m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.4m Windows x86-64

parallel_ssh-1.6.2-cp34-cp34m-win32.whl (956.7 kB view details)

Uploaded CPython 3.4m Windows x86

parallel_ssh-1.6.2-cp34-cp34m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.4m

parallel_ssh-1.6.2-cp27-cp27mu-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 2.7mu

parallel_ssh-1.6.2-cp27-cp27m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 2.7m Windows x86-64

parallel_ssh-1.6.2-cp27-cp27m-win32.whl (957.0 kB view details)

Uploaded CPython 2.7m Windows x86

parallel_ssh-1.6.2-cp27-cp27m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 2.7m

parallel_ssh-1.6.2-cp27-cp27m-macosx_10_13_x86_64.whl (1.3 MB view details)

Uploaded CPython 2.7m macOS 10.13+ x86-64

parallel_ssh-1.6.2-cp27-cp27m-macosx_10_12_x86_64.whl (1.3 MB view details)

Uploaded CPython 2.7m macOS 10.12+ x86-64

parallel_ssh-1.6.2-cp27-cp27m-macosx_10_11_x86_64.whl (1.3 MB view details)

Uploaded CPython 2.7m macOS 10.11+ x86-64

parallel_ssh-1.6.2-cp27-cp27m-macosx_10_10_intel.whl (1.3 MB view details)

Uploaded CPython 2.7m macOS 10.10+ intel

File details

Details for the file parallel-ssh-1.6.2.tar.gz.

File metadata

File hashes

Hashes for parallel-ssh-1.6.2.tar.gz
Algorithm Hash digest
SHA256 f85cca88d55b19c9c06454326ce7bfec7093c1e32cb375f5ee2aabc39fc15ba0
MD5 bc9cbbcb9982b25f445238ad5b34780b
BLAKE2b-256 76503749deee17c4538534df9e39968f4b1b49171a733e88b33804ea1a2ebeac

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 605fd33647e9cb3fd7795e4c5441307baaf617678986cc1426dcc95c6cb45b04
MD5 5aacb56e87bedd479ee3d5504e3cb270
BLAKE2b-256 615ac6e69b9de0141a79f2b4136287ada3cf479f328891eea79dab6a296492e9

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 c3229b6d2de30c8b2971dcd4e2f5eb4100cf736fc4ea571f20b94aa178ccc741
MD5 e3cee6ee957bd0155b25e5f2d1e0e22c
BLAKE2b-256 8e683a4023a4fcfd748ee94b515e3254138af2823cb119e04f82d514e3b00d4f

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c3523dfa5f8da4d3366e28bef4c6bcf2850ff4da3fc9fe59837967acfa88fe4d
MD5 8ce3cf64f6eb1346aaea76cf8525c94c
BLAKE2b-256 cc0281f1074b05f11dd9f882e0b7ef0ad18918828d23fb97041796456e5f7dc7

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 23525dc9db0e8cac0eb01cfbca34f3ca48c09cf29e4f285c29e231e55779463e
MD5 e51ef528ee949a1ee748372806cfe70e
BLAKE2b-256 2bde43ed88995de2a435e3525582f07a863c5656cc80021c73a7536871f350e6

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 29918eff42c3a47aede4ac20abb2bc56ee482b03c5726fd09b0892ee540dad40
MD5 85404dc5cce276b46baec95f46df7052
BLAKE2b-256 57d4c40319f4ab2c177e7cb65dbf68fc18be91fc9908aa33c45dcfa9abd7c49c

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9ccdaa71754de932ad9144ca4df429f06386a3d20ce057b6cb06f7f056fcf2b8
MD5 c4776ee252484fa0923b091344cc7a53
BLAKE2b-256 4a73d8e1efc4ab984b2a2f4e3533548cc0004df529e734f3e01019f67da3f3a2

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 0a3b75fc901313c072fec690b2b021cb4bc64d0da9efdc748c303d79d35c36a6
MD5 a955fcdcb4da84e721d5ead4a5774bdd
BLAKE2b-256 1b3c8a660d9bbd0b4fc3446679d49e85aa9a41c38d13aa5319fef9f41165503a

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 9661ac2204e1ba4ecef89d9eef7a181a0d9f68377bd4ec027a7e314c3bb62281
MD5 bbe827d1244decec575b89399d1a1bc7
BLAKE2b-256 3b2dbfaec20890df149020d850b8dd296ebe8ed199b86d00311a765080109b5a

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 57a446046f7f9b4aa1d94eebdb0ded1ef96ae5fbfd4be433064ded188fe93c2a
MD5 e80599db97001d8b220387873f068e85
BLAKE2b-256 3d15bd949d083e18e46a31933cff72f3fa81a9b1d06bb360162b2b8aebbabad5

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff6e29ab71f4efe0323fded99acaca4da7458e48d532c72e5e42f0df8148b860
MD5 52dff750bc964db102ba00b9d419d2f6
BLAKE2b-256 9d715b2120ab6b9707f3d964dd9aad6561684fbd69525e9715c62f811e176b8e

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 f6c567acb3409e25bd5e4313953ea4bacdb1bcf08b71584cae55d75b963b6f50
MD5 febd241ab2533322a51dbf53a0680f3a
BLAKE2b-256 03e388e1725f6408129c7b5273a1056e9c35a3baef995d3b681338f1d80dbcc2

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 8d4dde86c968ff1b48c67ef36062864ffaf2f16c04bb1c0666148fcd5697fc58
MD5 a2945081c35e8ef256db19a1954a6986
BLAKE2b-256 7493a29a47a3fbcd469ea74dbb5a31c0274d24ec152d10e94dfd3887fe688aad

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d0419d69732655afd231bc2ed7851d9d80583eec1d3ec5b53e9616bb54ae0d7e
MD5 e82cf6d85c04c1a7cff2ca57456691a9
BLAKE2b-256 a401607641e263b3512b0c2d8b463669250521b116fbabb3fe6c899ee8578ed0

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 c8c43b13ea295ebf8b3ac9d275d8f86e8495c8dfc4c0418fb153c90a68f0eb44
MD5 7afe9ef2e8bbce5e0e6fbd92d08d7c55
BLAKE2b-256 462ddbe0aaaa57c69c3b9cbcc9353e7ddab59a46311bb1918792ab07311f6588

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8c908e961702b53b7b537c7d8d2836c60943344dde81f0c312614c66b0acade3
MD5 0be1205a0a9076c4fa5826622f8474af
BLAKE2b-256 3f019ec3a1f526f8c6cfd97f4427442981a78419809c7f5cf85c783e3d650231

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 503880ac94099bd2f815f79c79430239d1816d1647369d93fd01be65bb3c2385
MD5 e3e31d6c60be991ea5414cf48c447e81
BLAKE2b-256 9528018e93835a3979d57ce47230236b400e8060eaaf48c98f13f615631837d4

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.6.2-cp27-cp27m-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.6.2-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 f81c92e215878b10c8e117369f8c10925972939314139969fb36e4d68bff2ca4
MD5 dd88206966cc0fbbd4b8f947c579e44a
BLAKE2b-256 e5997817cab8de59f983bd902fcd03acae24239101fe8979d37c99c83c50c68d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page