parallel-ssh (pssh)

2.16.0.post1 · active · verified Fri Apr 17

parallel-ssh, imported as `pssh`, is an asynchronous Python library for executing commands and transferring files over SSH to multiple hosts in parallel. It leverages `asyncio` for high-performance operations, making it suitable for managing large clusters or performing fast, concurrent remote tasks. The current version is 2.16.0.post1, and it maintains a relatively active release cadence with minor updates every few months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates connecting to multiple hosts using `ParallelSSHClient` and executing a command. It uses `asyncio` to run the asynchronous operations. Replace `hostname1`, `hostname2`, and `your_user` with your actual host details and username. For authentication, consider using SSH agents, `known_hosts`, or passing credentials/private key paths to the client.

import asyncio
from pssh.clients import ParallelSSHClient
import os

async def run_remote_commands():
    # Replace with your actual hostnames or IP addresses
    hosts = ['hostname1', 'hostname2'] 
    
    # You can specify username, password, or private key path
    # client = ParallelSSHClient(hosts, user='myuser', password=os.environ.get('SSH_PASSWORD', ''))
    # client = ParallelSSHClient(hosts, user='myuser', pkey='/path/to/id_rsa')
    client = ParallelSSHClient(hosts, user=os.environ.get('SSH_USER', 'your_user'))

    print(f"Connecting to {hosts} and running 'ls -l /tmp'...")
    output = await client.run_command('ls -l /tmp')

    async for host_output in output:
        print(f"\n--- Host: {host_output.host} ---")
        print(f"Return Code: {host_output.return_code}")
        print("Stdout:")
        for line in host_output.stdout:
            print(f"  {line}")
        print("Stderr:")
        for line in host_output.stderr:
            print(f"  {line}")

if __name__ == '__main__':
    # Ensure your SSH_USER env var is set or replace 'your_user' directly
    # Also, ensure SSH agents or known_hosts are configured for passwordless access
    # or provide password/pkey arguments to ParallelSSHClient.
    asyncio.run(run_remote_commands())

view raw JSON →