Finding strings in a log and other patterns

Asked

Viewed 182 times

-2

I’m trying to identify two strings in a log file: "Connected" and "Disconnected". But I’m not sure how to make it work.

On my server I have stored several log files

import os
import time
import re

data = time.strftime("%d.%m.%Y")

logs = []

#listar os arquivos .LOGS
for file in os.listdir('/var/log/mikrotik'):
 if file.endswith(".log"):
  if(re.search(data,file)): 
   logs.append(os.path.join("/var/log/mikrotik", file))

for l in logs:
 file=open(l,"r")
 for line in file.read():
  if re.match("/connected|disconnected/",line):
   print line
  else:
   print line 

What I’m trying to do, I check all my . logs that have the current date. and store them in a list, then try to treat each log individually trying to identify the lines that have "Connected" and "Disconnected", if I find in need just take the "user" and the date and time of those lines, but I have no idea how this can be. Someone could give me a light?

Log:

inserir a descrição da imagem aqui

  • 1

    Edith your question and put the code.

  • Edited and posted the code.

  • Shell script meets ide very simple way this. Why not use then?

2 answers

1

...
file=open(l,"r")
    for line in file.readline():
        if 'connected' in line or 'disconnected' in line:
            #capture line
            #filtre para obter o usuário, data e hora
            print line
...

0


I found this solution:

import os
import re
import sys

f = open('log.log','r')
log = []
for line in f:
 if re.search(r': connected|: disconnected',line):
  ob = dict()
  ob['USER'] = re.search(r'<pppoe(.*?)>',line).group(0).replace("<pppoe-","").replace(">","")
  ob['DATA'] = re.search(r'^\w{3} \d{2} \d{2}:\d{2}:\d{2}',line).group(0)
  ob['CONNECTION'] = re.search(r': .*',line).group(0).replace(": ", "")
  log.append(ob)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.