nmap_os_detection
Identify operating systems on network targets using Nmap's OS fingerprinting capabilities to enhance network security analysis and inventory management.
Instructions
Perform operating system detection scan
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| targets | Yes | ||
| ports | No | common | |
| max_retries | No |
Implementation Reference
- server.py:140-158 (handler)The main handler function for the nmap_os_detection tool, decorated with @app.tool for registration. It constructs Nmap arguments for OS detection (-O flag) and executes via run_nmap_command helper, returning scan results or error.@app.tool( name="nmap_os_detection", description="Perform operating system detection scan" ) async def nmap_os_detection( targets: str, ports: str = "common", max_retries: int = 2 ) -> str: """Perform operating system detection scan.""" args = ["-O", f"--osscan-retries={max_retries}", "-p", ports, targets] result = run_nmap_command(args) if result["success"]: return f"OS detection scan completed:\n\n{result['stdout']}" else: return f"OS detection scan failed:\n\n{result['stderr']}"
- server.py:140-143 (registration)Registration of the nmap_os_detection tool using FastMCP's @app.tool decorator, specifying the tool name and description.@app.tool( name="nmap_os_detection", description="Perform operating system detection scan" )
- server.py:38-92 (helper)Shared helper function used by nmap_os_detection and other tools to execute Nmap commands via subprocess, handling output, errors, and timeouts.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 }