Last active
February 11, 2024 16:12
-
-
Save infnetdanpro/b044131355fcf83177de9c2f279df542 to your computer and use it in GitHub Desktop.
UFW Log analyzer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import re | |
log_entry_pattern = re.compile(r"\bSRC=([\d.]+)\b") | |
def parse_log(log_file): | |
src_ip_counts = defaultdict(int) | |
with open(log_file, "r") as f: | |
for line in f: | |
match = re.search(log_entry_pattern, line) | |
if match: | |
src_ip = match.group(1) | |
src_ip_counts[src_ip] += 1 | |
return src_ip_counts | |
def main(): | |
log_file = input( | |
"введите адрес лог файла: " | |
) # Замените 'your_log_file.log' на путь к вашему журналу | |
src_ip_counts = parse_log(log_file) | |
print("Статистика SRC IP адресов:") | |
rows = [] | |
for src_ip, count in src_ip_counts.items(): | |
rows.append({"ip": src_ip, "count": count}) | |
rows = sorted(rows, key=lambda x: x["count"], reverse=True) | |
for r in rows: | |
print(r["ip"], r["count"]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment