Skip to content

Instantly share code, notes, and snippets.

@grafuls
Created June 23, 2018 16:26
Show Gist options
  • Select an option

  • Save grafuls/d542e10ee5bdaa7d74c543328f60af09 to your computer and use it in GitHub Desktop.

Select an option

Save grafuls/d542e10ee5bdaa7d74c543328f60af09 to your computer and use it in GitHub Desktop.
asyncssh example
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