import paramiko, time NAS_HOST = "100.85.152.112" NAS_PORT = 22 NAS_USER = "simon" NAS_PASS = "Nas82829330" NAS_DEPLOY_PATH = "/volume1/docker/gaokao-portal" DOCKER_BIN = "/usr/local/bin/docker" c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(NAS_HOST, NAS_PORT, NAS_USER, NAS_PASS, timeout=20) def shell(cmd): transport = c.get_transport() ch = transport.open_session() ch.get_pty() ch.exec_command(cmd) out = b"" while True: if ch.recv_ready(): out += ch.recv(4096) if ch.exit_status_ready(): break time.sleep(0.3) while ch.recv_ready(): out += ch.recv(4096) return out.decode() # 手动构建 compose 命令 CMD = f"echo '{NAS_PASS}' | sudo -S {DOCKER_BIN} compose -f {NAS_DEPLOY_PATH}/docker-compose.yml" # 启动所有容器 print("启动容器...") print(shell(f"{CMD} up -d")) time.sleep(20) # 检查状态 print(shell(f"{CMD} ps")) # 检查日志 print("\n--- 后端日志 ---") print(shell(f"{CMD} logs backend | tail -20")) # 测试 API print("\n--- API 测试 ---") print(shell("curl -s http://localhost:8080/api/questionnaires | head -c 500")) c.close()