Python Projects & Skills / Files and data
Lesson 16 of 28
Log file analysis
Security teams read logs to spot attacks. Count failed logins per IP with a Counter and print the worst offenders.
Key points
- collections.Counter counts things
- .most_common(5) shows the top 5
- Combine with regex to pull out IPs
from collections import Counter
ips = Counter()
for line in open("auth.log"):
if "Failed password" in line:
ips[line.split()[-4]] += 1
print(ips.most_common(5))Quiz · +10 XP
Which tool counts how often each item appears?
Log in to save progress and earn XP.