簡單介紹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 sys
import nmap                         # import nmap.py module
try:
    nm = nmap.PortScanner()         # instantiate nmap.PortScanner object
except 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 443
nm.command_line()                   # get command line used for the scan : nmap -oX – -p 22-443 127.0.0.1
nm.scaninfo()                       # get nmap scan informations {‘tcp': {‘services': ’22-443′, ‘method': ‘connect’}}
nm.all_hosts()                      # get all hosts that were scanned
nm[‘127.0.0.1′].hostname()          # get hostname for host 127.0.0.1
nm[‘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 protocol
nm[‘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.1
nm[‘127.0.0.1′][‘tcp’][22]          # get infos about port 22 in tcp on host 127.0.0.1
nm[‘127.0.0.1′].tcp(22)             # get infos about port 22 in tcp on host 127.0.0.1
nm[‘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 PortScannerAsync
nma = nmap.PortScannerAsync()
def callback_result(host, scan_result):
    print ‘——————’
    print host, scan_result
nma.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 : tcp

port : 22       state : open

port : 25       state : open

port : 80       state : open

—————————————————-

192.168.1.0:down

192.168.1.1:down

192.168.1.10:down

192.168.1.100:down

。。。

192.168.1.159:down

192.168.1.16:down

192.168.1.160:down

192.168.1.161:down

192.168.1.162:down

192.168.1.163:down

192.168.1.164:down

192.168.1.165:down

192.168.1.166:down

192.168.1.167:down

192.168.1.168:down

192.168.1.169:down

。。。

192.168.1.97:down

192.168.1.98:down

192.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這個軟件是必須條件。。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章