Nmap结果文件XML文件解析
对nmap扫描结果xml格式的文件进行解析,无需直接xml解析或读取,可直接使用模块:
1.nmapparser
安装:pip install nmapparser
Demo:
#!/usr/bin/env python
# Copyright (C) 2007 Guilherme Polo <ggpolo@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
"""
A demo script showing how to use nmapparser.
""" import sys
import re
from nmapparser import NmapParser def usage():
"""Show demo usage."""
print "Usage: %s xmlfile1.xml xmlfile2.xml ..." % __file__ def getaddress(str):
reg=u"addr': '(.*?)'}"
lister=re.compile(reg)
mylist=re.findall(lister,str)
#print mylist
return mylist[0] def main(args):
parser = NmapParser()
for xmlf in sys.argv[1:]:
print "%s\nParsing %s" % ('*' * 75, xmlf)
parser.parse(xmlf) if not parser.parsed:
continue print "Options:", parser.options
print "Finish time:", parser.runstats.finished.time h_stats = parser.runstats.hosts
print "Hosts -> total %s, up: %s, down: %s" % (
h_stats.total, h_stats.up, h_stats.down) for host in parser.host:
print "Host options:", host.options if 'extraports' in host.options:
print "Host extraports:", host.ports.extraports print "Hostname:", host.hostnames
print "HostIp:", getaddress(str(host.address)) if 'ports' not in host.options or \
'ports' not in host.ports.options:
continue if 'script' in host.ports.ports[0].options:
print host.ports.ports[0].script[0].output
print print "Host ports info:"
for p in host.ports.ports:
print "%20s%7s%9s%6s" % (getaddress(str(host.address)),p.portid, p.state, p.protocol) if __name__ == "__main__":
if len(sys.argv) < 2:
sys.exit(usage())
main(sys.argv)
解析:
demo.py *.xml
结果输出:

2.python-libnmap
安装:
pip install python-libnmap //copyright AnYun.ORG
Demo:
#!/usr/bin/env python import argparse
from libnmap.process import NmapProcess
from libnmap.parser import NmapParser, NmapParserException def parse_args():
''' Create the arguments '''
parser = argparse.ArgumentParser()
parser.add_argument("-x", "--nmapxml", help="Nmap XML file to parse")
parser.add_argument("-l", "--hostlist", help="Host list file")
return parser.parse_args() def report_parser(report):
''' Parse the Nmap XML report '''
for host in report.hosts:
ip = host.address if host.is_up():
hostname = 'N/A'
# Get the first hostname (sometimes there can be multi)
if len(host.hostnames) != 0:
hostname = host.hostnames[0] print '[*] {0} - {1}'.format(ip, hostname) # Get the port and service
# objects in host.services are NmapService objects
for s in host.services: # Check if port is open
if s.open():
serv = s.service
port = s.port
ban = s.banner # Perform some action on the data
print_data(ip, port, serv, ban) def print_data(ip, port, serv, ban):
''' Do something with the nmap data '''
if ban != '':
ban = ' -- {0}'.format(ban)
print ip,port,serv,ban #print ' {0}: {1}{2}'.format(port, serv, ban)
#print ' {0}: {1}{2}'.format(port, serv, ban) def main():
args = parse_args()
report = NmapParser.parse_fromfile(args.nmapxml)
report_parser(report) main()
解析:
nmap-parser-dome.py -x *.xml
结果输出:

以上能基本解析了XML文件内容,若有其他需求,可参照修改偷懒
Nmap结果文件XML文件解析的更多相关文章
- Adobe AIR对本地文件(XML文件)的操作
引用:http://addiwang.blog.163.com/blog/static/118130772011221114230288/ Air的文件操做主要涉及两个类,FIle和FileStrea ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- 利用oxygen编辑并生成xml文件,并使用JAVA的JAXB技术完成xml的解析
首先下载oxygen软件(Oxygen XML Editor),目前使用的是试用版(可以安装好软件以后get trial licence,获得免费使用30天的权限,当然这里鼓励大家用正版软件!!!) ...
- 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点
使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...
- python 常用包之xml文件处理
1,处理xml的包 from xml.etree import ElementTree as ET 2,如何写出xml文件 xml文件和html中的元素很像,有父级子集之说, root = ET.El ...
- xml文件读取到数据库
xml文件读取到数据库 第一步,导包 c3p0,dom4j,jaxen,MySQL-connector 第二步 xml文件,config文件 第三步 javabean 第四步 c3p0的工具类 ...
- python读写xml文件
python读取xml文件 xml文件是具有树状结构的,如果想要访问某个叶子结点,必须逐层获取其父结点,要读取某个叶子结点内容用text成员 使用前先加载xml工具包 try: import xml. ...
- Spring-Batch将CSV文件转为XML文件
1 介绍 用Spring Batch实现一个简单的需求,将csv文件转换成xml文件. csv文件如下:record.csv username, user_id, transaction_date, ...
- Android 解析XML文件和生成XML文件
解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...
随机推荐
- delphi打开项目提示unable to find resource on dll projects
用记事本打开*.dof文件, 把这行[Resource DLL Projects]及它的所属的内容删除就行了.
- 微信小程序--swiper组件
<view class='swiper-container'> <swiper indicator-dots="true" autoplay=" ver ...
- JS面试典型常见问题与解答
Q1:下面代码段的输出是什么? 为什么? (Scope) (function() { var a = b = 5; })(); console.log(b); A1: 输出是5. 在上面的立即调用函数 ...
- vue实现未登录跳转到登录页面
环境:vue 2.9.3; webpack;vue-router 目的:实现未登录跳转 例子:直接在url地址栏输入...../home,但是这个页面要求需要登陆之后才能进入,判断的值就通过登陆之后给 ...
- Vue项目,运行出现warning(Emitted value instead of an instance of Error)
组件:<XXXX v-for="item in items" /> warning:(Emitted value instead of an instance of E ...
- LeetCode 3. longest characters & 切片
Longest Substring Without Repeating Characters 找无重复的最长子串 第1次提交 class Solution: def lengthOfLongestSu ...
- 使用wifi ssh: connect to host hadoop000 port 22: No route to host
使用的wifi,在wifi下IP会自动分配. 先尝试能否ping通,如果都无法ping通那先把IP配置那些先搞定ping通之后遇到此问题再来尝试解决. 查看 /etc/hosts 中配置的 IP ...
- bayes公式 - 再从零开始理解
bayes公式与机器学习 - 再从零开始理解 从本科时候(大约9年前)刚接触Bayes公式,只知道P(A|B)×P(B) = P(AB) = P(B|A)×P(A) 到硕士期间,机器学习课上对P(B| ...
- iPhone投影
iPhone投影到Mac上面的操作,用QuickTime,选择主菜单的新建屏幕录制,然后点击录制按钮右边的箭头,相机切换到iPhone就可以了. 相关操作参照 https://jingyan.baid ...
- 11.采集手机端app企查查上司公司数据(未成功)
---恢复内容开始--- 采集企查查手机端app数据: 1.首先手机端安装app并usb连接电脑端,fiddler监控手机请求数据对数据进行分析抓取. 手机端界面与fiddler界面参照: 2.对获取 ...