当目标资产很多时,py-nmap不能满足多网段同时扫描的需求了,对于扫描结果的提取也很痛苦于是便产生了这个工具了~
工具还是有些瑕疵的,但是对于我自己的需求是满足了的。大家使用上有啥问题的话欢迎留言或者简信我^_^.
贴代码:
# coding: utf-8 try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET reload(__import__('sys')).setdefaultencoding('utf-8') ''' get-nmap-ip-port.py nmap -Pn -n -vv -F -oX d:\\xxx.xml 10.129.246.1/24 nmap -n -vv --open -oX d:\\vm.xml 192.168.106.1/24 --open这个参数很重要 ''' # Method 1 bd = open(r'd:\rs1.xml', 'r').read() #nmap的扫描结果文件 root = ET.fromstring(bd) # Method 2 # tree = ET.parse('xxx.xml') # root = tree.getroot() ip_list = [] res = open('res.txt', 'w') for host in root.findall('host'): if len(host) > 3: print '\n', str(len(host)), # 打印host标签中子元素个数 if host[0].get('state') == "up": # 判断IP是否存活 ip = host[1].get('addr') # 提取IP地址 print ip, ip_list.append(ip) #验证存活IP个数 ip_ = '\n'+ip+'\t' res.writelines(ip_) # 提取端口 if len(host) == 6: for port in host[4][1:]: #若确认端口开放,但没有提取出端口请修改host[4][此处+1试试:],加一后为host[4][2:],下面的几处方法一样 print port.get('portid'), port_ = str(port.get('portid')) + ',' res.write(port_) elif len(host) == 5: for port in host[3][2:]: # print port.tag, print port.get('portid'), port_ = str(port.get('portid'))+',' res.write(port_) elif len(host) == 4: for port in host[3][1:]: print port.get('portid'), port_ = str(port.get('portid')) + ',' res.write(port_) elif len(host) < 4: print host[0].get('state') res.close() print '\n Alive IP Total:{} '.format(len(ip_list)) |
运行结果截图
结果文件