Python Forum
python- read specific words from log file
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python- read specific words from log file
#1
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
Reply
#2
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.
Reply
#3
thanks, but new to Python
Reply
#4
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.
Reply
#5
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
Reply
#6
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']
Reply
#7
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()
Reply
#8
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.
Reply
#9
(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()
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 427 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Recommended way to read/create PDF file? Winfried 3 2,784 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,309 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,048 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,140 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,501 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,814 Jan-25-2023, 04:12 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020