Skip to main content

Executing commands using SSH concurrently on multiple hosts

Project description

concurrent-executor

PyPI

Executing commands with ssh concurrently on multiple hosts using asyncio

Installation

pip install concurrent-executor
# Or from the latest GitHub version
pip install git+https://github.com/DCsunset/concurrent-executor

CLI Usage

All the following tools can handle signals as follows:

  • Upon receiving one SIGINT (including keyboard interrupt) or SIGTERM, the program will send SIGTERM to all spawned processes.
  • Upon receiving more than one of them, the program will instead send SIGKILL to kill all spawned processes.

cssh

cssh is a command line tool provided by this package. It is used to executing commands concurrently on remote servers vis SSH.

Use -H or --hosts to specify the hosts to run the commands on:

cssh -H host1 host2 ... host_n -- command
# pass extra ssh options
cssh -o="-q -4" -H host1 host2 ... host_n -- command
# read hosts from file
cssh -f hosts.txt -- command
# string interpolation (to include host name in command by {0})
cssh -H host1 host2 -- command --host {0}

Note that -- is necessary to separate the options and the command. For -o/--options to work correctly, use = to prevent it from being parsed as another option.

The standard input (stdin) of the cssh process is piped to the stdin of every spawned processes.

For more details, see cssh -h.

cexec

cexec is another command line tool provided by this package. It is used to execute arbitrary shell commands concurrently using template (string interpolation in Python).

The command itself can container placeholder in strings: (See Python string interpolation for more detail.)

# The variables are a, b, c in the template command
# This command creates 3 directories and write to a file in each directory
cexec -V a b c -- "mkdir {0} && echo 1 > {0}/out"
# Read variables from a file
cexec -f vars.txt -- "mkdir {0} && echo 1 > {0}/out"
# Run different commands directly
cexec -V "cmd1" "cmd2" "cmd3" -- "{}"

For more details, see cexec -h.

Library

It can also be used as a library:

import asyncio
from concurrent_executor.executor import SshExecutor

async def main():
  hosts = ["host1", "host2"]
  executor = SshExecutor(hosts)
  # running concurrently
  await executor.run("some_command --test")

  # access stdout for all hosts (or stderr)
  async for index, out in executor.stdout:
    print(f"{host[index]}: {out}")

  # wait until all finished
  ret_codes = await executor.wait()
  
asyncio.run(main())

See more usage in concurrent_executor/cli.py.

Development

To set up the development environment, first clone this repo.

Then it's recommended to usevenv:

# suppose PWD is the root dir of the repo
python -m venv venv
# activate the environment``
source venv/bin/activate
pip install -r requirements.txt

To deactivate, run deactivate.

License

This project is licensed under AGPL-3.0. Copyright notice:

concurrent-executor
Copyright (C) 2023 DCsunset

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

concurrent-executor-1.0.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

concurrent_executor-1.0.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file concurrent-executor-1.0.0.tar.gz.

File metadata

  • Download URL: concurrent-executor-1.0.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for concurrent-executor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 98cc11977a1804a1d1b277415fb573af5f4adf5540db7032a5a5e5526a02a98f
MD5 15b7dfa0bd56af316ab8c9d916f3db6e
BLAKE2b-256 4e6f6f866074aa0735195213c2d5e61832b6fecb04a7b213391f30a290fd9147

See more details on using hashes here.

File details

Details for the file concurrent_executor-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for concurrent_executor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82b42439d57dd4ab7e34d964c77326feac5bd48ad4a90fdf3921b65c5f210844
MD5 43903ccf6287814adb4462688ffaf838
BLAKE2b-256 5985865ab0fc5b49cbffb7c249a79fd4147b52f31f14e28f846245c92489fa2f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page