Skip to main content

Asynchronous parallel SSH library

Project description

Non-blocking, 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.pssh_client 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.pssh2_client 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 - once remaining features have been implemented.

The current default client will remain available as an option under a new name.

For example:

from pprint import pprint
from pssh.pssh2_client 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.pssh_client 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.ssh2_client.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

This version

1.5.1

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.5.1.tar.gz (93.0 kB view details)

Uploaded Source

Built Distributions

parallel_ssh-1.5.1-cp36-cp36m-win_amd64.whl (106.0 kB view details)

Uploaded CPython 3.6m Windows x86-64

parallel_ssh-1.5.1-cp36-cp36m-win32.whl (96.6 kB view details)

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.6m

parallel_ssh-1.5.1-cp35-cp35m-win_amd64.whl (105.6 kB view details)

Uploaded CPython 3.5m Windows x86-64

parallel_ssh-1.5.1-cp35-cp35m-win32.whl (96.3 kB view details)

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.5m

parallel_ssh-1.5.1-cp34-cp34m-win_amd64.whl (103.7 kB view details)

Uploaded CPython 3.4m Windows x86-64

parallel_ssh-1.5.1-cp34-cp34m-win32.whl (97.2 kB view details)

Uploaded CPython 3.4m Windows x86

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

Uploaded CPython 3.4m

parallel_ssh-1.5.1-cp33-cp33m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.3m

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

Uploaded CPython 2.7mu

parallel_ssh-1.5.1-cp27-cp27m-win_amd64.whl (104.8 kB view details)

Uploaded CPython 2.7m Windows x86-64

parallel_ssh-1.5.1-cp27-cp27m-win32.whl (96.9 kB view details)

Uploaded CPython 2.7m Windows x86

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

Uploaded CPython 2.7m

parallel_ssh-1.5.1-cp27-cp27m-macosx_10_12_x86_64.whl (1.2 MB view details)

Uploaded CPython 2.7m macOS 10.12+ x86-64

parallel_ssh-1.5.1-cp27-cp27m-macosx_10_11_x86_64.whl (1.2 MB view details)

Uploaded CPython 2.7m macOS 10.11+ x86-64

parallel_ssh-1.5.1-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.5.1.tar.gz.

File metadata

File hashes

