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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.6m Windows x86-64

parallel_ssh-1.8.0-cp36-cp36m-win32.whl (959.0 kB view details)

Uploaded CPython 3.6m Windows x86

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.5m Windows x86-64

parallel_ssh-1.8.0-cp35-cp35m-win32.whl (958.6 kB view details)

Uploaded CPython 3.5m Windows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.4m Windows x86-64

parallel_ssh-1.8.0-cp34-cp34m-win32.whl (960.1 kB view details)

Uploaded CPython 3.4m Windows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7m Windows x86-64

parallel_ssh-1.8.0-cp27-cp27m-win32.whl (960.5 kB view details)

Uploaded CPython 2.7m Windows x86

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

Uploaded CPython 2.7m

parallel_ssh-1.8.0-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.8.0-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.8.0-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.8.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.8.0.tar.gz.

File metadata

File hashes

Hashes for parallel-ssh-1.8.0.tar.gz
Algorithm Hash digest
SHA256 d808c05c2b47d679a1e38e0341a4afb133250f42379900d617dae39c802af9b7
MD5 8b19e6398923279fdf63fdbe0fe43c90
BLAKE2b-256 59338d9e062bbb8b49f09ad94665792b4fed3fe628241d7c8c0430c6977fc441

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 23494428efa72cf15ae282d3d9ea3375279f53468df0fdd12e462b46eac28862
MD5 24ab59b035c22dcacc50dcb7574818b6
BLAKE2b-256 27a82b0150a9ca1be58b76f884d1e304baf06bb2a63af008d71b966e68bb3402

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 da25125cf195244657f6a7472bb22244fa9dd1a948570a45d1c123b8479d8953
MD5 533f1a4ce8885fb0882c17df347c3273
BLAKE2b-256 965bb3c82656f30f590d7106e9e573afcde984c1064549944414514de537a56f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d7fca2e47aef7517567ef8205802e8330898f4f4d689fc4d80d1e2a700f5a9f2
MD5 9189d24aebba7f1a117b99a0d6acfb15
BLAKE2b-256 d97ed9b27d477a9a5b9f7eaf42c77f153754c2ec6946514e572952a7847879c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 343454bffd7236e142305b3e00d5a0d223c6ff715368510248daf001e2a72038
MD5 304593173e83828aa1a91920f8c318ce
BLAKE2b-256 5b511e2898f6bfc1f584e14b38f7f9d027183723b091dadf2f0770d0596d0146

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 a95e1dc82199e387a572a3a61611f4b90e989c24a36ddc10bdaf91664b3e17f7
MD5 38eea8643b4cb0150a7f08c5b0cd0ad9
BLAKE2b-256 0264decdd0116ced88172feafe8a4ed868ed4d3de35197354527e058fbfc064c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a9811cd718d34e0f0f673027c73de5275a5c133baaa5d1375b47fbc745be6321
MD5 3a9079703c825b55147d5d5854d89dc6
BLAKE2b-256 306634328ca1f608fa2193071c8e19b854d3aaf31558a64df1fb013c700c7b08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 48877d839ca2089f1106271083bcfba24e00acb31d023b5b987b7e3c0d506234
MD5 ba7e6ed039fac77e9e58452c025608f1
BLAKE2b-256 e8c964a6daf97b8a3809e20bc004d331fef767b9ccc7b9be1f85ff5fe2a3ca2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 28291e074182092fdb90c7f0e9b4d5f84d856e341586e75d10a64a11f8897bbe
MD5 b34ad8b8e1e4a2bd89061c9b80ddd672
BLAKE2b-256 68bd65e20e6c76f357d6d5892085b761bee1aaad433e01540f5848fca35f405d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 40c40b1378e9fa98b332d31a40f197dc5dd2f2ffe6aa6f726fab1ce827cb0e59
MD5 e7deeb9e5698d727367fc78fcba2cebf
BLAKE2b-256 dedf3384d8e7959ecc88bd9a4c10f3f99f5415e8f7e8a002bd4e541a91012ff5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 56635ce85a59b0ca205bfbeb268ac891a3b29e22723897e21adfe32b5b606af4
MD5 c360c921ea164963c077c9ab8424164d
BLAKE2b-256 85532c3ce9225d29897d73aa22ac66ec147344db37cdbdc5590e0df9edea3c3e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 e843849dd21251347593e3ed33403bc58cb99b247c7e7878f8105f36330a4210
MD5 b7508f3c834f3546d244847c39e8c10a
BLAKE2b-256 bf95760815b81a1658e5c9bb236f06b115b68995b49c63c4d2c03695f4de1cd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 be1c94915ec485fbb5dc2695622ca42415a29de4a136849b44243104ee39230a
MD5 4ea108453313681c0d7c1b872f630ca5
BLAKE2b-256 87709a536879c1ce139f76a5bc931f7ce2913b2ea3f89f6dac58c52afc18df02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 12e8518dd7b6e3edaac98e14eb335762759446bf7f7ea368b101f182ca484318
MD5 a45215c5cf37b7b888db9e57d663c9e1
BLAKE2b-256 5ba1cf4bf2eaeb1bf67bc1c6d17eda98bdece8f286de79a835eff8043fb057ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 16bb1d0b441f1f4964bddc44e1ce63c1ae846c480b9d573910dc4bf12aac3e1d
MD5 d1fdc6c1f10a3f7b8238d32fbbe9f89b
BLAKE2b-256 cbdc7bbefe7eac890505fda978fdc373a4fc4baea9227bb2da49f4807b2f0760

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e4ea7bee5f27e2ee5592b181ed4c5697cb86a6c76ea15a764928f3bef06c3d35
MD5 1e81750b4994052b992bd347b246f963
BLAKE2b-256 39bfcafb2252411f1f76bef3806c86066c7a9c13e444b9f8614a93dd3c039d90

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 bf760f02c0c4835e5586f2e69cc4aac088932d1eb8cc6a710f13a13e6b3f35b1
MD5 6d65a59da090810c4633fec18e295233
BLAKE2b-256 ea507f82b8c0d5ab7cf6f65cab5f41907ff4afeb1053cfba60f97cf78c096683

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parallel_ssh-1.8.0-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 c1390b7fa1c7de3d38eabb408868c02a02d1a28c8213a733d3e9434934c51222
MD5 04c3ac503c8eeabcd725c21f54148db7
BLAKE2b-256 10e67e9ecd906585e2d3a31e945daba576ecb142106b7ff11d66cb59fec532d7

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