Guest User

Untitled

a guest
Jun 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import asyncio, asyncssh, sys
  2.  
  3. class MySSHClientSession(asyncssh.SSHClientSession):
  4. def data_received(self, data, datatype):
  5. print(data, end='')
  6.  
  7. def connection_lost(self, exc):
  8. if exc:
  9. print('SSH session error: ' + str(exc), file=sys.stderr)
  10.  
  11. async def run_client(host, cmd):
  12. conn, client = await asyncssh.create_connection(asyncssh.SSHClient, host)
  13.  
  14. async with conn:
  15. chan, session = await conn.create_session(MySSHClientSession, cmd)
  16. await chan.wait_closed()
  17.  
  18. CMD = "ping %s"
  19. try:
  20. asyncio.get_event_loop().run_until_complete(asyncio.gather(
  21. run_client('localhost', CMD % 'localhost'),
  22. run_client('localhost', CMD % 'google.com')
  23. ))
  24. except (OSError, asyncssh.Error) as exc:
  25. sys.exit('SSH connection failed: ' + str(exc))
Add Comment
Please, Sign In to add comment