當目標資產很多時,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))
運行結果截圖
結果文件
使用scrapy shell功能時報錯,可嘗試使用下面命令
scrapy shell -s USER_AGENT='custom user agent' visit_url