Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
unban-ip.sh
#!/bin/bash
# ########################################################################### #
# Unban IP #
# -------- #
# This script makes it easier to unban IP Addresses that where banned using #
# the application called "fail2ban". #
# ########################################################################### #
red=$'\033[1;31m'
nc=$'\033[0m'
# ------------- #
# PRE-MAIN CODE #
# ------------- #
# Checks to see if this script was executed with root privilege
if [[ $EUID -ne 0 ]]; then
echo "${red}Please run this script as root or with root privilege${nc}"
echo -e "\nExiting..."
exit 1
fi
# Checks if fail2ban is installed
if ! hash fail2ban-client; then
echo "${red}The application 'fail2ban' is not installed${nc}"
echo -e "\nExiting..."
exit 1
fi
# --------- #
# FUNCTIONS #
# --------- #
# Lists either the banned ip addresses within a jail or lists all active jails
jail_list() {
jail_name=$(echo $1 | awk '{print $2}')
# $jail_name doesn't have quotes surrounding it because if the variable is left
# empty/blank, the command will ignore the blank variable, instead of it
# interpreting the blank variable as a jail. If the variable is left blank, the
# output will show the active jails.
fail2ban-client status $jail_name 2>/dev/null
echo ""
}
# --------- #
# MAIN CODE #
# --------- #
echo "Helpful commands (they can be used whenver you are able to input text):"
echo " list - Lists all active/enabled jails"
echo -e " list [name of jail] - Lists all banned IP Addresses inside" \
"spcified jail\n\n"
while true; do
echo "What is the IP Address you want to unban?"
read ip
if [[ $(echo $ip | awk '{print $1}') = "list" ]]; then
jail_list "$ip"
continue
fi
break
done
echo -e "\n--------------------------\n"
while true; do
echo "What jail is the IP Address located in?"
read jail
if [[ $(echo $jail | awk '{print $1}') = "list" ]]; then
jail_list "$jail"
else
fail2ban-client status $jail &>/dev/null || {
echo -e "Sorry but the jail '$jail' does not exist\n"
continue
}
break
fi
done
fail2ban-client set "$jail" unbanip "$ip" 2>/dev/null
@StrangeRanger

This comment has been minimized.

Copy link
Owner Author

@StrangeRanger StrangeRanger commented Jun 7, 2021

Project Tracker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment