Spaces:
Sleeping
Sleeping
Download tools/nmap_tool.py from bh4utik/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/bh4utik/First_agent_template/resolve/main/tools/nmap_tool.py
- Command line
-
hf download hf://spaces/bh4utik/First_agent_template/tools/nmap_tool.py
-
curl -L -o nmap_tool.py https://huggingface.co/spaces/bh4utik/First_agent_template/resolve/main/tools/nmap_tool.py
1.17 kB
| import subprocess | |
| import shlex | |
| from smolagents import tool | |
| def scan_target(target: str, scan_type: str = "basic") -> str: | |
| """ | |
| Runs a controlled Nmap scan against a target. | |
| Args: | |
| target: Target domain or IP address to scan. | |
| scan_type: Type of scan to perform. Supported values: | |
| - basic: Fast port scan | |
| - service: Service/version detection | |
| Returns: | |
| Raw Nmap scan output as text. | |
| """ | |
| allowed_scan_types = { | |
| "basic": "-F", | |
| "service": "-sV", | |
| "full": "-p-" | |
| } | |
| if scan_type not in allowed_scan_types: | |
| return "Error: Unsupported scan type." | |
| # Very basic validation to reduce command injection risk | |
| if not all(c.isalnum() or c in ".:-" for c in target): | |
| return "Error: Invalid target format." | |
| nmap_flag = allowed_scan_types[scan_type] | |
| command = f"nmap {nmap_flag} {target}" | |
| try: | |
| result = subprocess.run( | |
| shlex.split(command), | |
| capture_output=True, | |
| text=True, | |
| timeout=60 | |
| ) | |
| return result.stdout | |
| except Exception as e: | |
| return f"Scan failed: {str(e)}" | |