class MySSHClientSession(asyncssh.SSHClientSession):
def data_received(self, data, datatype):
print(data, end='')
def connection_made(self, conn):
print("connection made")
def connection_lost(self, exc):
print("connection lost")
if exc:
print('SSH session error: ' + str(exc), file=sys.stderr)
async def run_client():
async with asyncssh.connect('localhost') as conn:
chan, session = await conn.create_session(MySSHClientSession)
#await chan.wait_closed()
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run_client())
except (OSError, asyncssh.Error) as exc:
sys.exit('Error starting server: ' + str(exc))
loop.run_forever()
Thank you for a detailed answer, Ron!
You mention “reverse SSH” here, but your example below seems to be a traditional “forward” direction SSH client.-- Yes, I'm trying to follow the method given in the shared link.You can see some examples of this at https://github.com/ronf/asyncssh/issues/205.-- Would be great if you can include them in the examples folder. I'll try this out separately by having the server authenticate to the client, like you have mentioned.In that implementation, it looks like the client only opens one SSH session with the server, so the server must send multiple commands over that single session. It also assumes that all of the output of the command being run is less than 1024 bytes, and that it will all be sent in a single SSH data message, which seems like a very questionable assumption.-- True, the code needs to be tweaked to perform better.Actually, this is my requirement:Client: Connect to server and accept reverse ssh tunnels to be opened on same connectionServer: Accepts connection from client, then opens 3 reverse ssh tunnels on the incoming connection. Each of these tunnels would run one command [“ls”, “sleep 30 && date”, “sleep 5 && cat /proc/cpuinfo”] Server program, in return, prints the received response for each of these commands (one should come back almost immediately, other after 5 and other after 30)Do you have some advice on how this scenario should ideally be implemented using asyncssh?
This is a good example. It should go to the examples folder in the repo.
Question:How do we create/manage the ssh_host_key and ssh_user_ca ?Server needs to have a way to trust the client(s) that are connectingClient further needs a way to trust the incoming reverse ssh sessions.
Can you provide an example of generating and using the certificates between the client and server. Which files need to go on which host (client/server)?
I looked at this response: https://github.com/ronf/asyncssh/issues/87#issuecomment-298142877Let's say I want each client to have a different user (client1_user, client2_user, etc..) For each client, I am assuming that a key pair (pvt, pub) needs to be created. The public key should be copied over to the servers authorized_keys? Is this correct? Are there other ways to do this?It would be good to add this to the README in examples folder (on how to guide to create/manage CA and client keys).
I should clarify this further.I am unclear on how to create the following keys (both client and server). An example would really help. thank you!