Explorar el Código

修复253远端连接问题

lxylxy123321 hace 1 semana
padre
commit
d82210141c
Se han modificado 1 ficheros con 8 adiciones y 6 borrados
  1. 8 6
      backend/app/core/remote_executor.py

+ 8 - 6
backend/app/core/remote_executor.py

@@ -13,10 +13,6 @@ settings = get_settings()
 def _get_ssh_prefix() -> list[str]:
     """构建 ssh/scp 命令前缀,支持密钥或密码登录。"""
     prefix = ["-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10"]
-    if settings.compute_node_ssh_key:
-        prefix.extend(["-i", settings.compute_node_ssh_key])
-    elif settings.compute_node_ssh_password:
-        prefix = ["sshpass", "-p", settings.compute_node_ssh_password] + prefix
     return prefix
 
 
@@ -26,17 +22,23 @@ def ssh_exec(cmd: str, timeout: int | None = None) -> tuple[int, str, str]:
         raise RuntimeError("未配置算力节点(compute_node_host 为空)")
 
     target = f"{settings.compute_node_ssh_user}@{settings.compute_node_host}"
-    ssh_cmd = [
+    ssh_args = [
         "ssh", *_get_ssh_prefix(),
         "-p", str(settings.compute_node_ssh_port),
         target,
         cmd,
     ]
 
+    # sshpass 需要包裹 ssh 命令,而不是作为 ssh 的参数
+    if settings.compute_node_ssh_key:
+        ssh_args = ["ssh", "-i", settings.compute_node_ssh_key] + ssh_args[1:]
+    elif settings.compute_node_ssh_password:
+        ssh_args = ["sshpass", "-p", settings.compute_node_ssh_password] + ssh_args
+
     timeout = timeout or settings.compute_node_ssh_timeout
     try:
         proc = subprocess.run(
-            ssh_cmd,
+            ssh_args,
             capture_output=True,
             text=True,
             timeout=timeout,