Implementing SSH clients and servers with the asyncSSH and asyncio modules
asyncssh (https://libraries.io/github/ronf/asyncssh) is a Python package that provides a server implementation of the SSHv2 protocol and an asynchronous client that works over the asyncio module in Python 3: https://docs.python.org/3/library/asyncio.html.
This module requires Python 3.4 or later and the Python cryptography library for some cryptographic functions. You can install asyncssh by running the following command:
$ python3 -m pip install asyncssh
In the following example, we're going to implement a client SSH server to execute the command introduced by the user. You can find the following code in the client_ssh.py file inside the asyncssh folder:
import asyncssh import asyncio import getpass async def execute_command(host, command, username, password): async with asyncssh.connect(host, username = username, password= password) as connection: ...