1#!/usr/bin/env python3.6 2# 3# Copyright (c) 2013-2018 by Ron Frederick <ronf@timeheart.net> and others. 4# 5# This program and the accompanying materials are made available under 6# the terms of the Eclipse Public License v2.0 which accompanies this 7# distribution and is available at: 8# 9# http://www.eclipse.org/legal/epl-2.0/ 10# 11# This program may also be made available under the following secondary 12# licenses when the conditions for such availability set forth in the 13# Eclipse Public License v2.0 are satisfied: 14# 15# GNU General Public License, Version 2.0, or any later versions of 16# that license 17# 18# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 19# 20# Contributors: 21# Ron Frederick - initial implementation, API, and documentation 22 23# To run this program, the file ``ssh_host_key`` must exist with an SSH 24# private key in it to use as a server host key. An SSH host certificate 25# can optionally be provided in the file ``ssh_host_key-cert.pub``. 26# 27# The file ``ssh_user_ca`` must exist with a cert-authority entry of 28# the certificate authority which can sign valid client certificates. 29 30import asyncio, asyncssh, sys 31 32class MySSHServerSession(asyncssh.SSHServerSession): 33 def __init__(self): 34 self._input = '' 35 self._total = 0 36 37 def connection_made(self, chan): 38 self._chan = chan 39 40 def shell_requested(self): 41 return True 42 43 def session_started(self): 44 self._chan.write('Enter numbers one per line, or EOF when done:\n') 45 46 def data_received(self, data, datatype): 47 self._input += data 48 49 lines = self._input.split('\n') 50 for line in lines[:-1]: 51 try: 52 if line: 53 self._total += int(line) 54 except ValueError: 55 self._chan.write_stderr('Invalid number: %s\n' % line) 56 57 self._input = lines[-1] 58 59 def eof_received(self): 60 self._chan.write('Total = %s\n' % self._total) 61 self._chan.exit(0) 62 63 def break_received(self, msec): 64 self.eof_received() 65 66class MySSHServer(asyncssh.SSHServer): 67 def session_requested(self): 68 return MySSHServerSession() 69 70async def start_server(): 71 await asyncssh.create_server(MySSHServer, '', 8022, 72 server_host_keys=['ssh_host_key'], 73 authorized_client_keys='ssh_user_ca') 74 75loop = asyncio.get_event_loop() 76 77try: 78 loop.run_until_complete(start_server()) 79except (OSError, asyncssh.Error) as exc: 80 sys.exit('Error starting server: ' + str(exc)) 81 82loop.run_forever() 83