I am beginner to python and I have a log file which contains MBX_AUTHENTICATION_FAILED where I already took the information of username, timestamp and IP address.
My question is how can I take a username who has changed password for 10 times from log file, because the condition is whenever a user changes password for 10 times, we need the detail of the user such as username, timestamp and IP address. This is my log file:
20170119 193739188+0900 elim1td001p imapserv 52324 75559 132341478487808 Note;UserDataException(504/1) MBX_AUTHENTICATION_FAILED:{protocolType=[imap], userName=[teasst_emailrei_6000], password=[XXXXX]}:AuthenticateAndGetMailboxService\3aPOST:Authenticating Failed.::user=test_emaili_3000:cmd=1 LOGIN teasst_emailrei_6000 <password>:fromhost=129.0.0.1:sid=b34f10a-fd04-11e7-b246-7f629ba04def
This is my python code
import re
from csv import writer
import datetime
log_file = '/Users/kiya/Desktop/ip.txt'
output_file = '/Users/kiya/Desktop/output.csv'
name_to_check = 'MBX_AUTHENTICATION_FAILED'
with open(log_file,encoding="utf-8") as infile:
for line in infile:
if name_to_check in line:
username = re.search(r'(?<=userName=\[)(.*)(?=\],)', line)
username = username.group()
date = re.search(r'(?P<date>\d{8})\s+(?P<time>\d{9})\+(?P<zone>\d{4})', line)
date = datetime.datetime.strptime(date.group('date'), "%Y%m%d").strftime("%Y-%m-%d")
print(date)
time = re.search(r'(?P<date>\d{8})\s+(?P<time>\d{9})\+(?P<zone>\d{4})', line)
time = datetime.datetime.strptime(time.group('time'), "%H%M%S%f").strftime("%H:%M:%S")
print(time)
ip = re.search(r'(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])',line)
ip = ip.group()
with open(output_file, 'w') as outfile:
csv_writer = writer(outfile)
csv_writer.writerow(["Username","Date","Time","Ip"])
csv_writer.writerow([username,date,time,ip])
MBX_AUTHENTICATION_FAILEDusing the current code?