Nmap输出格式过滤小工具

发表于:2015-4-23 11:08

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:老李的博客    来源:51Testing软件测试网采编

  想用nmap 扫描ip段输出一个干净的 IP:PORT格式的文件。于是写了个简单脚本。
import xml.dom.minidom
import sys
import getopt
statstr=""
statstrlist=["open"]
infile=""
outfile=""
infileflag=0
outfileflag=0
statstrflag=0
isstatflag=0
helpflag=0
#chu li can shu
options,args = getopt.getopt(sys.argv[1:],"hni:o:s:",["help","in=","out=","stat"])
for name,value in options:
if name in ("-h","--help"):
print """
xxoo.py
-h,--help print this help
-i,--in read the nmap xml outfile
-o,--out out the result (default: xmlname+_done.txt)
-s,--stat splited by dot (example:open.filtered.close default:open)
-n no status info
"""
helpflag=1
if name in ("-i","--in"):
infile=value
infileflag=1
if name in ("-o","--out"):
outfile=value
outfileflag=1
if name in ("-s","--stat"):
statstrlist=value.split(".")
statflag=1
if name=="-n":
isstatflag=1
#chu li mo ren can canshu
if outfileflag==0:
outfile=infile+"_done.txt"
if statstrflag==0:
statstrlist=["open"]
if helpflag==1:
exit()
#chu li dom
file=open(outfile,"w+")
dom=xml.dom.minidom.parse(infile)
root=dom.documentElement
hostlist=root.getElementsByTagName("host")
for hostnode in hostlist:
for hostchild in hostnode.childNodes:
if hostchild.nodeName=="address":
ip=hostchild.getAttribute("addr")
if hostchild.nodeName=="ports":
for hostchildchild in hostchild.childNodes:
if hostchildchild.nodeName=="port":
for stat in statstrlist:
if hostchildchild.childNodes[0].getAttribute("state")==stat:
if isstatflag==0:
print ip+":"+hostchildchild.getAttribute("portid")+"(("+stat+"))"
file.write(ip+":"+hostchildchild.getAttribute("portid")+"(("+stat+"))"+"\n")
if isstatflag==1:
print ip+":"+hostchildchild.getAttribute("portid")
file.write(ip+":"+hostchildchild.getAttribute("portid")+"\n")
#if hostchildchild.childNodes[0].getAttribute("state")=="open":
#print ip+":"+hostchildchild.getAttribute("portid")+"((open))"
# file.write(ip+":"+hostchildchild.getAttribute("portid")+"((open))"+"\n")
#elif hostchildchild.childNodes[0].getAttribute("state")=="filtered":
# print ip+":"+hostchildchild.getAttribute("portid")+"((filtered))"
#  file.write(ip+":"+hostchildchild.getAttribute("portid")+"((filtered))"+"\n")
file.close()
  用法:
  先用nmap –oX 参数输出xml格式文件。然后tool –i name.xml –s open.filtered
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号