-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyssh.py
38 lines (27 loc) · 970 Bytes
/
myssh.py
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
import asyncio
import subprocess
from typing import Generator
import asyncssh
class NoAuthSSHServer(asyncssh.SSHServer):
"""No auth ssh server for local testing"""
def begin_auth(self, username: str) -> asyncssh.misc.MaybeAwait[bool]:
return False
async def simple_server(handler, port: int = 12361) -> Generator[int, None, None]:
"""Start a server and yield its port"""
private_key = asyncssh.generate_private_key("ssh-rsa")
async with asyncssh.create_server(
NoAuthSSHServer,
"localhost",
port,
server_host_keys=[private_key],
process_factory=handler,
encoding=None,
) as server:
# Await forever
await asyncio.Future()
if __name__ == "__main__":
def handler(process):
p = subprocess.run(["/bin/bash", "-c", process.command], capture_output=True)
process.stdout.write(p.stdout)
process.exit(0)
asyncio.run(simple_server(handler))