0

Basically, I have a folder where absolutely huge log files are archived every day. 3 log files are created per day more precisely.

I'm working on a Python script where the user has to enter a date in YYYYMMDD format in order to locate the 3 files that have been created on this date, then he enters a start and end time in hour, minute and seconds and an IP address. And the script will read the content of the 3 .gz files in the given interval and print the lines where the IP address is present.

import re
import os
import glob
import gzip
from datetime import datetime, timedelta

date_entry = raw_input('Enter a date in YEAR, MONTH, DAY format \n')
date = datetime.strptime(re.sub("\s+", "", date_entry), "%Y,%m,%d").date()

path = "/applis/tacacs/log/"


list_of_files = [
    file for file in glob.glob(path + '*.gz')
    if date == datetime.fromtimestamp(os.path.getmtime(file)).date()
]

debut = raw_input('Start (Hour:Minute:Second) \n')
fin = raw_input('End (Hour:Minute:Second) \n')
Adresse_IP = raw_input('IP Address \n')

filedata = {list_of_files: gzip.open(list_of_files, 'r') for list_of_files in list_of_files}
for line in filedata:
        d = line.split(" ",1)[0] 
        if d >= debut and d <= fin:
            re.search(Adresse_IP, line)
            print line,

But it doesn't work, when I try the script, nothing happens. There is no error or anything. Even though the IP address I gave should be in the range for at least one of the file I have given. Could someone please provide me with some guidance? I'm still a beginner on Python so maybe my code is completely wrong.

Kevin.T
  • 52
  • 1
  • 9
  • Try to split you code into separate parts, it will be easier to test it that way. You can start by first checking if your script chooses correct files – Grekkq Apr 19 '22 at 10:15
  • @Grekkq I've actually tested this before. It does find the 3 corresponding files after giving the date in YYYYMMDD. So everything above line 16 should be correct. – Kevin.T Apr 19 '22 at 10:17
  • Okay, now i see it, when you are trying to compare time you call ">=" and "<=" on two strings, you have to first map them to some kind of time format that allows checking if provided time is between provided limits. – Grekkq Apr 19 '22 at 10:22
  • @Grekkq You mean something like this ? `booked_date_ranges = [ [datetime.strptime(debut, '%H:%M:%S'), datetime.strptime(fin, '%H:%M:%S')] for debut, fin in list_of_files ]` – Kevin.T Apr 19 '22 at 10:39
  • I'm not sure what you want to do with that list comprehension but you can refer to this https://stackoverflow.com/questions/1831410/how-to-compare-times-of-the-day so firstly make the inputs a datetime format, and then in your if condition use them. (don't forger to convert the time read from file as well) – Grekkq Apr 19 '22 at 10:47

0 Answers0