Advertisement
gh0s7

Async Shutdown

Mar 30th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import asyncio, asyncssh
  3.  
  4. async def run_client(host, command):
  5. #    async with asyncssh.connect(host, username='username', password='password') as conn:
  6.     async with asyncssh.connect(host, username='username') as conn:
  7.         return await conn.run(command)
  8.  
  9. async def run_multiple_clients():
  10.     # Put your lists of hosts here
  11.     hosts = ['192.168.1.2','','']
  12.  
  13.     tasks = (run_client(host, "echo 'password' | sudo -S init 0") for host in hosts)
  14.     results = await asyncio.gather(*tasks, return_exceptions=True)
  15.     #print(results)
  16.     for i, result in enumerate(results, 1):
  17.         #you might have to change the if else stuff based on the command you choose to execute
  18.         if isinstance(result, Exception):
  19.             print('Task %d failed: %s' % (i, str(result)))
  20.         elif not result.stdout:
  21.             print('Task %d : Connection closed by remote host.' % (i))
  22.  
  23. #        print(75*'-')
  24.  
  25. asyncio.get_event_loop().run_until_complete(run_multiple_clients())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement