如何使用nmap输出格式过滤工具进行数据筛选?

2026-06-09 09:324阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计583个文字,预计阅读时间需要3分钟。

如何使用nmap输出格式过滤工具进行数据筛选?

pythonimport xml.dom.minidomimport sysimport getopt

def scan_ip_segment(ip_segment, output_file): try: # 执行nmap扫描 nmap_command=fnmap -sP {ip_segment} > {output_file} os.system(nmap_command)

# 解析nmap输出文件 dom=xml.dom.minidom.parse(output_file) root=dom.documentElement

# 查找所有主机 hosts=root.getElementsByTagName(host) for host in hosts: # 获取IP地址 ip=host.getElementsByTagName(address)[0].getAttribute(addr) print(fIP: {ip})

如何使用nmap输出格式过滤工具进行数据筛选?

# 查找所有端口 ports=host.getElementsByTagName(port) for port in ports: # 获取端口状态 state=port.getElementsByTagName(state)[0].getAttribute(state) portno=port.getAttribute(portid) print(fPORT {portno}: {state})

except Exception as e: print(fError: {e})

if __name__==__main__: # 解析命令行参数 try: opts, args=getopt.getopt(sys.argv[1:], hi:o:, [help, ip=, output=]) except getopt.GetoptError: print(Usage: python script.py -i -o ) sys.exit(2)

for opt, arg in opts: if opt in (-h, --help): print(Usage: python script.py -i -o ) sys.exit() elif opt in (-i, --ip): ip_segment=arg elif opt in (-o, --output): output_file=arg else: print(Invalid option) sys.exit()

if not ip_segment or not output_file: print(Please provide both IP segment and output file.) sys.exit()

scan_ip_segment(ip_segment, output_file)

想用nmap扫描ip段输出一个干净的IP:PORT格式的文件。于是写了个简单脚本。importxml.dom.minidomimportsysimportgetoptstatstr

想用nmap 扫描ip段输出一个干净的 IP:PORT格式的文件。于是写了个简单脚本。

import xml.dom.minidomimport sysimport getoptstatstr=""statstrlist=["open"]infile=""outfile=""infileflag=0outfileflag=0statstrflag=0isstatflag=0helpflag=0#chu li can shuoptions,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 domfile=open(outfile,"w+")dom=xml.dom.minidom.parse(infile)root=dom.documentElementhostlist=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

nmap 输出格式过滤小工具,,

nmap 输出格式过滤小工具

本文共计583个文字,预计阅读时间需要3分钟。

如何使用nmap输出格式过滤工具进行数据筛选?

pythonimport xml.dom.minidomimport sysimport getopt

def scan_ip_segment(ip_segment, output_file): try: # 执行nmap扫描 nmap_command=fnmap -sP {ip_segment} > {output_file} os.system(nmap_command)

# 解析nmap输出文件 dom=xml.dom.minidom.parse(output_file) root=dom.documentElement

# 查找所有主机 hosts=root.getElementsByTagName(host) for host in hosts: # 获取IP地址 ip=host.getElementsByTagName(address)[0].getAttribute(addr) print(fIP: {ip})

如何使用nmap输出格式过滤工具进行数据筛选?

# 查找所有端口 ports=host.getElementsByTagName(port) for port in ports: # 获取端口状态 state=port.getElementsByTagName(state)[0].getAttribute(state) portno=port.getAttribute(portid) print(fPORT {portno}: {state})

except Exception as e: print(fError: {e})

if __name__==__main__: # 解析命令行参数 try: opts, args=getopt.getopt(sys.argv[1:], hi:o:, [help, ip=, output=]) except getopt.GetoptError: print(Usage: python script.py -i -o ) sys.exit(2)

for opt, arg in opts: if opt in (-h, --help): print(Usage: python script.py -i -o ) sys.exit() elif opt in (-i, --ip): ip_segment=arg elif opt in (-o, --output): output_file=arg else: print(Invalid option) sys.exit()

if not ip_segment or not output_file: print(Please provide both IP segment and output file.) sys.exit()

scan_ip_segment(ip_segment, output_file)

想用nmap扫描ip段输出一个干净的IP:PORT格式的文件。于是写了个简单脚本。importxml.dom.minidomimportsysimportgetoptstatstr

想用nmap 扫描ip段输出一个干净的 IP:PORT格式的文件。于是写了个简单脚本。

import xml.dom.minidomimport sysimport getoptstatstr=""statstrlist=["open"]infile=""outfile=""infileflag=0outfileflag=0statstrflag=0isstatflag=0helpflag=0#chu li can shuoptions,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 domfile=open(outfile,"w+")dom=xml.dom.minidom.parse(infile)root=dom.documentElementhostlist=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

nmap 输出格式过滤小工具,,

nmap 输出格式过滤小工具