对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
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文件解析的更多相关文章

  1. Adobe AIR对本地文件(XML文件)的操作

    引用:http://addiwang.blog.163.com/blog/static/118130772011221114230288/ Air的文件操做主要涉及两个类,FIle和FileStrea ...

  2. JAVA使用SAX解析XML文件

    在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...

  3. 利用oxygen编辑并生成xml文件,并使用JAVA的JAXB技术完成xml的解析

    首先下载oxygen软件(Oxygen XML Editor),目前使用的是试用版(可以安装好软件以后get trial licence,获得免费使用30天的权限,当然这里鼓励大家用正版软件!!!) ...

  4. 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点

    使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...

  5. python 常用包之xml文件处理

    1,处理xml的包 from xml.etree import ElementTree as ET 2,如何写出xml文件 xml文件和html中的元素很像,有父级子集之说, root = ET.El ...

  6. xml文件读取到数据库

    xml文件读取到数据库   第一步,导包 c3p0,dom4j,jaxen,MySQL-connector 第二步  xml文件,config文件 第三步 javabean 第四步 c3p0的工具类 ...

  7. python读写xml文件

    python读取xml文件 xml文件是具有树状结构的,如果想要访问某个叶子结点,必须逐层获取其父结点,要读取某个叶子结点内容用text成员 使用前先加载xml工具包 try: import xml. ...

  8. Spring-Batch将CSV文件转为XML文件

    1 介绍 用Spring Batch实现一个简单的需求,将csv文件转换成xml文件. csv文件如下:record.csv username, user_id, transaction_date, ...

  9. Android 解析XML文件和生成XML文件

    解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...

随机推荐

  1. OpenJudge Cartesian Tree

    [代码] #include <cstdio> #include <cstdlib> #include <cstring> #include <algorith ...

  2. 猴哥来了-游戏开发记录17-微信排行榜bug

    上线后排行榜bug 1.排序算法 const dataSorter = (gameDatas, field = Consts.OpenDataKeys.LevelKey) => {  let d ...

  3. 第1章 计算机网络和协议(2)_OSI参考模型

    2. OSI参考模型 2.1 OSI参考模型详解 (1)参考模型的优点 ①将网络的通信过程划分为小一些.功能简单的部件,有助于各个部件开发.设计和故障排除. ②通过网络组件的标准化,允许多个供应商进行 ...

  4. (转)C# 控制蜂鸣器发声

    原文地址:http://blog.csdn.net/tsinfeng/article/details/6201918 在C#中可以通过以下四种方式来实现蜂鸣或者报警,播放声音之类的功能.XP下对蜂鸣有 ...

  5. async 常用函数总结

    待更新. waterfall auto(神器) parallel mapSeries(数据库多条记录操作神器)

  6. 在mvc中动态加载菜单

    最近做了一个项目, 要在客户端动态的显示菜单,也就是这些菜单是保存在数据库中的, 在客户端动态加载菜单,这样做的好处很明显,就是菜单很容易修改,直接在后台进行维护,再也不会直接在前面的 视图页面中进行 ...

  7. springboot工程的添加方式

    1.将spring boot做为父工程 <parent> <groupId>org.springframework.boot</groupId> <artif ...

  8. [转]ORA-12560: TNS: 协议适配器错误

    转自:http://worms.blog.51cto.com/969144/1293265 Sqlplus 登陆oracle时报错ORA-12560:TNS: 协议适配器错误 如下:C:\Users\ ...

  9. 8.2.1.2-MySQL如何优化 WHERE 语句

    这一章节讨论能够在WHERE处理语句中使用的优化. 样例使用SELECT 语句, 但是同样适用于DELETE,UPDATE语句中的WHERE语句. 注意 因为MYSQL优化器在不断的发展,MySQL执 ...

  10. uva-317-找规律

    无耻的抄袭了结果,三组数,从每一组数中选取一个数组成正六边形的对边,总共会有27个正六边形,从27个小六边形中选取19个组成大六边形,求大六边形的最大值 #include<iostream> ...