soarli

使用Python提取Nmap特定状态IP地址
说明1.0版本5月18日下午,考虑到Nmap输出数据分析汇总的困难性,尝试使用Python自动汇总指定状态的ip地...
扫描右侧二维码阅读全文
18
2021/05

使用Python提取Nmap特定状态IP地址

说明

1.0版本

5月18日下午,考虑到Nmap输出数据分析汇总的困难性,尝试使用Python自动汇总指定状态的ip地址集合。由于一个细微的逻辑疏忽,导致折腾了半小时左右,问题已经得以解决。

2.0版本

5月18日下午,考虑到1.0版本存在:

  1. 每一次切换文件都需要修改源代码;
  2. 仅分析了open和filtered状态(没有分析closed);
  3. 输出格式为['xxx.xxx.xxx.xxx'](不直观)

问题,2.0版本得以诞生(结果默认输出到控制台,命令结果输出到文件需使用>指令)。

i:nmap输出原始文件完整路径;
o:依次输出端口状态openfilteredclosed的ip地址集合。

3.0版本

5月20日下午,针对80端口扫描结果批量打开的需求,尝试按照chrome书签栏导出格式自动生成导出文件,直接导入即可借助chrome实现批量打开。这次总体还算顺利,从产生需求到解决问题用时不到20分钟。

i:nmap输出原始文件完整路径;
o:依次输出端口状态openfilteredclosed的文件集合(open.html,filtered.html,closed.html)。
命令窗口对应输出“成功输出端口状态为open书签文件。”等。

3.1版本

5月21日晚上,将3.0版本功能打包成了一个可执行程序:点我下载

效果:

代码

1.0版本

import os
import re

f1 = open('D:/283389.txt',"r")
f2 = open('D:/283389.txt',"r")
line = f1.readline()
subline = 'open'
subline2 = 'filtered'
#line.replace(' ', '')
print('端口状态为open的ip地址:')
while line:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
    if (flag!=[]):
        ip = flag
    if subline in line:
        print(ip)
        #print(line)
    line = f1.readline()
f1.close()

line2 = f2.readline()
print('端口状态为filtered的ip地址:')
while line2:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line2)
    if (flag!=[]):
        ip = flag
    if subline2 in line2:
        print(ip)
        #print(line)
    line2 = f2.readline()
    
f2.close()

2.0版本

import os
import re

filenmae = input("请输入nmap输出文件目录:")
print('')
f1 = open(filenmae,"r")
f2 = open(filenmae,"r")
f3 = open(filenmae,"r")
line = f1.readline()
subline = 'open'
subline2 = 'filtered'
subline3 = 'closed'
#line.replace(' ', '')
print('端口状态为open的ip地址:')
while line:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
    if (flag!=[]):
        ip = flag
    if subline in line:
        print(ip[0])
        #print(line)
    line = f1.readline()
f1.close()

line2 = f2.readline()
print('端口状态为filtered的ip地址:')
while line2:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line2)
    if (flag!=[]):
        ip = flag
    if subline2 in line2:
        print(ip[0])
        #print(line)
    line2 = f2.readline()

line3 = f3.readline()
print('端口状态为closed的ip地址:')
while line3:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line3)
    if (flag!=[]):
        ip = flag
    if subline3 in line3:
        print(ip[0])
        #print(line)
    line3 = f3.readline()
f3.close()

3.0版本

import os
import re

filenmae = input("请输入nmap输出文件目录:")
print('')
f1 = open(filenmae,"r")
f2 = open(filenmae,"r")
f3 = open(filenmae,"r")
sq_open = open('open.html','w')
sq_filtered = open('filtered.html','w')
sq_closed = open('closed.html','w')
print('<!DOCTYPE NETSCAPE-Bookmark-file-1>',file=sq_open)
print('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',file=sq_open)
print('<TITLE>Bookmarks</TITLE>',file=sq_open)
print('<H1>Bookmarks</H1>',file=sq_open)
print('<DL><p>',file=sq_open)
print('<!DOCTYPE NETSCAPE-Bookmark-file-1>',file=sq_filtered)
print('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',file=sq_filtered)
print('<TITLE>Bookmarks</TITLE>',file=sq_filtered)
print('<H1>Bookmarks</H1>',file=sq_filtered)
print('<DL><p>',file=sq_filtered)
print('<!DOCTYPE NETSCAPE-Bookmark-file-1>',file=sq_closed)
print('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',file=sq_closed)
print('<TITLE>Bookmarks</TITLE>',file=sq_closed)
print('<H1>Bookmarks</H1>',file=sq_closed)
print('<DL><p>',file=sq_closed)
line = f1.readline()
subline = 'open'
subline2 = 'filtered'
subline3 = 'closed'
#line.replace(' ', '')
while line:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
    if (flag!=[]):
        ip = flag
    if subline in line:
        print("    <DT><A HREF=" + '"' + "http://" + ip[0] + '"' + "></A>",file=sq_open)
    line = f1.readline()
print('</DL><p>',file=sq_open)
f1.close()
sq_open.close()
print('成功输出端口状态为open书签文件。')

