nmap_network_discovery
Scan and identify hosts, services, and open ports on a network using specified discovery methods to analyze network activity and detect connected devices.
Instructions
Discover hosts and services on a network
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| discovery_method | No | all | |
| include_ports | No | ||
| network | Yes |
Implementation Reference
- server.py:296-319 (handler)The handler function that implements the logic for the nmap_network_discovery tool. It constructs Nmap arguments based on discovery method and ports option, executes the scan using run_nmap_command, and returns formatted results.async def nmap_network_discovery( network: str, discovery_method: str = "all", include_ports: bool = True ) -> str: """Discover hosts and services on a network.""" if discovery_method == "ping": args = ["-sn", network] elif discovery_method == "arp": args = ["-PR", network] elif discovery_method == "syn": args = ["-PS", network] else: # all args = ["-sn", "-PS", "-PA", network] if include_ports: args.extend(["-sS", "-sV", "--top-ports=100"]) result = run_nmap_command(args) if result["success"]: return f"Network discovery completed:\n\n{result['stdout']}" else: return f"Network discovery failed:\n\n{result['stderr']}"
- server.py:292-295 (registration)The FastMCP decorator that registers the nmap_network_discovery tool with its name and description.@app.tool( name="nmap_network_discovery", description="Discover hosts and services on a network" )
- server.py:38-92 (helper)Core helper function that runs Nmap subprocess commands, handles output, errors, timeouts, and returns structured results. Used by the nmap_network_discovery handler.def run_nmap_command(args: List[str], timeout: int = 300) -> Dict[str, Any]: """ Execute an nmap command and return the results. Args: args: List of nmap command arguments timeout: Command timeout in seconds Returns: Dictionary containing command output, error, and exit code """ try: # Construct the full nmap command cmd = ["nmap"] + args logger.info(f"Executing nmap command: {' '.join(cmd)}") # Run the command with timeout result = subprocess.run( cmd, capture_output=True, text=True, timeout=timeout, check=False ) return { "stdout": result.stdout, "stderr": result.stderr, "exit_code": result.returncode, "success": result.returncode == 0 } except subprocess.TimeoutExpired: return { "stdout": "", "stderr": f"Command timed out after {timeout} seconds", "exit_code": -1, "success": False } except FileNotFoundError: return { "stdout": "", "stderr": "nmap command not found. Please ensure nmap is installed and in PATH", "exit_code": -1, "success": False } except Exception as e: return { "stdout": "", "stderr": f"Error executing nmap command: {str(e)}", "exit_code": -1, "success": False }