Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import asyncio, asyncssh
- async def run_client(host, command):
- # async with asyncssh.connect(host, username='username', password='password') as conn:
- async with asyncssh.connect(host, username='username') as conn:
- return await conn.run(command)
- async def run_multiple_clients():
- # Put your lists of hosts here
- hosts = ['192.168.1.2','','']
- tasks = (run_client(host, "echo 'password' | sudo -S init 0") for host in hosts)
- results = await asyncio.gather(*tasks, return_exceptions=True)
- #print(results)
- for i, result in enumerate(results, 1):
- #you might have to change the if else stuff based on the command you choose to execute
- if isinstance(result, Exception):
- print('Task %d failed: %s' % (i, str(result)))
- elif not result.stdout:
- print('Task %d : Connection closed by remote host.' % (i))
- # print(75*'-')
- asyncio.get_event_loop().run_until_complete(run_multiple_clients())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement