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

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

Uploaded Source

Built Distributions

parallel_ssh-1.4.0-cp36-cp36m-win_amd64.whl (105.6 kB view details)

Uploaded CPython 3.6m Windows x86-64

parallel_ssh-1.4.0-cp36-cp36m-win32.whl (96.3 kB view details)

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.6m

parallel_ssh-1.4.0-cp35-cp35m-win_amd64.whl (105.2 kB view details)

Uploaded CPython 3.5m Windows x86-64

parallel_ssh-1.4.0-cp35-cp35m-win32.whl (96.0 kB view details)

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.5m

parallel_ssh-1.4.0-cp34-cp34m-win_amd64.whl (103.3 kB view details)

Uploaded CPython 3.4m Windows x86-64

parallel_ssh-1.4.0-cp34-cp34m-win32.whl (96.9 kB view details)

Uploaded CPython 3.4m Windows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 3.3m

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

Uploaded CPython 2.7mu

parallel_ssh-1.4.0-cp27-cp27m-win_amd64.whl (104.5 kB view details)

Uploaded CPython 2.7m Windows x86-64

parallel_ssh-1.4.0-cp27-cp27m-win32.whl (96.6 kB view details)

Uploaded CPython 2.7m Windows x86

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

Uploaded CPython 2.7m

parallel_ssh-1.4.0-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.4.0-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.4.0-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.4.0.tar.gz.

File metadata

File hashes

Hashes for parallel-ssh-1.4.0.tar.gz
Algorithm Hash digest
SHA256 2eb666c2bf8b87185b4e7e4e32e010fc2a8ec986e928d37a7ff03f36140dfdf0
MD5 6fa72bcf4c03a0ddca90f25b7a1e4355
BLAKE2b-256 80b6728253828d2d5910d16f4cf69014bb02e6b8b5e2889a5400201b76df44ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 4dec6e95b7764c1dc21951a3116b4ef55c60961209c0142042e95cf6e964f782
MD5 cfbd4038b2d7b3f4efbf731a792a9534
BLAKE2b-256 3e209a932f134b8940d7535c6caf3a7251cfe288d9f0698860ea4740b7b1ef2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 775aea7d6fcdd0c73774d38c336a6de3e9204d5c3b5214a3228b6b262d5249d1
MD5 f651e10a99f2038f69cb49e5f688818a
BLAKE2b-256 8f0288dd2c7c2e72c6f33f3e1006b419b2bf90936e9f255a6d393304d6f81781

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7a173e77a002989685687443804b81576fdb9678c9663b07c9512a46d3193ae7
MD5 25c8f04103ddf1875c5127559550ce43
BLAKE2b-256 03791e90df8c295646d1883fa1a2efd9573680167c286ebfb3613ef0d15c28e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 e24fcb8dea3b2cb4fb07da53eea87c4ad551c8e6be1aff724dae1c85d640988b
MD5 09e0d6cf7783796eafcdbb3613aa39c7
BLAKE2b-256 200b76f0da0406c6a13adbd92e3e90a44c960d18f1464bdb3f0405e0e9c24adf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 6078a0b3800c93dab5932725b0be4175f21bc9ba704dbb10445d142dc0a3db82
MD5 b861558818e52f06d1bbc741903d5f09
BLAKE2b-256 dd972748fcfa572b046426f72036267e49c7f7935210d67590e957633f633b2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 eb7055707602f40e921d657d5f3e6f22a4531b397013eaa7c339aae4bc5f68ad
MD5 108c65065f596469962f7428cb5e3cf6
BLAKE2b-256 779cd844cb640156ef3d5063b31d5d84e18d9fbf2c59f858d2e9852e7adbc0cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 a856bc6e7c955a5faf3d6d20d342b6e4620ce73a8fb1499c831285dda30c0ef6
MD5 c9e5afa2f2f038f01263dea53b979cc3
BLAKE2b-256 571149f55f17d39d97f0a9c1deb6134e9c151c4d7d521f0c33cfd58cd846dae9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 0e529d63f4a3b97c18638c6a9a93118cab45fbfb72227a7f847ecbdac1a065e2
MD5 ddafae7535fb854dd42373cb127e2bc2
BLAKE2b-256 13dc934bbde02a6aeee39649935d9398e25373d42bcea2738bd378c701937266

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 32cf8f94d6aa2147d6ad37979e04ac2cbb400bd5bdc718fe529a5dc46708cac2
MD5 26cd1a342c73210cc0114bb56501d625
BLAKE2b-256 c609c80f1639d8a5f4af4f06a7dc704e4c35fcf2ac9daa1b1891ad5bed8fcb12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8e337890a4702f2b9015228a7b2cedfa57d502b2a6d405d3ae468c9ce1264e72
MD5 89221ec16a6776d65a86d6c9a9795132
BLAKE2b-256 e093ff8fb590ef4acdf730222b024338ac4cb5cc1aa226d74effff411cca0da7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 13e7f3f170d34797dff1bbd06d61b5a2c73d1d8f9b3235cbc368e5b0b314b91f
MD5 fd0ef36d442bc73d2683cb98211c09cf
BLAKE2b-256 5b382d8a78b920141eeb5f27b11e9378948c76a72c88e0c2abf7017477a6657d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 7cb88eed6097950a696ee9e1715045ac55a3a0c3a51e2fc7bef701949601f9db
MD5 b57b12d857333c8c37b4b1bca0a13785
BLAKE2b-256 b0ac8fbf432ce38eec9e53552088a41069d1a3e86e5431f28dd8ca71f3f33713

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 fea89e323bb52d1429a7cac7bdc32ce2c250ba4d1ce79ea879571de2c6ab086d
MD5 4fe607e60fcd323bae198df5a5033c85
BLAKE2b-256 9866483626ddb1ba917e311507c25511581913cba24a2fadcdea32645e8fb5c9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5eb1ce7da2567902e560cdff32fce9e2d46c7823f2124c664714001c7a6a97ca
MD5 9ce5485ae3c6f256df1635725099744b
BLAKE2b-256 eb3df8f702eb045b51debd08fae49e248daab908612d85545697208ebd8d33bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 40b1e68a46ba7ba3dc0a257ad0a5ec382c46cee7d7bfe0ba9b14f22d79ea1eb3
MD5 40a877c065e270afaf089b98f40187ff
BLAKE2b-256 a0d9fccab6323ca2d31b360607bcf33c6d4d339197682ac532961ec41bf4f5e2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 fd53c180cf021bead47bed3c8761e46b6c883ee3b22a090b2d6b9455ec8da114
MD5 0f954fc50a6750a8755aa1c00f5ee759
BLAKE2b-256 c947c87fdd22cdeed40e3b17fc03d002f5e0b9c573737492b5fa60a4b4462eb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.4.0-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 9da86079205b03f1387abd5e48e8df81acadf6156e19150a2e62a297d4757e35
MD5 abd8a72b39f46ff08cc4be2c0c3345b7
BLAKE2b-256 9cf280b7619c3ff7e0204cb907db3277cc72cfae5729baf06b9709a35709736b

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