I am working on a Python script that will allow me to better find specific syslogs files. The script simply consists in typing the IP address of the concerned machine and then the date (in YYYYMMDD format) since there is one log file per day on the server..
The script is largely copied and adapted from a similar script that a colleague programmer did before me. But I don't have as much expertise as he does on Python, and he didn't leave comments on his code.
# This Python file uses the following encoding: utf-8
import os, sys, re
name = raw_input("IP Address? \n")
dir = os.listdir(os.getcwd())
flag_equip = False
flag_h_start = True
flag_h_end = True
flag_date = True
for n in dir :
if(n!="syslog_hosts" and n!="syslog_hosts.avec.domain" and n!="syslog_hosts.BKP"):
for nn in os.listdir("/applis/syslog/syslog_cpe"):
if(re.search(name,nn)):
flag_equip = True
print("Equipment found!")
while(flag_date):
date = raw_input("date AAAAMMJJ ?\n")
if(re.search("[\d]{8}$",date)):
flag_date = False
for nnn in os.listdir("/applis/syslog/syslog_cpe"+"/"+n+"/"+nn):
raw_filename = nnn.split(".")
for i in raw_filename :
if(i==date):
break
if(flag_equip==False):
print("Equipment not found")
I have a problem with the part where I have to enter the date. No matter what I put in the date, it always gives me this error below. The IP address matches the one I entered but not the date which is always the first one of the machine.
File "scriptsyslog.py", line 21, in <module>
for nnn in os.listdir("/applis/syslog/syslog_cpe"+"/"+n+"/"+nn):
OSError: [Errno 2] No such file or directory:'/applis/syslog/syslog_cpe/.bash_history/100.117.130.80.20220404.log'
My code may seem a bit strange but that's because it's not complete. I hope that's clear enough. The log files are all named in the format "[IP address][YYYYMMDD].log" For example 100.117.130.80.20220410.log
Thank you in advance. This is my first question on StackOverflow.