MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / run_nmap_scan

Function run_nmap_scan

actions/Scanner.py:60–112  ·  view source on GitHub ↗

Run Nmap scan on specific IP to get hostname and open ports

(ip_address, port_range='1-1000')

Source from the content-addressed store, hash-verified

58 return []
59
60def run_nmap_scan(ip_address, port_range='1-1000'):
61 """Run Nmap scan on specific IP to get hostname and open ports"""
62 print(f"[*] Scanning {ip_address} with Nmap...")
63
64 try:
65 # Run nmap with service detection
66 result = subprocess.run(
67 ['nmap', '-sV', '-p', port_range, '--open', ip_address],
68 capture_output=True,
69 text=True,
70 timeout=120
71 )
72
73 output = result.stdout
74
75 # Extract hostname
76 hostname = 'Unknown'
77 hostname_match = re.search(r'Nmap scan report for (.*?) \(', output)
78 if hostname_match:
79 hostname = hostname_match.group(1)
80 else:
81 hostname_match = re.search(r'Nmap scan report for (.*)', output)
82 if hostname_match:
83 hostname = hostname_match.group(1)
84
85 # Extract open ports
86 ports = []
87 port_section = False
88 for line in output.split('\n'):
89 if 'PORT' in line and 'STATE' in line:
90 port_section = True
91 continue
92 if port_section and line.strip():
93 match = re.match(r'(\d+)/(\w+)\s+(\w+)\s+(.*)', line)
94 if match:
95 ports.append({
96 'port': match.group(1),
97 'protocol': match.group(2),
98 'state': match.group(3),
99 'service': match.group(4).strip()
100 })
101
102 return {
103 'hostname': hostname,
104 'ports': ports
105 }
106
107 except subprocess.TimeoutExpired:
108 print(f"[!] Nmap scan timed out for {ip_address}")
109 return {'hostname': 'Timeout', 'ports': []}
110 except Exception as e:
111 print(f"[!] Error scanning {ip_address}: {e}")
112 return {'hostname': 'Error', 'ports': []}
113
114def scan_network(interface='eth0', port_range='1-1000', quick=False):
115 """Main function to scan network"""

Callers 1

scan_networkFunction · 0.85

Calls 2

appendMethod · 0.80
runMethod · 0.45

Tested by

no test coverage detected