1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/usr/bin/env python3.7
#
# Copyright (c) 2013-2024 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
# GNU General Public License, Version 2.0, or any later versions of
# that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
# To run this program, the file server_key must exist on the server,
# containing an SSH private key for the server to use to authenticate itself
# to the client. An SSH certificate can optionally be provided in the file
# server_key-cert.pub.
#
# The file trusted_client_host_keys must also exist on the server, containing
# a list of trusted client host keys or a @cert-authority entry with a public
# key trusted to sign client host keys if certificates are used. This file
# should be in "known_hosts" format.
import asyncio, asyncssh, sys
async def run_commands(conn: asyncssh.SSHClientConnection) -> None:
"""Run a series of commands on the client which connected to us"""
commands = ('ls', 'sleep 30 && date', 'sleep 5 && cat /proc/cpuinfo')
async with conn:
tasks = [conn.run(cmd) for cmd in commands]
for task in asyncio.as_completed(tasks):
result = await task
print('Command:', result.command)
print('Return code:', result.returncode)
print('Stdout:')
print(result.stdout, end='')
print('Stderr:')
print(result.stderr, end='')
print(75*'-')
async def start_reverse_server() -> None:
"""Accept inbound connections and then become an SSH client on them"""
await asyncssh.listen_reverse(port=8022, client_keys=['server_key'],
known_hosts='trusted_client_host_keys',
acceptor=run_commands)
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(start_reverse_server())
except (OSError, asyncssh.Error) as exc:
sys.exit('Error starting server: ' + str(exc))
loop.run_forever()
|