passh
passh (Parallel Asynchronous SSH) is a Python3 library to run a number of SSH processes in parallel.
As passh uses asyncio, it requires Python 3.4 or newer.
Features
PAssh
class to run SSH in parallel.- SSH outputs are forwarded to local stdout/stderr.
Every line is to be prefixed by the remote hostname. - Instead of forwarding, SSH outputs can be collected in memory for later use.
- SSH outputs are forwarded to local stdout/stderr.
- Non-asyncio apps can use passh as well as asyncio apps.
- A file can be given as inputs for all SSH processes.
- Limit on the number of simultaneous SSH processes.
- Built-in command-line interface.
Install
passh is available via PyPI.
pip3 install passh
Command-line
passh [-n PROCS] [-i FILE] host1,host2,... COMMAND [arg1 arg2 ...]
Invoke "date" at once.
$ passh host-1,host-2,host-3 date
[host-3] Sat May 23 07:20:22 UTC 2015
[host-1] Sat May 23 07:20:22 UTC 2015
[host-2] Sat May 23 07:20:22 UTC 2015
Send a file at once.
$ passh -i $HOME/.bashrc host-1,host-2,host-3 dd of=$HOME/.bashrc
[host-2] 7+1 records in
[host-2] 7+1 records out
[host-2] 3650 bytes (3.6 kB) copied, 3.0315e-05 s, 120 MB/s
[host-3] 7+1 records in
[host-3] 7+1 records out
[host-3] 3650 bytes (3.6 kB) copied, 6.8925e-05 s, 53.0 MB/s
[host-1] 7+1 records in
[host-1] 7+1 records out
[host-1] 3650 bytes (3.6 kB) copied, 3.0475e-05 s, 120 MB/s
Python module
Use PAssh.run() for non-asyncio application.
>>> import passh
>>> p = passh.PAssh(['host-1', 'host-2', 'host-3'], ['date'], use_stdout=True)
>>> p.run()
True
>>> from pprint import pprint
>>> pprint(p.outputs)
{'host-1': b'Sat May 23 07:30:07 UTC 2015\n',
'host-2': b'Sat May 23 07:30:07 UTC 2015\n',
'host-3': b'Sat May 23 07:30:07 UTC 2015\n'}
Embed PAssh into your asyncio application.
Use yield from
to asynchronously wait the result:
import passh
p = passh.PAssh(['host-1', 'host-2'], ['date'])
done, _ = yield from p.wait()
for task in done:
task.result() # check results.
if len(p.failed_hosts) > 0:
# handle failures
Use asyncio.async
to schedule passh as an independent coroutine:
import asyncio, passh
p = passh.PAssh(['host-1', 'host-2'], ['date'])
task = asyncio.async(p.wait())
task.add_done_callback(lambda x: ...) # handle PAssh results.
License
Author
Yamamoto, Hirotaka (@ymmt2005)