#!/usr/bin/env python3 """ 检查OpenClaw网关API状态的脚本 """ import requests import websocket import json import sys def check_http_api(): """检查HTTP API状态""" try: url = "http://127.0.0.1:18789/__openclaw__/api/v1/health" response = requests.get(url, timeout=5) print(f"HTTP API状态: {response.status_code}") print(f"响应内容: {response.text}") except Exception as e: print(f"HTTP API检查失败: {e}") def check_websocket_with_auth(): """使用身份验证检查WebSocket连接""" try: # 从配置文件中获取认证令牌 config_path = "/Users/chufeng/.openclaw/openclaw.json" with open(config_path, "r") as f: config = json.load(f) token = config["gateway"]["auth"]["token"] print(f"获取到认证令牌: {token}") # 使用身份验证连接WebSocket ws_url = f"ws://127.0.0.1:18789/__openclaw__/ws?token={token}" print(f"连接到WebSocket: {ws_url}") ws = websocket.create_connection(ws_url, timeout=5) print("WebSocket连接成功") # 发送节点列表请求 request_data = { "id": "1", "method": "node.list", "params": {} } ws.send(json.dumps(request_data)) print("已发送节点列表请求") # 接收响应 response = ws.recv() print(f"WebSocket响应: {response}") ws.close() except Exception as e: print(f"WebSocket检查失败: {e}") print(f"错误类型: {type(e).__name__}") def check_nodes_through_cli(): """通过CLI命令检查节点列表""" print("\n=== 通过OpenClaw CLI检查节点列表 ===") try: import subprocess result = subprocess.run( ["openclaw", "nodes", "list"], capture_output=True, text=True, timeout=30 ) print(f"命令执行状态: {result.returncode}") print(f"标准输出: {result.stdout}") print(f"标准错误: {result.stderr}") except Exception as e: print(f"命令执行失败: {e}") if __name__ == "__main__": print("开始检查OpenClaw网关状态...") print("=" * 50) check_http_api() print("\n" + "=" * 50) check_websocket_with_auth() print("\n" + "=" * 50) check_nodes_through_cli() print("\n" + "=" * 50) print("检查完成")