29 lines
960 B
Python
29 lines
960 B
Python
import re, sqlite3, os
|
|
from datetime import datetime
|
|
|
|
db = sqlite3.connect('../db/dns.sqlite')
|
|
db.execute('''CREATE TABLE IF NOT EXISTS logs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
timestamp TEXT,
|
|
src_ip TEXT,
|
|
qtype TEXT,
|
|
domain TEXT
|
|
)''')
|
|
|
|
pattern = re.compile(r'(\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}).*UDP Rcv (\d+\.\d+\.\d+\.\d+).*Q
|
|
|
|
\[.*\]
|
|
|
|
(\w+) ([\w\.-]+)\.')
|
|
|
|
for filename in os.listdir('../logs'):
|
|
if filename.startswith('dns') and filename.endswith('.log'):
|
|
with open(f'../logs/{filename}', encoding='utf-8') as f:
|
|
for line in f:
|
|
match = pattern.search(line)
|
|
if match:
|
|
ts = datetime.strptime(match.group(1), "%d/%m/%Y %H:%M:%S")
|
|
db.execute("INSERT INTO logs (timestamp, src_ip, qtype, domain) VALUES (?, ?, ?, ?)",
|
|
(ts.isoformat(), match.group(2), match.group(3), match.group(4)))
|
|
db.commit()
|
|
db.close() |