Search code examples
pythonformatted-input

Parse file organised in a certain pattern


f is a file and is shown below:

+++++192.168.1.1+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.1

+++++192.168.1.2+++++
Port Number: 80
......
product: Apache http
IP Address: 192.168.1.2

+++++192.168.1.3+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.3

+++++192.168.1.4+++++
Port Number: 3306
......
product: MySQL
IP Address: 192.168.1.4

+++++192.168.1.5+++++
Port Number: 22
......
product: Open SSH
IP Address: 192.168.1.5

+++++192.168.1.6+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.6

The expected output is:

These hosts have Apache services:

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.6

The code I tried:

for service in f:
    if "product: Apache httpd" in service:
        for host in f:
            if "IP Address: " in host:
                print(host[5:], service)

It just gave me all ip addresses instead of the specific hosts with Apache installed.

How can I make the expected output?


Solution

  • You could also try this:

    apaches = []
    with open('ips.txt') as f:
        sections = f.read().split('\n\n')
    
        for section in sections:
            _, _, _, product, ip = section.split('\n')
            _, product_type = product.split(':')
            _, address = ip.split(':')
    
            if product_type.strip().startswith('Apache'):
                apaches.append(address.strip())
    
    print('These hosts have Apache services:\n%s' % '\n'.join(apaches))
    

    Which Outputs:

    These hosts have Apache services:
    192.168.1.1
    192.168.1.2
    192.168.1.3
    192.168.1.6