From 0de0733d3735a0ab6dc3e10bee8173b71ce1deb7 Mon Sep 17 00:00:00 2001 From: master3395 Date: Thu, 26 Feb 2026 21:00:10 +0100 Subject: [PATCH] fix(webterminal): use SSH port from sshd_config for custom SSH port (fixes #1713) - Add get_ssh_port() to read Port from /etc/ssh/sshd_config - Use 127.0.0.1 and port=SSH_PORT in asyncssh.connect() - Web Terminal now works when SSH runs on non-22 port --- fastapi_ssh_server.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/fastapi_ssh_server.py b/fastapi_ssh_server.py index 22ed566cd..923caaec0 100644 --- a/fastapi_ssh_server.py +++ b/fastapi_ssh_server.py @@ -27,6 +27,27 @@ app.add_middleware( SSH_USER = "your_website_user" # Replace with a real user for testing AUTHORIZED_KEYS_PATH = f"/home/{SSH_USER}/.ssh/authorized_keys" +# Read the actual SSH port from sshd_config (fixes WebTerminal when SSH uses custom port) +def get_ssh_port() -> int: + try: + with open("/etc/ssh/sshd_config", "r") as f: + for line in f: + line = line.strip() + if not line or line.startswith('#'): + continue + line = line.split('#')[0].strip() + parts = line.split() + if len(parts) >= 2 and parts[0].lower() == 'port': + port = int(parts[1]) + logging.info(f"[get_ssh_port] SSH port detected: {port}") + return port + except Exception as e: + logging.warning(f"[get_ssh_port] Could not read sshd_config: {e}") + logging.warning("[get_ssh_port] Falling back to default port 22") + return 22 + +SSH_PORT = get_ssh_port() + # Helper to generate a keypair def generate_ssh_keypair(): key = paramiko.RSAKey.generate(2048) @@ -90,7 +111,8 @@ async def websocket_endpoint(websocket: WebSocket, token: str = Query(None), ssh process = None try: conn = await asyncssh.connect( - "localhost", + "127.0.0.1", + port=SSH_PORT, username=user, client_keys=[keyfile_path], known_hosts=None