Hashes for parallel-ssh-1.5.1.tar.gz
Algorithm Hash digest
SHA256 486ecacd97a365899e433954120c5fc6d6ceb0c1b63df7f9c4dce07d32cabe7b
MD5 66b3bdab6e16c3c758907c1ec81adfcd
BLAKE2b-256 5febf91ac409b726b788dac305de5a177d96d7c2dc8ddd986479224249cc505e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 e9808cd0a0d0fd87515aea297aa6bad3084fe4e9b8ae4dfc94bc4094dd669ef0
MD5 c519c8d26481d6375547a2b4b9808f54
BLAKE2b-256 c046459dfc4b5b05b6721a0f802de9ae8b1f61764c154ce1afaa34b9cb43cdfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 fcc1d535e2d58805fb12bfd1a3e0cada42d16e08fb13282e2227ad82ee6e5d2e
MD5 f9177b5c35b98db055d25a9206cddd72
BLAKE2b-256 83d33a2f5941e5e95b985c5f20f9538ba4d950bc98f8899b6f0aecefeb8e06cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b17581ddd17f320968c4dd2109f348f2055433facf7d621482579c9b39f16ed1
MD5 5078326e9c01f43d7dcc65ceabc3c97a
BLAKE2b-256 5bdd9dec716d18b1e1d65aed08c2f4bf3e94279092313f09a96ece94811d7a73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 bea81e7f6e0ea50a6936e8b60017dd60840f495909ff21682ea14d9260047780
MD5 81bdee5abf93d2307ee6ee0ae35d3b74
BLAKE2b-256 13e3f25df5f7d846db4514ea343706b49105de70a5197ead6ca1dc305e0ee1d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 a1d8574c536982b1eb611e9685637f7f8505fed5e97dfe93c29a9b7ca0e30014
MD5 2600ae9d565170b5b2f3f824801425d8
BLAKE2b-256 480bac780cb35ad1c417594c51fb2231ea6c735c29f51d51fc2389459cbeb1d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 61ae1446c3950d1422d14ebc4e0fe01f6f1ee2543681efc07ae8f78ef6904ba4
MD5 b54a6f5f0a51ce9055b0419e286d1438
BLAKE2b-256 67e4d2c22b378a51bc822321749ffdf8a09e53383be3b85786e783a1c2cc9964

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 71328a94de41ea89a15dcf74979b18d557c41f1a64678089f7b177d53c7fe923
MD5 b717e4a6c668424ba7d5841c8788efdc
BLAKE2b-256 3282c92e1ee1bd7e183929ed08edd728c1931308b7c7a984ed58a5bde0f0705b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 fa9c3357d63856e7c0f3ed2dc63585716e03140a312c1ec7438587e16215b5d9
MD5 a3793bdf1d1a4806a9858fea70ab4778
BLAKE2b-256 d508fdf04b1dadefbbe23dcc18add0475a07df71d50add7f8d78262c83d64b80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0f59a330271bd4f9bc31d44d42ceaa1328fd11ae275526108adb35ddd540a020
MD5 e7dd70c69e962db3f999f5d4493ea013
BLAKE2b-256 187c2c0e22b5b46e7e7341248cdf653ef825b12d25d0dc61d357d040bce0e412

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.5.1-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 296be48cf5946f05fe76f202a3a6dc1241c2e54310d11eb103c0ed50afb7daf8
MD5 0dc5affd3a87dda17ad49855e293db57
BLAKE2b-256 81317cb82d0813df87d6ffe335cb89d6639e2ee68cf6da50fcf1903296a257ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3c0cee5096b300740f59e7a91d603bded561d8941be7a57075425ec7ea9a2134
MD5 cf7800c7345c3a8a52c97ace2ac86bdf
BLAKE2b-256 d035378cedf6f05e043a5925514405d8313fba8b3ed45fa130ee64b4df537f62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 506e905127a3ce3e77652bc62c3bb8f6daee9594ad54345daed505ab450a4d8d
MD5 0e5e0bd7bbd25609a25f1cfa9db79ff4
BLAKE2b-256 bd767de31de4a6f549bc1a750dc394fcd623677610aec7b953f42c6ac8db9e06

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 315b76539621d826fdfcfc696140a89f06cce432d7efa336d4e455f609eaa59d
MD5 5c54ef6d787bb694410d49611a5129e4
BLAKE2b-256 0dccf5a7881967746883071c9ac3b9fd982787d762340be53b217bb9d6ca68ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 04907d2168fa8dab107800dd804869cd29233e3fefcb72d7a818f7111a6e8c8e
MD5 1372d17c60af7dc2bcf7ee9848004a9b
BLAKE2b-256 da6ae42a07128cb2947363b2b8848b9bcadbc31c30ebc33b3487d83af0292a6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6428286dbf0ff063bb4642a75d318ace6353b3d434cc89dfd1a71658bd136d0c
MD5 2f46d94a3addc1e7feb0c9e7c670e0a5
BLAKE2b-256 38ee74174c091257f0c5ca726865e18ed74e67534d51f7e11167501fc89d588b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 e968953c10b185bf75b6d6cd6403ab4ebad31509ea16557b316416c33986060c
MD5 c738773da638daf81faf6d8159e56a4b
BLAKE2b-256 eb8bfb96454954ea57834a290db59990d8fc13e9f849aa98052641c8ecc9fbf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.5.1-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 4626843444a7011916c5ed92a0143c550b939af429da2112bb5b6e5e8255e45a
MD5 c70dbf1bbe371ccd0c97af18f2853ba4
BLAKE2b-256 1bd8a1d49aff1d796a271cf37b9facead3edda9893918185b9282daf836c47b1

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