fix(ssh): ignore known_hosts to avoid host key mismatch

Use UserKnownHostsFile=/dev/null and GlobalKnownHostsFile=/dev/null for sshpass-based connections, so REMOTE HOST IDENTIFICATION HAS CHANGED does not break router actions.

Made-with: Cursor
This commit is contained in:
Андрей Бобырев
2026-04-29 16:32:49 +03:00
parent 22cc8035d4
commit 7624f46fad

View File

@@ -74,12 +74,18 @@ async def _ssh_on_router(rcfg: dict, remote_cmd: str, timeout: int = 45) -> tupl
pass pass
user = rcfg.get("user") or config.SSH_USER user = rcfg.get("user") or config.SSH_USER
pwd = rcfg.get("password") or config.SSH_PASS pwd = rcfg.get("password") or config.SSH_PASS
ssh_opts = [
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "GlobalKnownHostsFile=/dev/null",
"-o", "LogLevel=ERROR",
]
try: try:
r = await asyncio.to_thread( r = await asyncio.to_thread(
subprocess.run, subprocess.run,
[ [
"sshpass", "-p", pwd, "sshpass", "-p", pwd,
"ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=12", "ssh", *ssh_opts, "-o", "ConnectTimeout=12",
*extra_args, *extra_args,
f"{user}@{ssh_host}", remote_cmd, f"{user}@{ssh_host}", remote_cmd,
], ],
@@ -100,6 +106,12 @@ async def _push_one_router(server_url: str, router_key: str, rcfg: dict) -> dict
return {"router": router_key, "ok": False, "msg": "нет IP и нет тоннеля"} return {"router": router_key, "ok": False, "msg": "нет IP и нет тоннеля"}
user = rcfg.get("user") or config.SSH_USER user = rcfg.get("user") or config.SSH_USER
pwd = rcfg.get("password") or config.SSH_PASS pwd = rcfg.get("password") or config.SSH_PASS
ssh_opts = [
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "GlobalKnownHostsFile=/dev/null",
"-o", "LogLevel=ERROR",
]
cmd = ( cmd = (
f"curl -sf '{server_url}/hydra/domain.conf' -o /opt/etc/HydraRoute/domain.conf && " f"curl -sf '{server_url}/hydra/domain.conf' -o /opt/etc/HydraRoute/domain.conf && "
f"curl -sf '{server_url}/hydra/ip.list' -o /opt/etc/HydraRoute/ip.list && " f"curl -sf '{server_url}/hydra/ip.list' -o /opt/etc/HydraRoute/ip.list && "
@@ -107,7 +119,7 @@ async def _push_one_router(server_url: str, router_key: str, rcfg: dict) -> dict
) )
if tunnel_port: if tunnel_port:
ssh_cmd = ["sshpass", "-p", pwd, "ssh", ssh_cmd = ["sshpass", "-p", pwd, "ssh",
"-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10", *ssh_opts, "-o", "ConnectTimeout=10",
"-p", str(int(tunnel_port)), f"{user}@127.0.0.1", cmd] "-p", str(int(tunnel_port)), f"{user}@127.0.0.1", cmd]
else: else:
port_args: list[str] = [] port_args: list[str] = []
@@ -118,7 +130,7 @@ async def _push_one_router(server_url: str, router_key: str, rcfg: dict) -> dict
except Exception: except Exception:
port_args = [] port_args = []
ssh_cmd = ["sshpass", "-p", pwd, "ssh", ssh_cmd = ["sshpass", "-p", pwd, "ssh",
"-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10", *ssh_opts, "-o", "ConnectTimeout=10",
*port_args, *port_args,
f"{user}@{ip}", cmd] f"{user}@{ip}", cmd]
try: try: