Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import asyncio, asyncssh
  2.  
  3. async def run_client(host, command):
  4.    async with asyncssh.connect(host) as conn:
  5.        return await conn.run(command)
  6.  
  7. async def run_multiple_clients():
  8.    # Put your lists of hosts here
  9.    hosts = 5 * ['localhost']
  10.  
  11.    tasks = (run_client(host, 'ls abc') for host in hosts)
  12.    results = await asyncio.gather(*tasks, return_exceptions=True)
  13.  
  14.    for i, result in enumerate(results, 1):
  15.        if isinstance(result, Exception):
  16.            print('Task %d failed: %s' % (i, str(result)))
  17.        elif result.exit_status != 0:
  18.            print('Task %d exited with status %s:' % (i, result.exit_status))
  19.            print(result.stderr, end='')
  20.        else:
  21.            print('Task %d succeeded:' % i)
  22.            print(result.stdout, end='')
  23.  
  24.        print(75*'-')
  25.  
  26. asyncio.get_event_loop().run_until_complete(run_multiple_clients())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement