Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
shell script to remove IPv4 addresses from all fail2ban jails
#!/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
You can’t perform that action at this time.