有什么方法可以优化/正确完善这个 <50 行脚本吗?
我仍在学习 python,我决定深入研究的第一个项目是对大型 nmap 日志进行排序,拉出开放端口,并将它们转储到 IP:Port 格式的单独文本文件中。它可以工作,但是有更好的方法来写这个吗?这就是我最终得到的结果:
import sys
import string
"""
Written 6/24/2011 to pull out OPEN ports of an nmap proxy scan
Command:
nmap 218.9-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog3.txt
"""
if len(sys.argv) != 3:
print 'Usage: python proxy.py <input file> <output file>'
print 'nmap 218.1-255.0-255.0-255 -p 8080,3128,1080 -M 50 -oG PLog.txt'
print 'Example: python ./proxy.py PLog.txt proxies.txt'
sys.exit(1)
r = open(sys.argv[1], 'r')
o = open(sys.argv[2], 'w')
pat80 = '80/open/'
pat8080 = '8080/open'
pat3128 = '3128/open'
for curline in r.xreadlines():
sift = string.split(curline, ' ')
ip = sift[1]
if curline.find(pat3128) >= 0:
curport = '3128'
elif curline.find(pat8080) >= 0:
curport = '8080'
elif curline.find(pat80) >= 0:
curport = '80'
else:
curport = '100'
pass
if (curport == '3128') or (curport == '8080') or (curport == '80'):
o.write(ip + ':' + curport + '\n')
print ip + ':' + curport
else:
pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以像这样循环遍历文件。无需使用
xreadlines()
。with
确保当r
超出范围时关闭文件在元组中查找比
or
链更整洁You can loop over a file like this. There is no need to use
xreadlines()
.with
makes sure the file is closed whenr
goes out of scopeLooking in a tuple is neater than the chain of
or
因为我似乎记得使用 python 解析 nmap 输出文件是我的第一个 python 应用程序之一,所以我可以提出一些建议:
1) 如果您想学习 XML 解析和 python,建议使用替代 XML 格式 nmap。这样做的优点是,与纯文本输出不同,XML 输出不太可能以小但破坏脚本的方式进行更改。 (基本上,字符串字段的匹配对于快速破解来说非常有用,但几乎肯定会在路上咬你,因为我发现 nmap 更新时,它们稍微改变了我正在解析的列之一的格式......我还认为,当我们升级其中一个 Windows 盒子时,操作系统或服务字段中的一些文本与我匹配的内容相匹配。如果您有兴趣沿着这条路走下去,我可以看看我是否有我的 nmap。使用 xpath 的解析器位于
2) 如果你想要为了坚持文本输出和正则表达式,我建议学习分组。
具体来说,您可以定义一个组并进行检查,而不是为每个端口创建自定义模式。
Since I seem to remember using python to parse nmap output files was one of my first python applications, I can make a couple of recommendations:
1) If you'd like to learn XML parsing and python, using the alternate XML format of nmap would be advised. This has the advantage that the XML output is less like to change in small but script breaking ways unlike the plain text output. (Basically, matching on string fields is great for a quick hack but is almost guaranteed to bite you down the road, as I found out when nmap was updated and they slightly changed the format of one of the columns I was parsing on... also think I got bit when we upgraded one of the Windows boxes and some of the text in the OS or services fields matched something I was matching on. If you're interested in going down this path, I can see if I have my nmap parser using xpath lying around
2) If you want to stick with text output and regexp, I'd suggest learning about grouping.
Specifically, rather than creating custom patterns for each port, you can define a group and check that out instead.
查看 argparse 来处理参数。
拆分为函数。
使用main结构。
查看 csv 模块。您可以将分隔符设置为空格。
再看看 re 表达式。您可以使用一个 re 表达式来完成此操作,其中它是不同模式的“或”。
Check out argparse for handling the arguments.
Split into functions.
Use the main construct.
Look at the csv module. You can set the delimiter to a space.
Look again at the re expression. You can do it with one re expression where it is an 'or' of the different patterns.