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

ParallelSSH’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.

Scaling

Some guide lines on scaling ParallelSSH client 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 process 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 processes, each with its own event loop, may be used to scale this use case further as CPU overhead allows.

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 overhead 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.2.1.post3.tar.gz (88.0 kB view details)

Uploaded Source

Built Distributions

parallel_ssh-1.2.1.post3-cp36-cp36m-win_amd64.whl (104.4 kB view details)

Uploaded CPython 3.6m Windows x86-64

parallel_ssh-1.2.1.post3-cp36-cp36m-win32.whl (95.0 kB view details)

Uploaded CPython 3.6m Windows x86

parallel_ssh-1.2.1.post3-cp35-cp35m-win_amd64.whl (104.0 kB view details)

Uploaded CPython 3.5m Windows x86-64

parallel_ssh-1.2.1.post3-cp35-cp35m-win32.whl (94.7 kB view details)

Uploaded CPython 3.5m Windows x86

parallel_ssh-1.2.1.post3-cp34-cp34m-win_amd64.whl (101.9 kB view details)

Uploaded CPython 3.4m Windows x86-64

parallel_ssh-1.2.1.post3-cp34-cp34m-win32.whl (95.5 kB view details)

Uploaded CPython 3.4m Windows x86

parallel_ssh-1.2.1.post3-cp27-cp27m-win_amd64.whl (103.1 kB view details)

Uploaded CPython 2.7m Windows x86-64

parallel_ssh-1.2.1.post3-cp27-cp27m-win32.whl (95.2 kB view details)

Uploaded CPython 2.7m Windows x86

parallel_ssh-1.2.1.post3-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.2.1.post3-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.2.1.post3-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.2.1.post3.tar.gz.

File metadata

File hashes

Hashes for parallel-ssh-1.2.1.post3.tar.gz
Algorithm Hash digest
SHA256 cfa83bf2f5cc16cb7591581251a3d5532d70bb6838900c93e513321b9166ec5c
MD5 5536ae81dc75f68578c22de9e59b3f3b
BLAKE2b-256 4f27ee34859e56f2c1e4ab0586bb6d0c97586558b7541429c2bf4fba130483b4

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 474aa2c66db9ca8388e8cb63ed04771aa6bfc90ec239bfac0ea64e56e80db7cc
MD5 f4281f6004540f3f6bff9be37962267d
BLAKE2b-256 3157074c7bd760be9a95c935a2247d79aa12f84cb24fc93a9a7a6dfee02f4506

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 85d0c796e6f8851591b6dac6d59fe65bc4efca19b4b890b33b4fd1d19e268624
MD5 4cf79548c45f5ecabed0abb865e5369f
BLAKE2b-256 80d59206e3929df187203997287bd6c306f197e5c2a9e6cfa4f400b47caa0aad

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 781c86dd964e83699f0d829d05d09c7acf097e4f9f35d98d6ec561a626d907e6
MD5 9a206430bdcf855a15a1bd19ec2d9c6f
BLAKE2b-256 2167bf26b41bffb903c68b7e2cd4c41e83d8c38ae98fd22920430d3abfa57c6a

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 68bc4aebea48e6d3c89d672cfd031ac29a2dd1ea467bfa435f7eb74f53d0e0ce
MD5 42a964f387e1ddc239d7eb874a1306b9
BLAKE2b-256 c6ccf99ddd1cdaedc7a634df5775b82744609037602a563fe456edd26979774c

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 90d4557a5352869edf65e3ecd31180c1ba535581da3b37e51b239158cc0cdd9e
MD5 24067e5c31414187592154e6a245833e
BLAKE2b-256 993b9a149a324c44a4a6ca4f663f064f0edc148da4e6f0ea550b94c670989a40

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0fc9ea4eb0feb7651de873ee55cd940756d41796f34b2307e7603247ab4ee378
MD5 6fcc691993ebc870b7c59163a4be6189
BLAKE2b-256 0a4bb0d53964ba02919c5b77360de1ab77dc3a7297943b03ef7de4312fdcea1c

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 64f9c774fcff8b1901cd89be394c0264df0040220ae497ca084eed5db3565816
MD5 a35e044af1b056ac3a3831ffbcd49834
BLAKE2b-256 fb2d0296e3604bfe97eab4ef80d3309989b25664cdc2f96cf4409d71f39b79de

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 6d5082210103af2ca33a9e08beab43f555d005a4521f6bc1bfe6f6c45d68299e
MD5 df2e44e8e8228d39e6a7e7020a4d6c06
BLAKE2b-256 b0a271d73de2afe8c841777e482880d9909c049a0a2f91d7e081836579d5dc2f

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 500cd9f0b170fdf4f845d7b4ff70b06debf90d13ba972fc441316f893e99874a
MD5 70db9816851a97f6fa02a1b7ef1a1984
BLAKE2b-256 47e36bf06170cef4093328a832f706c94913231d3307fe159b0cc90dee7aaab9

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9036546a090ef8ee16af0bcf1412e83854c561e949f6e1f825a1c2596201d722
MD5 5d056e358920eb4f11573cc46b312703
BLAKE2b-256 65c4f48ef66678f214b5a80564b1bd4d417909561747e86f0c114b72dca8f9da

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f6f76c5743109bddc4c15d13896273da2d50ab3906e049b21b4434ef8792a6b5
MD5 714539eb4e1ba31ae116e277a8f779fe
BLAKE2b-256 b371ce24e7e50ed6508efdfab3622aef0db3e290e61611e93bacc752586f3248

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 a6b1da53f26ec1d6b358a294621404ceb1fe04d73752a6e43cfa10ddd03c3f3a
MD5 d7d3ff3757fc35d83e23514b8b307963
BLAKE2b-256 9da7500d842f2b23e78764660369f180d675b80082e481d81a5937f299e1de2a

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 d78cad6476edb756c82c70c6a2a640d3316f70dad9e254614f7089ba462d7eed
MD5 08a6a3f8a54c39bce4013b4a162cd8a1
BLAKE2b-256 649475d44b0243ceec375493af82568c24b685b6e1e021286925b3facf6f8cbd

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 67ac6bd13c6e9cec0a2c0cc8fa4a8227502a0ec590484ba567212e2229d83945
MD5 962456cad42c83e607a9ddfbdb408a7e
BLAKE2b-256 458cb0e0dca00addd5441a6673798a7b7130ceabc69cdfb2775ef0424d438f9e

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1fa903a90be7d5789ea7875fb4509c8830800d9d7d57a503f785312d25ba87fa
MD5 695aa076a76c51882b9e1140bc48f051
BLAKE2b-256 2a07fc764bbb9c189ebaea0d793f8b9d010a6f48910895b3035283e43c5966a4

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 df096b5db7f8a3c0086bd5e1ed18eb508e875268ec27eb929a499d980519df14
MD5 5ed2e9024b2ed57fb02e015ac812b97e
BLAKE2b-256 85f01ff9e21bb58ee9e8c4a4985bb39955baa65ff44da637b74c0c74c44eb836

See more details on using hashes here.

File details

Details for the file parallel_ssh-1.2.1.post3-cp27-cp27m-macosx_10_10_intel.whl.

File metadata

File hashes

Hashes for parallel_ssh-1.2.1.post3-cp27-cp27m-macosx_10_10_intel.whl
Algorithm Hash digest
SHA256 1b7c75ded2765603762c669937b2379d484fd7794b78a3de2f66f238361afe54
MD5 511cb00d01471f619029408ce7b77296
BLAKE2b-256 51c736a7dea12329a7937d2420707e966c1bb7c614928a47331b34f910a82d37

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