fix: handle GBK encoding from Windows schtasks output via SSH

This commit is contained in:
hmo
2026-07-19 08:20:08 +08:00
parent 9fca8185a5
commit 4bba29965f
+17 -5
View File
@@ -844,7 +844,7 @@ def api_monitor():
status = "error"
result["tasks"].append({"name": name, "status": status})
else:
# Linux: SSH 到 Windows 查 schtasks
# Linux: SSH 到 Windows 查 schtasks(输出可能是 GBK
win_host = os.environ.get("WINDOWS_HOST", "192.168.1.16")
for name in expected_tasks:
try:
@@ -852,13 +852,25 @@ def api_monitor():
["ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=5",
"-o", "BatchMode=yes", f"hmo@{win_host}",
f"schtasks /Query /TN {name} /FO CSV /NH"],
capture_output=True, text=True, timeout=10
capture_output=True, timeout=10
)
if r.returncode == 0 and name in r.stdout:
if "就绪" in r.stdout or "Ready" in r.stdout or "Running" in r.stdout:
# 先看去重定向码:非0 = 任务不存在
if r.returncode != 0:
result["tasks"].append({"name": name, "status": "not_deployed"})
continue
# 尝试解码输出(Windows schtasks 默认 GBK,也试试 UTF-8
out = ""
for enc in ["utf-8", "gbk", "gb2312", "cp936"]:
try:
out = r.stdout.decode(enc)
break
except UnicodeDecodeError:
continue
if name in out:
if "就绪" in out or "Ready" in out or "Running" in out:
result["tasks"].append({"name": name, "status": "schtasks_ok"})
else:
result["tasks"].append({"name": name, "status": "schtasks_unknown", "raw": r.stdout.strip()})
result["tasks"].append({"name": name, "status": "schtasks_unknown", "raw": out.strip()})
else:
result["tasks"].append({"name": name, "status": "not_deployed"})
except Exception as e: