inspect_network
Check network interfaces and connection status on servers using hostname, username, and optional parameters like port and timeout for remote diagnostics.
Instructions
检查网络接口和连接状态
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| hostname | Yes | ||
| password | No | ||
| port | No | ||
| timeout | No | ||
| username | Yes |
Implementation Reference
- Core implementation of the inspect_network tool: connects via SSH, retrieves network interfaces with 'ip a', parses them, gets listening ports with 'ss -tuln', checks internet with ping to 8.8.8.8.@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 inspect_network tool handler, functionally identical to the main version.@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/main.py:42-66 (registration)Registration of inspect_network in the tools dictionary and dynamic @mcp.tool() decoration in the FastMCP server.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)Manual dispatch and call to inspect_network in the SSE server's tool_handler function.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) )
- server_monitor/tools/utils.py:78-84 (schema)Tool schema definition including name, description, and parameters for inspect_network.{"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} ]},