#coding=utf-8 import nmap
import optparse
import threading
import sys
import re
'''
需安装python_nmap包,支持2.x以及3.x
python_nmap包提供了python调用nmap的一系列接口 (一)重要类及方法:
1.创建nmap扫描器
class PortScanner()
__init__(self, nmap_search_path=('nmap', '/usr/bin/nmap', '/usr/local/bin/nmap', '/sw/bin/nmap', '/opt/local/bin/nmap'))
Initialize PortScanner module * detects nmap on the system and nmap version
* may raise PortScannerError exception if nmap is not found in the path :param nmap_search_path: tupple of string where to search for nmap executable. Change this if you want to use a specific version of nmap.
:returns: nothing
2.扫描器方法
scan(self, hosts='127.0.0.1', ports=None, arguments='-sV', sudo=False)
Scan given hosts May raise PortScannerError exception if nmap output was not xml Test existance of the following key to know if something went wrong : ['nmap']['scaninfo']['error']
If not present, everything was ok. :param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
:param ports: string for ports as nmap use it '22,53,110,143-4564'
:param arguments: string of arguments for nmap '-sU -sX -sC'
:param sudo: launch nmap with sudo if True :returns: scan_result as dictionnary (二)例子
import nmap
scanner = nmap.PortScanner() #nmap_search_path已包含了nmap所在路径,若默认路径中没有nmap,则需指出
results = scanner.scan(hosts='192.168.2.1',ports='80')
pprint.pprint(results)
{'nmap': {'command_line': 'nmap -oX - -p 80 -sV 192.168.2.1',
'scaninfo': {'tcp': {'method': 'syn', 'services': '80'}},
'scanstats': {'downhosts': '0',
'elapsed': '11.59',
'timestr': 'Thu Jul 21 10:08:34 2016',
'totalhosts': '1',
'uphosts': '1'}},
'scan': {'192.168.2.1': {'addresses': {'ipv4': '192.168.2.1',
'mac': 'D0:C7:C0:6A:F6:A0'},
'hostnames': [],
'status': {'reason': 'arp-response',
'state': 'up'},
'tcp': {80: {'conf': '3',
'cpe': '',
'extrainfo': '',
'name': 'http',
'product': '',
'reason': 'no-response',
'state': 'filtered',
'version': ''}},
'vendor': {'D0:C7:C0:6A:F6:A0': 'Tp-link '
'Technologies'}}}} '''
def anlyze_port(target_port):
#解析-p参数传入的值,返回端口列表
try:
pattern = re.compile(r'(\d+)-(\d+)') #解析连接符-模式
match = pattern.match(target_port)
if match:
start_port = int(match.group(1))
end_port = int(match.group(2))
return([x for x in range(start_port,end_port + 1)])
else:
return([int(x) for x in target_port.split(',')])
except Exception as err:
print('请注意错误1:',sys.exc_info()[0],err)
print(parser.usage)
exit(0) def portscanner(target_host,target_port):
scanner = nmap.PortScanner()
results = scanner.scan(hosts=target_host,ports=target_port,arguments='-T4 -A -v -Pn ') #禁ping的快速扫描
print('扫描语句是:',results['nmap']['command_line'])
print('[*]主机' + target_host + '的' + str(target_port) + '端口状态为:' + results['scan'][target_host]['tcp'][int(target_port)]['state']) def main():
usage = 'Usage:%prog --host <target_host> --port <target_port>'
parser = optparse.OptionParser(usage,version='v1.0')
parser.add_option('--host',dest='target_host',type='string',
help='需要扫描的主机,域名或IP')
parser.add_option('--port',dest='target_port',type='string',
help='需要扫描的主机端口,支持1-100或21,53,80两种形式')
(options,args) = parser.parse_args()
if options.target_host == None or options.target_port == None:
print(parser.usage)
exit(0)
else:
target_host = options.target_host
target_port = options.target_port target_port = anlyze_port(target_port)
for port in target_port:
t = threading.Thread(target=portscanner,args=(target_host,str(port)))
t.start() if __name__ == '__main__':
main()
运行后的结果为:
c:\python34\python.exe NmapScanner.py --host 192.168.1.1 --port 80
扫描语句是: nmap -oX - -p 80 -T4 -A -v -Pn 192.168.1.1
[*]主机192.168.1.1的80端口状态为:filtered