Skip to content

How can I handle session requests? #180

@supriyo-biswas

Description

@supriyo-biswas

First of all, thanks for the great project! :)

I'm trying to log a few things from a AsyncSSH server, using a custom session. Typically handling sessions are easy enough with asyncssh.create_server(process_factory=handler), but It's not clear to me from reading the docs and the source as to how I can handle the session request when using a custom Session class.

Here's the code I have:

import sys
import asyncio
import subprocess
from asyncssh import *

class MySession(SSHServerSession):
    def connection_made(self, chan):
        self.ip = chan.get_extra_info('peername')[0]

    def shell_requested(self):
        print(f'{self.ip} - Shell session requested')
        return True

    def exec_requested(self, command):
        print(f'{self.ip} - Exec requested')
        return True

    def subsystem_requested(self, subsystem):
        print(f'{self.ip} - Subsystem requested')
        return True

class MySSHServer(SSHServer):
    def connection_made(self, conn):
        self.ip = conn.get_extra_info('peername')[0]
        print(f'{self.ip} - Connection received')

    def connection_lost(self, exc):
        print(f'{self.ip} - Connection closed')

    def begin_auth(self, username):
        return True

    def password_auth_supported(self):
        return True

    def kbdint_auth_supported(self):
        return False

    def validate_password(self, username, password):
        rv = len(password) > 4 and len(username) > 2
        status = "accepted" if rv else "rejected"

        print(f'{self.ip} - Password auth {status}: {username}:{password}')
        return rv

    def session_requested(self):
        print(f'{self.ip} - Session requested')
        return MySession()

async def start_server():
    await create_server(MySSHServer, '', 5833, server_host_keys=['ssh_host_key'])

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(start_server())
    loop.run_forever()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions