Skip to content

Instantly share code, notes, and snippets.

@yolabingo
Last active May 6, 2020 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save yolabingo/c810db6fe7f8bfcb9eb4f6ffc531e474 to your computer and use it in GitHub Desktop.
Save yolabingo/c810db6fe7f8bfcb9eb4f6ffc531e474 to your computer and use it in GitHub Desktop.
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