базовая структура проекта создана
This commit is contained in:
		
							
								
								
									
										29
									
								
								parser/parse_dns_logs.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								parser/parse_dns_logs.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| 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() | ||||
		Reference in New Issue
	
	Block a user