Linux: Execute commands in parallel with parallel-ssh

parallel-ssh is an asynchronous parallel SSH library designed for large scale automation.

Linux: Execute commands in parallel with parallel-ssh
Photo by Gabriel Heinzer on Unsplash

parallel-ssh is an asynchronous parallel SSH library designed for large scale automation.

It differentiates ifself from alternatives, other libraries and higher level frameworks like Ansible or Chef in several ways:

  • Scalability — Scales to hundreds, thousands, tens of thousands hosts or more.
  • Ease of use — Running commands over any number of hosts can be achieved in as little as two lines of code.
  • Speed — Fastest Python SSH library available — see performance comparison.
  • Resource use — Uses the least resources, both CPU and memory, of any other Python SSH library.
  • And more.

You can install parallel-ssh using pip

pip install parallel-ssh

Parameters

  • p: Use the given number as the maximum number of concurrent connections.
  • -h: Read hosts from the given host_file.
  • -i: Display stdout and stderr from each host.
  • -l: User to connect
  • -I: Read input and send to each ssh process.

Example: Execute a command in servers defined in hosts_file

parallel-ssh -p10 -h <hosts_file> -i -l <user> """cmd"""

Example: Execute local script remotely

parallel-ssh -p10 -i -h <hosts_file> -l <user> -I<./<script_file>

parallel-ssh is not so powerful or as feature full like Ansible, but in can be really useful for day to day operations with a minimal learning curve!