Upload files to "/"
This commit is contained in:
78
version14.py
Normal file
78
version14.py
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import re
|
||||||
|
import os
|
||||||
|
import concurrent.futures
|
||||||
|
|
||||||
|
CHUNK_SIZE = 5
|
||||||
|
|
||||||
|
def read_lookup_table(lookup_table_path):
|
||||||
|
"""
|
||||||
|
para ler a tabela de pesquisa e retornar um dicionário mapeando endereços IP para nomes de usuário.
|
||||||
|
"""
|
||||||
|
ip_to_username = {}
|
||||||
|
with open(lookup_table_path, 'r') as lookup_file:
|
||||||
|
for line in lookup_file:
|
||||||
|
parts = line.strip().split()
|
||||||
|
if len(parts) == 2:
|
||||||
|
ip, username = parts
|
||||||
|
ip_to_username[ip] = username
|
||||||
|
else:
|
||||||
|
print(f"Skipping line in lookup table: {line.strip()}")
|
||||||
|
return ip_to_username
|
||||||
|
|
||||||
|
|
||||||
|
def process_chunk(chunk_index, chunk, lookup_table_path):
|
||||||
|
"""
|
||||||
|
Processa um pedaço de linhas do arquivo de log.
|
||||||
|
|
||||||
|
"""
|
||||||
|
ip_to_username = read_lookup_table(lookup_table_path)
|
||||||
|
modified_lines = []
|
||||||
|
for line in chunk:
|
||||||
|
modified_line = line
|
||||||
|
match = re.search(r"PASS\s+-\s+(\d+\.\d+\.\d+\.\d+)", modified_line)
|
||||||
|
if match:
|
||||||
|
ip_address = match.group(1).strip()
|
||||||
|
if ip_address in ip_to_username:
|
||||||
|
username = ip_to_username[ip_address]
|
||||||
|
modified_line = re.sub(r"PASS\s+-\s+", f"PASS {username:<15}", modified_line)
|
||||||
|
modified_lines.append(modified_line)
|
||||||
|
return chunk_index, modified_lines
|
||||||
|
|
||||||
|
class LogTracker:
|
||||||
|
def __init__(self, input_log_path, output_log_path, lookup_table_path):
|
||||||
|
self.input_log_path = input_log_path
|
||||||
|
self.output_log_path = output_log_path
|
||||||
|
self.lookup_table_path = lookup_table_path
|
||||||
|
self.last_position = 0
|
||||||
|
|
||||||
|
def track_log(self):
|
||||||
|
while True:
|
||||||
|
with open(self.input_log_path, 'r') as infile:
|
||||||
|
infile.seek(self.last_position)
|
||||||
|
lines = infile.readlines()
|
||||||
|
self.last_position = infile.tell()
|
||||||
|
chunks = [lines[i:i + CHUNK_SIZE] for i in range(0, len(lines), CHUNK_SIZE)]
|
||||||
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||||
|
futures = {executor.submit(process_chunk, i, chunk, self.lookup_table_path): i for i, chunk in enumerate(chunks)}
|
||||||
|
# Creando um diccionari (array) results
|
||||||
|
results = {}
|
||||||
|
# colocar as linhas modificadas neste array abaixo
|
||||||
|
for future in concurrent.futures.as_completed(futures):
|
||||||
|
chunk_index = future.result()[0]
|
||||||
|
modified_lines = future.result()[1]
|
||||||
|
results[chunk_index] = modified_lines
|
||||||
|
|
||||||
|
# Escrever as linhas modificadas na order certa
|
||||||
|
with open(self.output_log_path, 'a') as outfile:
|
||||||
|
for i in range(len(chunks)):
|
||||||
|
outfile.writelines(results[i])
|
||||||
|
|
||||||
|
def modify_logfile_continuously(input_log_path, output_log_path, lookup_table_path):
|
||||||
|
log_tracker = LogTracker(input_log_path, output_log_path, lookup_table_path)
|
||||||
|
log_tracker.track_log()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
input_log_path = "logger.log"
|
||||||
|
output_log_path = "log_modified.log"
|
||||||
|
lookup_table_path = "lookup_table.tsv"
|
||||||
|
modify_logfile_continuously(input_log_path, output_log_path, lookup_table_path)
|
||||||
Reference in New Issue
Block a user