Skip to main content

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 clients with extremely high performance, making use of C libraries.

License Latest Version https://circleci.com/gh/ParallelSSH/parallel-ssh/tree/master.svg?style=svg 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

An update to pip may be needed to be able to install binary wheels.

pip install -U pip
pip install parallel-ssh

Usage Example

See documentation on read the docs for more complete examples.

Run uname on two hosts in parallel.

from pssh.clients import ParallelSSHClient

hosts = ['localhost', 'localhost']
client = ParallelSSHClient(hosts)

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

Single Host Client

Single host client with similar API can be used if parallel functionality is not needed.

from pssh.clients import SSHClient

host = 'localhost'
cmd = 'uname'
client = SSHClient(host)

host_out = client.run_command(cmd)
for line in host_out.stdout:
    print(line)
exit_code = host_out.exit_code

Questions And Discussion

Github discussions can be used to discuss, ask questions and share ideas regarding the use of parallel-ssh.

Native clients

The default client in parallel-ssh is a native client based on ssh2-python - libssh2 C library - which offers much greater performance and reduced overhead compared to other Python SSH libraries.

See this post for a performance comparison of different Python SSH libraries.

Alternative clients based on ssh-python (libssh) are also available under pssh.clients.ssh. See client documentation for a feature comparison of the available clients in the library.

parallel-ssh makes use of clients and an event loop solely based on C libraries providing native code levels of performance and stability with an easy to use Python API.

Native Code Client Features

  • Highest performance and least overhead of any Python SSH library

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

  • Natively asynchronous utilising C libraries implementing the SSH protocol

  • Significantly reduced overhead in CPU and memory usage

Why This Library

Because other options are either immature, unstable, lacking in performance or all of the aforementioned.

Certain other self-proclaimed leading Python SSH libraries leave a lot to be desired from a performance and stability point of view, as well as suffering from a lack of maintenance with hundreds of open issues, unresolved pull requests and inherent design flaws.

The SSH libraries parallel-ssh uses are, on the other hand, long standing mature C libraries in libssh2 and libssh that have been in production use for decades and are part of some of the most widely distributed software available today - Git itself, OpenSSH, Curl and many others.

These low level libraries are far better placed to provide the maturity, stability and performance needed from an SSH client for production use.

parallel-ssh provides easy to use SSH clients that hide the complexity, while offering stability and native code levels of performance and as well as the ability to scale to hundreds or more concurrent hosts.

See alternatives for a more complete comparison of alternative SSH libraries, as well as performance comparisons mentioned previously.

Waiting for Completion and Exit Codes

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

After join returns, commands have finished and all output can be read without blocking.

Once either standard output is iterated on to completion, or client.join() 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 - see join and output timeouts documentation.

HostOutput.exit_code is a dynamic property and will return None when exit code is not ready, meaning command has not finished, or unavailable due to error.

Once all output has been gathered exit codes become available even without calling join as per previous examples.

output = client.run_command('uname')

client.join()

for host_out in output:
    for line in host_out.stdout:
        print(line)
    print(host_out.exit_code)
Output:
Linux
0
Linux
0

Similarly, exit codes are available after client.join() without reading output.

output = client.run_command('uname')

client.join()

for host_output in output:
    print(host_out.exit_code)
Output:
0
0

Built in Host Output Logger

There is also a built in host logger that can be enabled to log output from remote hosts for both stdout and stderr. 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.run_command('uname')
client.join(consume_output=True)
Output:
[localhost]       Linux

SCP

SCP is supported - native client only - and provides the best performance for file copying.

Unlike with the SFTP functionality, remote files that already exist are not overwritten and an exception is raised instead.

Note that enabling recursion with SCP requires server SFTP support for creating remote directories.

To copy a local file to remote hosts in parallel with SCP:

from pssh.clients import ParallelSSHClient
from gevent import joinall

hosts = ['myhost1', 'myhost2']
client = ParallelSSHClient(hosts)
cmds = client.scp_send('../test', 'test_dir/test')
joinall(cmds, raise_error=True)

See SFTP and SCP documentation for more examples.

SFTP

SFTP is supported in the native client.

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 with configurable file names via the copy_remote_file function.

In addition, per-host configurable file name functionality is provided for both SFTP and SCP - see documentation.

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

See SFTP and SCP documentation for more examples.

Release files for parallel-ssh 2.16.0.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parallel-ssh 2.16.0.post1
File Size Uploaded
parallel_ssh-2.16.0.post1.tar.gz 72.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parallel-ssh 2.16.0.post1
File Interpreter ABI Platform
parallel_ssh-2.16.0.post1-py3-none-any.whl Python 3 none any Details

Total release size: 160.9 kB

Release files / parallel_ssh-2.16.0.post1.tar.gz

Download URL parallel_ssh-2.16.0.post1.tar.gz
Size 72.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c355eb965c8698e7ec8e7fd39fbe1b0d6d35debc2de603ebb1b397a9bc30d61c
BLAKE2b-256 checksum
How to use checksums
5fecc6e4d905364910de3e044c5eb80b9c1655ba7da9b7b81bc02b8a6d2285a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release files / parallel_ssh-2.16.0.post1-py3-none-any.whl

Download URL parallel_ssh-2.16.0.post1-py3-none-any.whl
Size 88.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
caf185609bef24f20ccac75adf4babf8baee83996bfc50d9d617e23d9cb75475
BLAKE2b-256 checksum
How to use checksums
3423001f528cd9041fcab9866f67b8ed54c3b5288b069f3cf436726453f4a5f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.19

Release history Release notifications | RSS feed

This release

2.16.0.post1 This release

2 release files

2.16.0

2 release files

2.15.0

2 release files

2.13.0

2 release files

2.12.0

2 release files

2.11.1

2 release files

2.9.1

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.1

2 release files

2.7.0

2 release files

2.5.4

2 release files

2.5.3

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.0.0

2 release files

1.9.0

28 release files

1.8.0

18 release files

1.7.0

18 release files

1.6.3

18 release files

1.6.2

18 release files

1.6.1

18 release files

1.6.0

18 release files

1.5.4

18 release files

1.4.0

18 release files

1.3.2

18 release files

1.3.1

18 release files

1.3.0

18 release files

1.2.0

18 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.94.0

3 release files

0.93.0

1 release file

0.92.2

3 release files

0.92.1

3 release files

0.91.1

1 release file

0.91.0

1 release file

0.90.0

1 release file

0.80.7

1 release file

0.80.6

1 release file

0.80.5

1 release file

0.80.3

2 release files

0.80.2

2 release files

0.70.4

1 release file

0.70.3

1 release file

0.70.2

1 release file

0.70.1

1 release file

0.70

1 release file

0.61

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page