inspect_network
Check network interfaces and connection status on remote servers via SSH to monitor connectivity and identify potential issues.
Instructions
检查网络接口和连接状态
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hostname | Yes | ||
| username | Yes | ||
| password | No | ||
| port | No | ||
| timeout | No |
Implementation Reference
- Core handler function implementing the inspect_network tool logic: SSH to server, run 'ip a' and 'ss -tuln' to get interfaces and listening ports, ping 8.8.8.8 for internet connectivity.@handle_exceptions def inspect_network( hostname: str, username: str, password: str = "", port: int = 22, timeout: int = 30 ) -> dict: """检查网络接口和连接状态""" result = {"status": "unknown", "interfaces": [], "connections": {}, "error": ""} try: with SSHManager(hostname, username, password, port, timeout) as ssh: # 获取网络接口信息 interfaces_command = "ip a" stdin, stdout, stderr = ssh.exec_command(interfaces_command, timeout=timeout) interfaces_output = stdout.read().decode().strip() # 解析网络接口信息 result["interfaces"] = ServerInspector.parse_network_interfaces(interfaces_output) # 获取网络连接信息 connections_command = "ss -tuln" stdin, stdout, stderr = ssh.exec_command(connections_command, timeout=timeout) connections_output = stdout.read().decode().strip() # 解析监听端口 listening_ports = [] for line in connections_output.split('\n')[1:]: # 跳过标题行 if "LISTEN" in line: parts = line.split() if len(parts) >= 5: address_port = parts[4] if ":" in address_port: port = address_port.split(":")[-1] listening_ports.append(port) result["connections"]["listening_ports"] = listening_ports # 检查是否可以连接公网 internet_check = ssh.exec_command("ping -c 1 -W 2 8.8.8.8", timeout=timeout) internet_output = internet_check[1].read().decode().strip() result["connections"]["internet_connectivity"] = "1 received" in internet_output result["status"] = "success" except Exception as e: result["status"] = "error" result["error"] = str(e) return result
- SSE variant of the core handler function for inspect_network tool, nearly identical to the main one.@handle_exceptions def inspect_network( hostname: str, username: str, password: str = "", port: int = 22, timeout: int = 30 ) -> dict: """检查网络接口和连接状态""" result = {"status": "unknown", "interfaces": [], "connections": {}, "error": ""} try: with SSHManager(hostname, username, password, port, timeout) as ssh: # 获取网络接口信息 interfaces_command = "ip a" stdin, stdout, stderr = ssh.exec_command(interfaces_command, timeout=timeout) interfaces_output = stdout.read().decode().strip() # 解析网络接口信息 result["interfaces"] = ServerInspector.parse_network_interfaces(interfaces_output) # 获取网络连接信息 connections_command = "ss -tuln" stdin, stdout, stderr = ssh.exec_command(connections_command, timeout=timeout) connections_output = stdout.read().decode().strip() # 解析监听端口 listening_ports = [] for line in connections_output.split('\n')[1:]: # 跳过标题行 if "LISTEN" in line: parts = line.split() if len(parts) >= 5: address_port = parts[4] if ":" in address_port: port = address_port.split(":")[-1] listening_ports.append(port) result["connections"]["listening_ports"] = listening_ports # 检查是否可以连接公网 internet_check = ssh.exec_command("ping -c 1 -W 2 8.8.8.8", timeout=timeout) internet_output = internet_check[1].read().decode().strip() result["connections"]["internet_connectivity"] = "1 received" in internet_output result["status"] = "success" except Exception as e: result["status"] = "error" result["error"] = str(e) return result
- server_monitor/tools/utils.py:78-84 (schema)JSON schema definition for the inspect_network tool parameters used in tool listing.{"name": "inspect_network", "description": "检查网络接口和连接状态", "parameters": [ {"name": "hostname", "type": "str", "default": None}, {"name": "username", "type": "str", "default": None}, {"name": "password", "type": "str", "default": ""}, {"name": "port", "type": "int", "default": 22}, {"name": "timeout", "type": "int", "default": 30} ]},
- server_monitor/main.py:42-65 (registration)Registration of inspect_network in tools_dict and dynamic application of @mcp.tool() decorator.tools_dict = { 'get_memory_info': get_memory_info, 'remote_server_inspection': remote_server_inspection, 'get_system_load': get_system_load, 'monitor_processes': monitor_processes, 'check_service_status': check_service_status, 'get_os_details': get_os_details, 'check_ssh_risk_logins': check_ssh_risk_logins, 'check_firewall_config': check_firewall_config, 'security_vulnerability_scan': security_vulnerability_scan, 'backup_critical_files': backup_critical_files, 'inspect_network': inspect_network, 'analyze_logs': analyze_logs, 'list_docker_containers': list_docker_containers, 'list_docker_images': list_docker_images, 'list_docker_volumes': list_docker_volumes, 'get_container_logs': get_container_logs, 'monitor_container_stats': monitor_container_stats, 'check_docker_health': check_docker_health } # 使用装饰器动态注册所有工具 for name, func in tools_dict.items(): mcp.tool()(func)
- server_monitor_sse/server.py:188-200 (registration)Explicit tool dispatch block for inspect_network in SSE server handler.elif name == "inspect_network": required_args = ["hostname", "username"] for arg in required_args: if arg not in arguments: raise ValueError(f"Missing required argument '{arg}'") result = inspect_network( hostname=arguments["hostname"], username=arguments["username"], password=arguments.get("password", ""), port=arguments.get("port", 22), timeout=arguments.get("timeout", 30) )