Detecting SSH Brute-Force Attempts with a Minimal Bash Script

I wrote this small Bash script that takes raw logs and generates a clean sorted report of who is hammering your SSH port and how often. This is the kind of small operational tool every Cloud or DevOps engineer should keep in their toolbox.

How it works

  • Detects your OS family Debian or RHEL
  • Selects the correct SSH auth log
  • Extracts failed login attempts
  • Count occurrences per IP
  • Filters by threshold – default: 3
  • Sorts the results by severity

Running it

./ssh_failed_summary.sh

Or, if you want to analyze more aggressive activity:

./ssh_failed_summary.sh 10

Output Example

[INFO] Detected OS family: debian family
[INFO] Using auth log: /var/log/auth.log

192.168.1.10      33
185.22.56.10      12
45.180.3.99       7

The script code

#!/usr/bin/env bash
set -euo pipefail

MIN_FAILS=${1:-3} #default 3 attempts

#detecting OS and auth log file
detect_auth_log() {
local os_family=""
local auth_log=""

if [[ -r /etc/os-release ]]; then
. /etc/os-release
os_family="${ID_LIKE:-$ID}"

case "$os_family" in
*debian*|*ubuntu*)
auth_log="/var/log/auth.log"
os_family="debian family"
;;
*)
auth_log="/var/log/secure"
os_family="rhel family"
;;
esac
else
# Fallback for /etc/os-release
if [[ -f /var/log/auth.log ]]; then
auth_log="/var/log/auth.log"
else
auth_log="/var/log/secure"
fi
os_family="unknown family"
fi

#on stderr
echo "[INFO] Detected OS family: $os_family" >&2
echo "[INFO] Using auth log: $auth_log" >&2

#on stdout
printf '%s\n' "$auth_log"
}

AUTH_LOG=$(detect_auth_log)

#check if the log file is accessible and readable 
if [[ ! -r "$AUTH_LOG" ]]; then
echo "[ERROR] Auth log file '$AUTH_LOG' is not readable or does not exist." >&2
exit 1
fi

awk -v min="$MIN_FAILS" '/Failed password/ {
ip = $(NF-3)
count[ip]++
} END {
for (ip in count) {
if (count[ip] >= min) {
printf "%-16s %d\n", ip, count[ip]
}
}
}' "$AUTH_LOG" | sort -k2 -nr

The code on github

Ali Alrahbe
Ali Alrahbe

Hi, đź‘‹ I'm Ali Alrahbe, a cybersecurity professional passionate about building cloud infrastructures that are both secure and resilient.

I got my start in tech on the front lines of IT support. That experience didn't just teach me how to solve complex problems—it showed me that proactive security is the bedrock of any successful digital system. That realization drove me to specialize in cloud security.
I'm AWS Certified Solutions Architect Associate, I hold a Bachelor's degree in computer systems engineering and currently pursuing a Master's in Cybersecurity in Berlin, focusing on Cloud Security, DevSecOps, and Infrastructure as Code (IaC).

On my website, Corefortify.com, I document my journey, share hands-on projects, and break down complex security concepts in the evolving world of cloud technology.

Feel free to connect with me on LinkedIn!

Articles: 14