line2 = f2.readline()
while line2:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line2)
    if (flag!=[]):
        ip = flag
    if subline2 in line2:
        print("    <DT><A HREF=" + '"' + "http://" + ip[0] + '"' + "></A>",file=sq_filtered)
    line2 = f2.readline()
print('</DL><p>',file=sq_filtered)
f2.close()
sq_filtered.close()
print('成功输出端口状态为filtered书签文件。')

line3 = f3.readline()
while line3:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line3)
    if (flag!=[]):
        ip = flag
    if subline3 in line3:
        print("    <DT><A HREF=" + '"' + "http://" + ip[0] + '"' + "></A>",file=sq_closed)
    line3 = f3.readline()
print('</DL><p>',file=sq_closed)
f3.close()
sq_closed.close()
print('成功输出端口状态为closed书签文件。')

3.1版本

import os
import re
import time

def countdown(t):
    while t > 0:
        print("本程序将于" + str(t) + "秒后自动关闭!", end="\r")
        t -= 1
        time.sleep(1)

def success():
    print("\n")
    print("************************************************************")
    print("******程序执行完毕,请留意当前目录生成的三个html文件!******")
    print("************************************************************")
    print("\n")
    
filenmae = input("请输入nmap输出文件目录:")
print('')
f1 = open(filenmae,"r")
f2 = open(filenmae,"r")
f3 = open(filenmae,"r")
sq_open = open('open.html','w')
sq_filtered = open('filtered.html','w')
sq_closed = open('closed.html','w')
print('<!DOCTYPE NETSCAPE-Bookmark-file-1>',file=sq_open)
print('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',file=sq_open)
print('<TITLE>Bookmarks</TITLE>',file=sq_open)
print('<H1>Bookmarks</H1>',file=sq_open)
print('<DL><p>',file=sq_open)
print('<!DOCTYPE NETSCAPE-Bookmark-file-1>',file=sq_filtered)
print('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',file=sq_filtered)
print('<TITLE>Bookmarks</TITLE>',file=sq_filtered)
print('<H1>Bookmarks</H1>',file=sq_filtered)
print('<DL><p>',file=sq_filtered)
print('<!DOCTYPE NETSCAPE-Bookmark-file-1>',file=sq_closed)
print('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">',file=sq_closed)
print('<TITLE>Bookmarks</TITLE>',file=sq_closed)
print('<H1>Bookmarks</H1>',file=sq_closed)
print('<DL><p>',file=sq_closed)
line = f1.readline()
subline = 'open'
subline2 = 'filtered'
subline3 = 'closed'
#line.replace(' ', '')
while line:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
    if (flag!=[]):
        ip = flag
    if subline in line:
        print("    <DT><A HREF=" + '"' + "http://" + ip[0] + '"' + "></A>",file=sq_open)
    line = f1.readline()
print('</DL><p>',file=sq_open)
f1.close()
sq_open.close()
print('【状态】:成功输出端口状态为open书签文件。')

line2 = f2.readline()
while line2:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line2)
    if (flag!=[]):
        ip = flag
    if subline2 in line2:
        print("    <DT><A HREF=" + '"' + "http://" + ip[0] + '"' + "></A>",file=sq_filtered)
    line2 = f2.readline()
print('</DL><p>',file=sq_filtered)
f2.close()
sq_filtered.close()
print('【状态】:成功输出端口状态为filtered书签文件。')

line3 = f3.readline()
while line3:
    flag = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line3)
    if (flag!=[]):
        ip = flag
    if subline3 in line3:
        print("    <DT><A HREF=" + '"' + "http://" + ip[0] + '"' + "></A>",file=sq_closed)
    line3 = f3.readline()
print('</DL><p>',file=sq_closed)
f3.close()
sq_closed.close()
print('【状态】:成功输出端口状态为closed书签文件。')

success()
countdown(5)

参考资料:

https://www.runoob.com/python/python-reg-expressions.html

https://www.runoob.com/python/python-reg-expressions.html#flags

https://blog.csdn.net/weixin_30437481/article/details/101540482

https://www.runoob.com/python/python-lists.html

http://c.biancheng.net/view/2546.html

https://blog.csdn.net/zhengxiangwen/article/details/55148287

https://blog.csdn.net/quiet_girl/article/details/80113591

https://blog.csdn.net/haoaiqian/article/details/70228177

http://www.mzh.ren/python-5-ways-to-check-string-contains-substring.html

https://jingyan.baidu.com/article/acf728fd7614b4f8e510a3bc.html

http://www.yaoyan.me/2019/12/python-print/

https://yujunjiex.gitee.io/2018/10/18/PyInstaller%E6%89%93%E5%8C%85%E8%AF%A6%E8%A7%A3/

https://www.zhihu.com/question/20390166

https://cloud.tencent.com/developer/ask/51762

https://meishizui.com/1589

https://blog.csdn.net/huilan_same/article/details/54377919

最后修改:2022 年 01 月 07 日 05 : 19 PM

发表评论