Contents

fail2ban jail list & unban IP script

Contents

Fail2ban is my all-time favorite tool for protecting an exposed nginx server and in my case nginx is the primary gateway for the majority of my self-hosted services, I have created the following script that allows me to quickly view jail statuses and unblock IPs.

Tested with fail2ban version: 1.0.2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

jails=$(fail2ban-client status | awk '/Jail list:/ {gsub(",", ""); for(i=4; i<=NF; i++) printf "%s ", $i}')

for jail in $jails
do
    banned_ips=$(fail2ban-client status $jail | awk '/Banned IP list:/ {print $NF}')
    echo "Jail: $jail"
    echo "List of banned IPs:"
    echo "$banned_ips"
    echo ""

    read -p "Do you want to unban any IP address from $jail? (y/n) " unban

    if [ "$unban" = "y" ]; then
        read -p "Enter the IP address you want to unban: " ip
        fail2ban-client set $jail unbanip $ip
        echo "IP address $ip has been unbanned from $jail."
    fi
done

to use it clone the repo, make it executable, and run it:

1
2
3
git clone https://github.com/aganet/fail2ban-list-unban-script.git
chmod +x fail2ban-list-unblock.sh
sudo ./fail2ban-list-unblock.sh

https://github.com/aganet/fail2ban-list-unban-script