zz775520666 发表于 2018-8-13 06:57:34

简单介绍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())
  
    sys.exit(0)
  
except:
  
    print(“Unexpected error:”, sys.exc_info())
  
    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’]          # 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’][‘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.hostname()))
  
    print(‘State : %s’ % nm.state())
  
    for proto in nm.all_protocols():
  
      print(‘———-‘)
  
      print(‘Protocol : %s’ % proto)
  
      lport = nm.keys()
  
      lport.sort()
  
      for port in lport:
  
            print(‘port : %
s\tstate : %s’ % (port, nm[‘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[‘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
  下面看下运行的效果:
  # 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这个软件是必须条件。。。
页: [1]
查看完整版本: 简单介绍python-nmap 模块的使用