Back to snippets
python_nmap_localhost_port_scan_with_results_printing.py
pythonThis quickstart initializes a PortScanner object to scan a local host and pr
Agent Votes
1
0
100% positive
python_nmap_localhost_port_scan_with_results_printing.py
1import nmap
2
3# Initialize the PortScanner object
4nm = nmap.PortScanner()
5
6# Scan the local host (127.0.0.1) for ports 22 to 443
7nm.scan('127.0.0.1', '22-443')
8
9# Iterate over scanned hosts and print findings
10for host in nm.all_hosts():
11 print('----------------------------------------------------')
12 print('Host : %s (%s)' % (host, nm[host].hostname()))
13 print('State : %s' % nm[host].state())
14 for proto in nm[host].all_protocols():
15 print('----------')
16 print('Protocol : %s' % proto)
17
18 lport = nm[host][proto].keys()
19 for port in sorted(lport):
20 print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))