fix: 修复 Windows 下 SSH 命令找不到的问题
subprocess.run(cmd, shell=True) 在 Windows 调用 cmd.exe 时找不到 ssh/scp。 改为使用列表参数 + shell=False 直接执行 ssh/scp 二进制,避开 cmd.exe PATH 解析问题。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -101,16 +101,43 @@ def run_command(cmd, cwd=None, timeout=120, capture=True):
|
||||
return False, "", str(e)
|
||||
|
||||
|
||||
def run_ssh_args(args, timeout=30, capture=True):
|
||||
"""执行 ssh/scp 命令(使用列表参数,避免 Windows cmd.exe 找不到 ssh)"""
|
||||
try:
|
||||
if capture:
|
||||
result = subprocess.run(
|
||||
args, shell=False, cwd=PROJECT_DIR,
|
||||
capture_output=True, text=True, encoding='utf-8', errors='replace',
|
||||
timeout=timeout
|
||||
)
|
||||
return result.returncode == 0, result.stdout.strip(), result.stderr.strip()
|
||||
else:
|
||||
result = subprocess.run(
|
||||
args, shell=False, cwd=PROJECT_DIR, timeout=timeout
|
||||
)
|
||||
return result.returncode == 0, "", ""
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "", f"命令执行超时 ({timeout}s): {' '.join(args)}"
|
||||
except Exception as e:
|
||||
return False, "", str(e)
|
||||
|
||||
|
||||
def ssh_command(cmd, timeout=30):
|
||||
"""在远程服务器执行命令"""
|
||||
full = f'ssh {SSH_OPTS} {USERNAME}@{SERVER_IP} "{cmd}"'
|
||||
return run_command(full, timeout=timeout)
|
||||
return run_ssh_args([
|
||||
"ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10",
|
||||
"-o", "StrictHostKeyChecking=no",
|
||||
f"{USERNAME}@{SERVER_IP}", cmd
|
||||
], timeout=timeout)
|
||||
|
||||
|
||||
def scp_file(local, remote, timeout=120):
|
||||
"""上传文件到远程服务器"""
|
||||
full = f'scp {SSH_OPTS} "{local}" {USERNAME}@{SERVER_IP}:"{remote}"'
|
||||
return run_command(full, timeout=timeout)
|
||||
return run_ssh_args([
|
||||
"scp", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10",
|
||||
"-o", "StrictHostKeyChecking=no",
|
||||
local, f"{USERNAME}@{SERVER_IP}:{remote}"
|
||||
], timeout=timeout)
|
||||
|
||||
|
||||
def check_ssh():
|
||||
|
||||
Reference in New Issue
Block a user