Fail2ban Ban Report Shell Script for Linux (Daily Summary)

fail2ban report shell script

Get a daily Fail2ban ban report — see which IPs were blocked, per jail stats, and top SSH attackers on your Linux servers.

What this script does

  • Lists all active Fail2ban jails
  • Shows currently banned IP count per jail
  • Summarizes top attacking IPs from auth.log
  • Email daily report to admin
  • Logs output for SIEM ingestion

Prerequisites

  • Fail2ban installed and running
  • fail2ban-client command
  • Optional: mail

Step 1: Save the script

sudo nano /usr/local/bin/fail2ban-report.sh
sudo chmod +x /usr/local/bin/fail2ban-report.sh

Step 2: Full script (scroll to read)

fail2ban-report.sh
#!/usr/bin/env bash
set -euo pipefail

ALERT_EMAIL="[email protected]"
AUTH_LOG="/var/log/auth.log"
LOG="/var/log/fail2ban-report.log"

log(){ echo "[$(date '+%F %T')] $*" | tee -a "$LOG"; }

REPORT="Fail2ban report for $(hostname -s) — $(date)\n\n"

for jail in $(fail2ban-client status 2>/dev/null | grep 'Jail list' | sed 's/.*://;s/,/ /g'); do
  jail=$(echo "$jail" | xargs)
  [[ -z "$jail" ]] && continue
  status=$(fail2ban-client status "$jail" 2>/dev/null || true)
  banned=$(echo "$status" | grep 'Banned IP list' | sed 's/.*://' | xargs)
  count=$(echo "$status" | grep 'Currently banned' | awk '{print $NF}')
  REPORT+="Jail: $jail | Currently banned: $count\n"
  [[ -n "$banned" ]] && REPORT+="  IPs: $banned\n"
  REPORT+="\n"
done

if [[ -f "$AUTH_LOG" ]]; then
  REPORT+="Top failed SSH IPs (last 24h):\n"
  REPORT+=$(grep 'Failed password' "$AUTH_LOG" 2>/dev/null | \
    grep "$(date +%b' '%e)" | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head -5)
  REPORT+="\n"
fi

log -e "$REPORT"
[[ -n "$ALERT_EMAIL" ]] && command -v mail >/dev/null && \
  echo -e "$REPORT" | mail -s "Fail2ban daily report: $(hostname -s)" "$ALERT_EMAIL"

Scroll inside the box to read the full script.

Step 3: Configure settings

  • JAILS — space-separated jail names or auto-detect all
  • ALERT_EMAIL — daily report recipient
  • AUTH_LOG — path to auth.log for attacker summary
Fail2ban ban report shell script Linux
Fail2ban ban report shell script Linux

Step 4: Test manually

fail2ban-client status
sudo /usr/local/bin/fail2ban-report.sh

Schedule with cron

sudo crontab -e

Add:

0 8 * * * /usr/local/bin/fail2ban-report.sh >> /var/log/fail2ban-report.log 2>&1

Related tutorials

Terminal screenshot is an original illustration created for Gnome IT Solutions (blog.gnomeitsolutions.com).