Skip to main content

This is a python class to use nmap and access scan results from python3

Project description


python-nmap is a python library which helps in using nmap port scanner.
It allows to easilly manipulate nmap scan results and will be a perfect
tool for systems administrators who want to automatize scanning task
and reports. It also supports nmap script outputs.

Typical usage looks like::

#!/usr/bin/env python
import nmap # import module
nm = nmap.PortScanner() # instantiate nmap.PortScanner object
nm.scan('', '22-443') # scan host, ports from 22 to 443
nm.command_line() # get command line used for the scan : nmap -oX - -p 22-443
nm.scaninfo() # get nmap scan informations {'tcp': {'services': '22-443', 'method': 'connect'}}
nm.all_hosts() # get all hosts that were scanned
nm[''].hostname() # get hostname for host
nm[''].state() # get state of host (up|down|unknown|skipped)
nm[''].all_protocols() # get all scanned protocols ['tcp', 'udp'] in (ip|tcp|udp|sctp)
nm['']['tcp'].keys() # get all ports for tcp protocol
nm[''].all_tcp() # get all ports for tcp protocol (sorted version)
nm[''].all_udp() # get all ports for udp protocol (sorted version)
nm[''].all_ip() # get all ports for ip protocol (sorted version)
nm[''].all_sctp() # get all ports for sctp protocol (sorted version)
nm[''].has_tcp(22) # is there any information for port 22/tcp on host
nm['']['tcp'][22] # get infos about port 22 in tcp on host
nm[''].tcp(22) # get infos about port 22 in tcp on host
nm['']['tcp'][22]['state'] # get state of port 22/tcp on host (open

# a more usefull example :
for host in nm.all_hosts():
print('Host : %s (%s)' % (host, nm[host].hostname()))
print('State : %s' % nm[host].state())

for proto in nm[host].all_protocols():
print('Protocol : %s' % proto)

lport = nm[host][proto].keys()
for port in lport:
print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))

# If you want to do a pingsweep on network
nm.scan(hosts='', arguments='-n -sP -PE -PA21,23,80,3389')
hosts_list = [(x, nm[x]['status']['state']) for x in nm.all_hosts()]
for host, status in hosts_list:
print('{0}:{1}'.format(host, status))

print '----------------------------------------------------'
# Asynchronous usage of PortScannerAsync
nma = nmap.PortScannerAsync()
def callback_result(host, scan_result):
print '------------------'
print host, scan_result
nma.scan(hosts='', arguments='-sP', callback=callback_result)
while nma.still_scanning():
print("Waiting ...")
nma.wait(2) # you can do whatever you want but I choose to wait after the end of the scan



- corrected bug in about scope problem
- try to find nmap executable in known directories
- raise AssertionError when trying to call command_line, scaninfo, scanstats, has_host before scanning

- Make python-nmap works with Python 3.x
- Contribution from Brian Bustin <brian at>

Patches from Steve 'Ashcrow' Milner <steve at>
- remove shebang from as it is not a runnable script
- allow use with ALPHA and BETA nmap releases
- .has_key() is deprecated, replaced instances with in
- move to using the print function for python2 and 3 usag

- adding PortScanner.listscan
- PortScanner.scan now returns scan_result
- adding class PortScannerAsync (idea from Steve 'Ashcrow' Milner <steve at>)

- Import on google code
svn checkout python-nmap --username XXXXX
- added PortScanner.scanstats method
- updated and documentation for pingsweep
- updated Makefile for generating documentation

- Modified packaging. v0.1.1 [norman]

- Initial release. v0.1.0 [norman]

Project details

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page