Answer the question
In order to leave comments, you need to log in
I can’t parse the result of the program, got confused with the algorithm?
I am not very strong in programming, especially in python.
My task:
There is a txt file that contains the result of the masscan operation. It is
required to display each ip with all its open ports found:
res.txt
Code:
# 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
192.168.1.1 80, 801
192.168.1.2 801, 445
192.168.1.3 80, 8080, 21
#!/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:
print('1')
if elem.split(' ')[1] == ip:
ports.append(elem.split(' ')[3].split('/')[0])
continue
else:
print(ports)
print('nmap '+ip +' ports: '+str(ports))
ports=[]
else:
print('2')
#print('Unic: '+str(ip) + ' port: '+ str(elem.split(' ')[3].split('/')[0]))
ip = elem.split(' ')[1]
ports.append(elem.split(' ')[3].split('/')[0])
Answer the question
In order to leave comments, you need to log in
Sketched a bit, try it.
import re
from collections import defaultdict
ips = defaultdict(list)
regular = re.compile(r'Host: ([\d\.]+).+?Ports: (\d+)/')
with open('res.txt', 'r') as f:
for line in f:
line = line.strip()
if not line.startswith('#'):
ip, port = regular.search(line).groups()
ips[ip].append(port)
for k, v in ips.items(): # Выведет:
print(k, ', '.join(v)) # 192.168.1.1 80, 801
# 192.168.1.2 801, 445
# 192.168.1.3 80, 8080, 21
with open('outputfile.txt', 'w') as f: # Запишет тоже самое
for k, v in ips.items():
f.write('{} {}\n'.format(k, ', '.join(v)))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question