Finding strings in a log and other patterns [closed]

-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 only get the "user" and the date and time of those lines, however I do not have idea of how this can be. Could someone give me a light?

Log:

    
asked by anonymous 20.12.2017 / 00:06

2 answers

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)
    
24.12.2017 / 03:32
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
...
    
22.12.2017 / 22:29