Any Linux server that is reachable from the internet receives a constant stream of SSH login attempts. The evidence sits in the authentication log, but reading thousands of lines by hand is not realistic. In this post you will write a Python script that summarises failed logins in seconds.
Where the data lives
On Debian and Ubuntu systems, SSH authentication events are written to /var/log/auth.log. On Red Hat based systems the file is /var/log/secure. A failed attempt looks like this:
Mar 4 10:15:32 web01 sshd[2211]: Failed password for invalid user admin from 203.0.113.45 port 51234 ssh2
Mar 4 10:15:35 web01 sshd[2213]: Failed password for root from 198.51.100.7 port 40112 ssh2
Each line tells us the username that was tried and the source IP address. That is all we need.
Step 1: Match the lines with a regular expression
A regular expression describes the pattern we are looking for. This one captures the username and the IP address, and handles the optional "invalid user" text:
import re
PATTERN = re.compile(r"Failed password for (?:invalid user )?(\S+) from (\S+)")
line = "Failed password for invalid user admin from 203.0.113.45 port 51234 ssh2"
match = PATTERN.search(line)
print(match.group(1), match.group(2)) # admin 203.0.113.45
Step 2: Count attempts per IP and per user
Python's Counter makes counting effortless:
import re
from collections import Counter
PATTERN = re.compile(r"Failed password for (?:invalid user )?(\S+) from (\S+)")
by_ip = Counter()
by_user = Counter()
with open("/var/log/auth.log", errors="ignore") as log:
for line in log:
match = PATTERN.search(line)
if match:
user, ip = match.groups()
by_ip[ip] += 1
by_user[user] += 1
print("Top source IPs:")
for ip, count in by_ip.most_common(10):
print(f" {ip:<16} {count}")
print("Most targeted usernames:")
for user, count in by_user.most_common(10):
print(f" {user:<16} {count}")
Reading the file line by line keeps memory usage low, even for very large logs.
Step 3: Flag likely brute-force sources
A handful of failures is normal: people mistype passwords. Hundreds of failures from one address are not. Add a simple threshold:
THRESHOLD = 20
suspects = [ip for ip, count in by_ip.items() if count >= THRESHOLD]
print(f"{len(suspects)} IPs exceeded {THRESHOLD} failed attempts")
You can write the suspects to a file and feed them into a firewall rule or a report for your team.
Reading the results
When you run this on a real server, you will usually notice two things:
- The usernames are predictable. Attackers try
root,admin,test,ubuntuand other common names. - A few IPs make most of the noise. Automated bots hammer servers continuously.
Both findings point to the same fixes: disable password login in favour of SSH keys, disable direct root login and rate-limit or block abusive sources.
Taking it further
This script is a great introduction to log analysis, but in a real environment you would collect logs from every server centrally and alert on them automatically. That is exactly what a SIEM such as Wazuh does, and why we teach Python and Wazuh side by side.
Learn this hands-on
Applied Python Programming
Write real security scripts with a guided lab for every concept.
View course