Brief Introduction to the use of the python-nmap Module
Python-nmap is a python module library that allows python to conveniently operate nmap scanners. It helps administrators to automatically scan tasks and generate reports, it also supports nmap step output. The latest example is python-nmap-0.2.4.tar.gz, which is: Export-nmap.0.1.4.tar.gz. After the download link is release & nbsp; install, it will be done. The source code of example. py is provided below :#! /Usr/bin/env python #-*-coding: latin-1-*-import sysimport nmap # import nmap. py moduletry: nm = nmap. portmap () # instantiate nmap. portScanner objectexcept nmap. portScannerError: print ('nmap not found ', sys. exc_info () [0]) sys. exit (0) failed T: print ("Unexpected error:", sys. exc_info () [0]) sys. exit (0) nm. scan ('2017. 0.0.1 ', '22-443') # scan host 127.0.0.1, ports from 22 to 443nm. command_line () # get Command line used for the scan: nmap-oX--p 22-443 127.0.0.1nm.scaninfo () # get nmap scan informations {'tcp ': {'service': '22-443 ′, 'method': 'connect'} nm. all_hosts () # get all hosts that were scannednm ['2017. 0.0.1 ']. hostname () # get hostname for host 127.0.0.1nm ['2017. 0.0.1 ']. state () # get state of host 127.0.0.1 (up | down | unknown | skipped) nm ['2017. 0.0.1 ']. all_protocols () # get all scanned prot Ocols ['tcp ', 'udp'] in (ip | tcp | udp | sctp) nm ['2017. 0.0.1 '] ['tcp']. keys () # get all ports for tcp protocolnm ['2017. 0.0.1 ']. all_tcp () # get all ports for tcp protocol (sorted version) nm ['2017. 0.0.1 ']. all_udp () # get all ports for udp protocol (sorted version) nm ['2017. 0.0.1 ']. all_ip () # get all ports for ip protocol (sorted version) nm ['2017. 0.0.1 ']. all_sctp () # get all ports for sctp protocol (sorted vers Ion) nm ['2014. 127. 0.0.1 ']. has_tcp (22) # is there any information for port 22/tcp on host 127.0.0.1nm ['2017. 0.0.1 '] ['tcp'] [22] # get infos about port 22 in tcp on host 127.0.0.1nm ['2017. 0.0.1 ']. tcp (22) # get infos about port 22 in tcp on host 127.0.0.1nm ['20180101. 0.0.1 '] ['tcp'] [22] ['state'] # get state of port 22/tcp on host 127.0.0.1 (open # a more usefull example: 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: % s \ tstate: % s' % (port, nm [host] [proto] [port] ['state']) print ('----------------') # If you want to do a pingsweep on network 192.168. 1.0/24: nm. scan (hosts = '2017. 168.1.0/24 ', arguments ='-n-sP-PE-PA21, 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 PortScannerAsyncnma = nmap. portScannerAsync () def callback_result (host, scan_result): print '------ 'print host, scan_resultnma.scan (h Osts = '2017. 168.1.0/30', 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 to see the running effect: [root @ centos6 nmap] # python example. py ------------------ Host: 127.0.0.1 (localhost) State: up ---- Protocol: tcpport: 22 state: openport: 25 state: openport: 80 state: open ---------------- 192.168.1.0: down192.168.1.1: down192.168.1.10: down192.168.1.100: down... 192.168.1.159: down192.168.1.16: down192.168.1.160: down192.168.1.161: bandwidth: down192.168.1.163: down192.168.1.164: down192.168.1.165: down192.168.1.166: down192.168.1.167: down192.168.1.168: down192.168.1.169: down... 192.168.1.97: down192.168.1.98: down192.168.1.99: down ---------------- Waiting... ------ 192.168.1.0 {'nmap': {'scanstats': {'uphosts': u'0', 'timestr': u'mon Nov 14 17:25:27 2011 ', 'downhosts ': u'1', 'totalhosts': u'1', 'elapsed ': u'1. 24'}, 'scaninfo' :{}, 'COMMAND _ line': u'nmap-oX--sP 192.168.1.0 '}, 'scan': {u'192. 168.1.0 ': {'status': {'state': u'down', 'reason ': u'host-unreach'}, 'hostname': "}}} Waiting... ------ 192.168.1.1 {'nmap': {'scanstats': {'uphosts': u'0', 'timestr': u'mon Nov 14 17:25:28 2011 ', 'downhosts ': u'1', 'totalhosts': u'1', 'elapsed ': u'1. 23'}, 'scaninfo' :{}, 'COMMAND _ line': u'nmap-oX--sP 192.168.1.1 '}, 'scan': {u'192. 168.1.1 ': {'status': {'state': u'down', 'reason ': u'host-unreach'}, 'hostname': "}}} Waiting... ------ 192.168.1.2 {'nmap': {'scanstats': {'uphosts': u'0', 'timestr': u'mon Nov 14 17:25:29 2011 ', 'downhosts ': u'1', 'totalhosts': u'1', 'elapsed ': u'1. 23'}, 'scaninfo' :{}, 'COMMAND _ line': u'nmap-oX--sP 192.168.1.2 '}, 'scan': {u'192. 168.1.2 ': {'status': {'state': u'down', 'reason ': u'host-unreach'}, 'hostname ': "}}------ 192.168.1.3 {'nmap': {'scanstats': {'uphosts': u'0', 'timestr': u'mon Nov 14 17:25:31 2011 ', 'downhosts': u'1', 'totalhosts': u'1', 'elapsed': u'1. 23'}, 'scaninfo' :{}, 'COMMAND _ line': u'nmap-oX--sP 192.168.1.3 '}, 'scan': {u'192. 168.1.3 ': {'status': {'state': u'down', 'reason ': u'host-unreach'}, 'hostname ': "}}} you can practice other functions on your own. to install this module, you must first install the nmap software...