我开始学习编程,我试图写一个简单的解析器,但我困惑。如果有人帮助我,我会很高兴的。在
在mres.txt文件在
# Masscan 1.0.3 scan initiated Sun Dec 23 23:00:31 2018
# Ports scanned: TCP(1;80-80) UDP(0;) SCTP(0;) PROTOCOLS(0;)
Host: 192.168.1.1 () Ports: 80/open/tcp////
Host: 192.168.1.1 () Ports: 801/open/tcp////
Host: 192.168.1.2 () Ports: 801/open/tcp////
Host: 192.168.1.2 () Ports: 445/open/tcp////
Host: 192.168.1.3 () Ports: 80/open/tcp////
Host: 192.168.1.3 () Ports: 8080/open/tcp////
Host: 192.168.1.3 () Ports: 21/open/tcp////
# Masscan done at Sun Dec 23 23:00:45 2018
我想以以下格式接收数据:
^{pr2}$在脚本.py在
#!/usr/bin/env python
import sys, re, os
ports = []
ip = None
with open('mres.txt','r') as f:
for elem in f.readlines():
if not elem.startswith('#'):
if ip != None:
if elem.split(' ')[1] == ip:
ports.append(elem.split(' ')[3].split('/')[0])
continue
else:
print(ip + str(ports))
ports=[]
else:
#print('Unic: '+str(ip) + ' port: '+ str(elem.split(' ')[3].split('/')[0]))
ip = elem.split(' ')[1]
ports.append(elem.split(' ')[3].split('/')[0])
最好使用dict来处理数据: 1) IP可以是Dict的密钥 2) List可以是Dict中的值
如果需要,我可以为您创建示例代码。在
这将处理数据,并打印所需的输出。我已经尽力在下面的评论中解释清楚了,但是如果有什么不清楚的地方,请提问。在
相关问题 更多 >
编程相关推荐