Last active
May 6, 2020 13:16
-
-
Save yolabingo/c810db6fe7f8bfcb9eb4f6ffc531e474 to your computer and use it in GitHub Desktop.
shell script to remove IPv4 addresses from all fail2ban jails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function print_help { | |
echo "remove an ip address from all fail2ban jails" | |
echo "$(basename $0) ip_address [ip_address] [ip_address]" | |
} | |
if [[ -z "$@" ]] | |
then | |
print_help | |
fi | |
for ip in "$@" | |
do | |
# does it resemble an IPv4 address? | |
if ( echo "$ip" | egrep -q "^([12]?[0-9]{1,2}[\.]){3}[12]?[0-9]{1,2}$" ) | |
then | |
for jail in $(fail2ban-client status | grep 'Jail list:' | sed 's/.*Jail list://' | sed 's/,//g') | |
do | |
result=$(fail2ban-client set $jail unbanip $ip 2>&1 | grep -v 'IP.*is not banned') | |
if [[ "$result" =~ $ip ]] | |
then | |
echo "$ip removed from $jail" | |
else | |
echo "$ip not in jail $jail" | |
fi | |
done | |
else | |
echo "skipping $ip -- typo?" | |
print_help | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment