The `python-nmap` library is a wrapper around the Nmap binary. It parses the XML output for you, returning a lovely JSON object.
# Simple Network Scanner
import nmap
nm = nmap.PortScanner()
res = nm.scan('192.168.1.0/24', '22-80')
for host in nm.all_hosts():
print(f'Host: {host} ({nm[host].hostname()})')
for proto in nm[host].all_protocols():
lport = nm[host][proto].keys()
for port in lport:
print(f'port : {port}\tstate : {nm[host][proto][port]["state"]}')
Speed
Python is slower than C, but Nmap does the heavy lifting.
Your script just orchestrates it.
Use Python's `multiprocessing` library to run 10 Nmap instances at once for a massive speed boost.
1. Integrating with Databases
The real power comes when you save results to a database (MongoDB or SQL).
Scan every day. Diff the results.
Alert via Slack if a NEW port opens up. That is how you build a Security Operations Center (SOC).