Created
June 23, 2018 16:26
-
-
Save grafuls/d542e10ee5bdaa7d74c543328f60af09 to your computer and use it in GitHub Desktop.
asyncssh example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio, asyncssh, sys | |
| class MySSHClientSession(asyncssh.SSHClientSession): | |
| def data_received(self, data, datatype): | |
| print(data, end='') | |
| def connection_lost(self, exc): | |
| if exc: | |
| print('SSH session error: ' + str(exc), file=sys.stderr) | |
| async def run_client(host, cmd): | |
| conn, client = await asyncssh.create_connection(asyncssh.SSHClient, host) | |
| async with conn: | |
| chan, session = await conn.create_session(MySSHClientSession, cmd) | |
| await chan.wait_closed() | |
| CMD = "ping %s" | |
| try: | |
| asyncio.get_event_loop().run_until_complete(asyncio.gather( | |
| run_client('localhost', CMD % 'localhost'), | |
| run_client('localhost', CMD % 'google.com') | |
| )) | |
| except (OSError, asyncssh.Error) as exc: | |
| sys.exit('SSH connection failed: ' + str(exc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment