Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to correctly receive data in PYTHON in format exp: “127.0.0.1 80, 445”

I want to parse this file to get this result. I will be very grateful for the help.

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

I want:

 192.168.1.1 80, 801
 192.168.1.2 801, 445
 192.168.1.3 80, 8080, 21

I try like this, but it does not work:

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('nmap '+ip +' ports: '+str(ports))
                  ports=[]
          else:
              print('Unic: '+ip + ' port: '+ elem.split(' ')[3].split('/')[0])
              ip = elem.split(' ')[1]
              ports.append(elem.split(' ')[3].split('/')[0])

Comments