簡單介紹python-nmap 模組的使用
python-nmap是python的一個模組庫,使用這個模組可以讓python很方便的操作nmap掃描器來工作,它可以協助管理員完成自動掃描任務和產生報告的工具,它還支援nmap的腳步輸出。最新的版本是python-nmap-0.2.4.tar.gz,是:http://xael.org/norman/python/python-nmap/python-nmap-0.2.4.tar.gz不過這個版本是適合python3.*來使用的,如果你的python版本還是2.*的話,還是使用這個版本,python- nmap.0.1.4.tar.gz, 下載連結是http://xael.org/norman/python/python-nmap/python-nmap-0.1.4.tar.gz安裝還是很簡單的,解壓縮,運行setup.py install之後,就搞定了。 下面貼出內建的example.py的源碼: #!/usr/bin/env python# -*- coding: latin-1 -*-import sysimport nmap # import nmap.py moduletry: nm = nmap.PortScanner() # instantiate nmap.PortScanner objectexcept nmap.PortScannerError: print(‘Nmap not found’, sys.exc_info()[0]) sys.exit(0)except: print(“Unexpected error:”, sys.exc_info()[0]) sys.exit(0)nm.scan(‘127.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': {‘services': ’22-443′, ‘method': ‘connect’}}nm.all_hosts() # get all hosts that were scannednm[‘127.0.0.1′].hostname() # get hostname for host 127.0.0.1nm[‘127.0.0.1′].state() # get state of host 127.0.0.1 (up|down|unknown|skipped)nm[‘127.0.0.1′].all_protocols() # get all scanned protocols [‘tcp’, ‘udp’] in (ip|tcp|udp|sctp)nm[‘127.0.0.1′][‘tcp’].keys() # get all ports for tcp protocolnm[‘127.0.0.1′].all_tcp() # get all ports for tcp protocol (sorted version)nm[‘127.0.0.1′].all_udp() # get all ports for udp protocol (sorted version)nm[‘127.0.0.1′].all_ip() # get all ports for ip protocol (sorted version) nm[‘127.0.0.1′].all_sctp() # get all ports for sctp protocol (sorted version)nm[‘127.0.0.1′].has_tcp(22) # is there any information for port 22/tcp on host 127.0.0.1nm[‘127.0.0.1′][‘tcp’][22] # get infos about port 22 in tcp on host 127.0.0.1nm[‘127.0.0.1′].tcp(22) # get infos about port 22 in tcp on host 127.0.0.1nm[‘127.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=’192.168.1.0/24′, 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 PortScannerAsyncnma = nmap.PortScannerAsync()def callback_result(host, scan_result): print ‘——————’ print host, scan_resultnma.scan(hosts=’192.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下面看下啟動並執行效果:[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:down192.168.1.162: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': ”}}} 其他功能大家可以自己實踐,安裝這個模組,首先系統必須要安裝好nmap這個軟體是必須條件。。。