Using nmap from python

python-nmap is a python library which helps in using nmap port scanner. It supports nmap script outputs.

python-nmap
For Ubuntu users:

sudo apt-get install python-nmap
#!/usr/bin/env python
import nmap
nm = nmap.PortScanner()
nm.scan('127.0.0.1', '21-443')
for host in nm.all_hosts():
	print('----------------------------------------------------')
	print('Host : %s (%s)' % (host, nm[host].hostname()))
	print('State : %s' % nm[host].state())
	for proto in nm[host].all_protocols():
		print('----------')
		print('Protocol : %s' % proto)
		lport = nm[host][proto].keys()
		lport.sort()
		for port in lport:
			print ('port : %ststate : %s' % (port, nm[host][proto][port]['state']))
Categories: Python Tags: ,
%d bloggers like this: