1#!/usr/bin/env python3.8 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 server_key must exist on the server, 24# containing an SSH private key for the server to use to authenticate itself 25# to the client. An SSH certificate can optionally be provided in the file 26# server_key-cert.pub. 27# 28# The file trusted_client_host_keys must also exist on the server, containing 29# a list of trusted client host keys or a @cert-authority entry with a public 30# key trusted to sign client host keys if certificates are used. This file 31# should be in "known_hosts" format. 32 33import asyncio, asyncssh, sys 34 35async def run_commands(conn): 36 """Run a series of commands on the client which connected to us""" 37 38 commands = ('ls', 'sleep 30 && date', 'sleep 5 && cat /proc/cpuinfo') 39 40 async with conn: 41 tasks = [conn.run(cmd) for cmd in commands] 42 43 for task in asyncio.as_completed(tasks): 44 result = await task 45 print('Command:', result.command) 46 print('Return code:', result.returncode) 47 print('Stdout:') 48 print(result.stdout, end='') 49 print('Stderr:') 50 print(result.stderr, end='') 51 print(75*'-') 52 53async def start_reverse_server(): 54 """Accept inbound connections and then become an SSH client on them""" 55 56 await asyncssh.listen_reverse(port=8022, client_keys=['server_key'], 57 known_hosts='trusted_client_host_keys', 58 acceptor=run_commands) 59 60loop = asyncio.get_event_loop() 61 62try: 63 loop.run_until_complete(start_reverse_server()) 64except (OSError, asyncssh.Error) as exc: 65 sys.exit('Error starting server: ' + str(exc)) 66 67loop.run_forever() 68