Python Forum
python- read specific words from log file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python- read specific words from log file (/thread-7195.html)

Pages: 1 2


python- read specific words from log file - anna - Dec-27-2017

Hi There,

I have below AAA log file, i would like to read and store underlined strings in another file. Please help for script

26 Dec 2017 00:00:00,018 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 0221453122@ttml, MAC : ac:f1:df:ea:4c:26, NASIP : 10.124.117.36, NASPORTID : slot=4;subslot=2;port=1;vlanid=3727;vlanid2=1127;
26 Dec 2017 00:00:00,082 [WARN] RAD-AUTH-680 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0220367961@ttml, MAC : 00:17:7c:75:b6:05, NASIP : 10.124.117.36, NASPORTID : slot=5;subslot=2;port=1;vlanid=2000;vlanid2=2488;
26 Dec 2017 00:00:00,115 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 4010059315, MAC : 08:60:6e:c9:dd:9a, NASIP : 10.124.113.246, NASPORTID : slot=6;subslot=2;port=100;vlanid=560;vlanid2=1284;
26 Dec 2017 00:00:00,189 [WARN] RAD-AUTH-695 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0200400681@ttml, MAC : 5c:f4:ab:1f:3a:ec, NASIP : 10.124.117.180, NASPORTID : pppoe lag-50:2925.3643


regards
Anna Patil


RE: python- read specific words from log file - squenson - Dec-27-2017

The homework forum works this way:
1. You submit your problem AND the beginning of a solution;
2. You indicate which part of your code is not working or with which part you have difficulties;
3. We give you some hints to solve your issues.

Although we are perfectly able to write the entire program, Cool Big Grin , it will not make you progress and therefore it defeats the purpose of this forum.


RE: python- read specific words from log file - anna - Dec-27-2017

thanks, but new to Python


RE: python- read specific words from log file - micseydel - Dec-27-2017

squenson's post applies to users who are new to Python as well. That's certainly most of whose posts are in this section. We have to see an attempt and then we'd be delighted to help you once you show where you're getting blocked.


RE: python- read specific words from log file - Larz60+ - Dec-27-2017

Quote:thanks, but new to Python
Seems like every python programmer started with the same problem

Just make an effort, and you will get a response that you will like


RE: python- read specific words from log file - nilamo - Dec-27-2017

It looks a little bit like you have key:value pairs that are comma separated.  So, using the csv module, here's one way you could parse the contents of the file:
>>> data = '''
... 26 Dec 2017 00:00:00,018 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 0221453122@ttml, MAC : ac:f1:df:ea:4c:26, NASIP : 10.124.117.36, NASPORTID : slot=4;subslot=2;port=1;vlanid=3727;vlanid2=1127;
... 26 Dec 2017 00:00:00,082 [WARN] RAD-AUTH-680 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0220367961@ttml, MAC : 00:17:7c:75:b6:05, NASIP : 10.124.117.36, NASPORTID : slot=5;subslot=2;port=1;vlanid=2000;vlanid2=2488;
... 26 Dec 2017 00:00:00,115 [WARN] RAD-AUTH-RES-18 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS ACCEPT, REPLY MESSAGE : Authentication Success, USERNAME : 4010059315, MAC : 08:60:6e:c9:dd:9a, NASIP : 10.124.113.246, NASPORTID : slot=6;subslot=2;port=100;vlanid=560;vlanid2=1284;
... 26 Dec 2017 00:00:00,189 [WARN] RAD-AUTH-695 [AUTH_COUNT_LOGGER]: PACKET TYPE : ACCESS REQUEST, RESPONSE : ACCESS REJECT, REPLY MESSAGE : Account is not active, USERNAME : 0200400681@ttml, MAC : 5c:f4:ab:1f:3a:ec, NASIP : 10.124.117.180, NASPORTID : pppoe lag-50:2925.3643
'''.split("\n")
>>> import csv
>>> reader = csv.reader(data)
>>> for line in reader:
...   for option in line:
...     if ":" in option:
...       pair = option.split(":")
...       if pair[0].strip() == "REPLY MESSAGE":
...         print(pair)
...
[' REPLY MESSAGE ', ' Authentication Success']
[' REPLY MESSAGE ', ' Account is not active']
[' REPLY MESSAGE ', ' Authentication Success']
[' REPLY MESSAGE ', ' Account is not active']



RE: python- read specific words from log file - anna - Dec-30-2017

tried small code, not able to search other parameters like USERNAME and Message

import re
file = open("test.txt", "r")
#mac = re.compile(r'(?:[0-9A-F]{2}[:]){5}(?:[0-9A-F]{2})')
mac = re.compile(r'[a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}')
#dn = re.compile(r'(\d{10})')
for line in file:
    mac_address = mac.findall(line)
    for word in mac_address:
        print word
file.close()



RE: python- read specific words from log file - squenson - Dec-30-2017

The first thing to do, when you want to write a program, is to try to describe the steps you need to execute in plain English. Number the steps with large intervals so you can insert some missing steps easily. Then, you take each step and write the corresponding small piece of code. In your case, I would write:
10. Open the log file
20. For each line in the log file
30..... If the line contains the string "USERNAME : "
40.......... Extract the characters located after this string, until the ","
50.......... Print the extracted string
etc.


RE: python- read specific words from log file - Terafy - Dec-30-2017

(Dec-30-2017, 05:58 PM)anna Wrote: tried small code, not able to search other parameters like USERNAME and Message

import re
file = open("test.txt", "r")
#mac = re.compile(r'(?:[0-9A-F]{2}[:]){5}(?:[0-9A-F]{2})')
mac = re.compile(r'[a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}')
#dn = re.compile(r'(\d{10})')
for line in file:
    mac_address = mac.findall(line)
    for word in mac_address:
        print word
file.close()

You opened the file test.txt but didn't read it.

Use readlines, for reading each line and returning a list.
message = file.readlines()



RE: python- read specific words from log file - nilamo - Dec-31-2017

What? The very next thing they do, right after compiling a regex, is iterate over each line.

Also, never use readlines(). It'll crash for large files.