Damon
Damon

Reputation: 119

Searching for a given string IP-address in a logfile

I am working on a project to search an IP address and see if it is in the logfile. I made some good progress but got stuck when dealing with searching certain items in the logfile format.

Here is what I have:

IP = raw_input('Enter IP Address:')
with open ('RoutingTable.txt', 'r') as searchIP:
    for line in searchIP:
        if IP in line:
            ipArray =  line.split()
            print ipArray
            if IP == ipArray[0]:
                print "Success"
            else:
                print "Fail"

As you can see this is very bad code but I am new to Python and programming so I used this to make sure I can at least open file and compare first item to string I enter.

Her is an example file content (my actual file has like thousands of entries):

https://pastebin.com/ff40sij5

I would like a way to store all IPs (just IP and not other junk) in an array and then a loop to go through all items in array and compare with user defined IP.

For example, for this line all care care about is 10.20.70.0/23

D EX    10.20.70.0/23 [170/3072] via 10.10.10.2, 6d06h, Vlan111
                      [170/3072] via 10.10.10.2, 6d06h, Vlan111
                      [170/3072] via 10.10.10.2, 6d06h, Vlan111
                      [170/3072] via 10.10.10.2, 6d06h, Vlan111

Please help.

Thanks Damon

Edit: I am digging setting flags but that only works in some cases as you can see all lines do not start with D but there are some that start with O (for OSFP routes) and C (directly connected).

Here is how is what I am doing:

f = open("RoutingTable.txt")
Pr = False
for line in f.readlines():
    if Pr: print line
    if "EX" in line:
        Pr = True
        print line
    if "[" in line:
        Pr = False
f.close()

That gives me a bit cleaner result but still whole line instead of just IP.

Upvotes: 0

Views: 150

Answers (2)

ryugie
ryugie

Reputation: 181

Do you necessarily need to store all the IPs by themselves? You can do the following, where you grab all the data into a list and check if your input string resides inside the list:

your_file = 'RoutingTable.txt'
IP = input('Enter IP Address:')

with open(your_file, 'r') as f:
    data = f.readlines()

for d in data:
    if IP in d:
        print 'success'
        break
else:
    print 'fail'

The else statement only triggers when you don't break, i.e. there is no success case.

If you cannot read everything into memory, you can iterate over each line like you did in your post, but thousands of lines should be easily doable.


Edit

import re

your_file = 'RoutingTable.txt'
ip_addresses = []
IP = input('Enter IP Address:')

with open(your_file, 'r') as f:
    data = f.readlines()

for d in data:
    res = re.search('(\d+\.\d+\.\d+\.\d+\/\d+)', d)
    if res:
        ip_addresses.append(res.group(1))

for ip_addy in ip_addresses:
    if IP == ip_addy:
        print 'success'
        break
else:
    print 'fail'

print ip_addresses

Upvotes: 2

Bluer123
Bluer123

Reputation: 1

First up, I'd like to mention that your initial way of handling the file opening and closing (where you used a context manager, the "with open(..)" part) is better. It's cleaner and stops you from forgetting to close it again.

Second, I would personally approach this with a regular expression. If you know you'll be getting the same pattern of it beginning with D EX or O, etc. and then an address and then the bracketed section, a regular expression shouldn't be much work, and they're definitely worth understanding.

This is a good resource to learn generally about them: http://regular-expressions.mobi/index.html?wlr=1

Different languages have different ways to interpret the patterns. Here's a link for python specifics for it (remember to import re): https://docs.python.org/3/howto/regex.html

There is also a website called regexr (I don't have enough reputation for another link) that you can use to mess around with expressions on to get to grips with it.

To summarise, I'd personally keep the initial context manager for opening the file, then use the readlines method from your edit, and inside that, use a regular expression to get out the address from the line, and stick the address you get back into a list.

Upvotes: 0

Related Questions