2018-02-13 07:31:12 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2022-02-21 10:56:57 +00:00
|
|
|
# shellcheck source=../scripts/helpers/index.sh
|
|
|
|
source /usr/local/bin/helpers/index.sh
|
2018-02-13 07:31:12 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
function __usage { echo "Usage: ${0} [<unban> <ip-address>]" ; }
|
2018-02-13 07:31:12 +00:00
|
|
|
|
2020-11-05 10:41:18 +00:00
|
|
|
unset JAILS
|
2020-10-21 16:16:32 +00:00
|
|
|
declare -a JAILS
|
2022-04-05 13:13:59 +00:00
|
|
|
IP_REGEXP='((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
|
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
for LIST in $(fail2ban-client status | grep "Jail list" | cut -f2- | sed 's/,/ /g')
|
|
|
|
do
|
|
|
|
JAILS+=("${LIST}")
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -z ${1} ]]
|
|
|
|
then
|
2022-04-05 13:13:59 +00:00
|
|
|
IPS_BANNED=0
|
2020-10-21 16:16:32 +00:00
|
|
|
|
|
|
|
for JAIL in "${JAILS[@]}"
|
|
|
|
do
|
2022-04-05 13:13:59 +00:00
|
|
|
BANNED_IPS=$(fail2ban-client status "${JAIL}" \
|
|
|
|
| grep 'Banned IP list' \
|
|
|
|
| grep -oE "${IP_REGEXP}")
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-04-05 13:13:59 +00:00
|
|
|
if [[ -n ${BANNED_IPS} ]]
|
2020-10-21 16:16:32 +00:00
|
|
|
then
|
2022-04-05 13:13:59 +00:00
|
|
|
echo "Banned in ${JAIL}: ${BANNED_IPS//$'\n'/, }"
|
|
|
|
IPS_BANNED=1
|
2018-02-13 07:31:12 +00:00
|
|
|
fi
|
|
|
|
done
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-04-05 13:13:59 +00:00
|
|
|
[[ ${IPS_BANNED} -eq 0 ]] && _log 'info' "No IPs have been banned"
|
2018-02-13 07:31:12 +00:00
|
|
|
else
|
2020-11-05 10:41:18 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
case "${1}" in
|
2020-11-05 10:41:18 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( 'unban' )
|
2018-02-13 07:31:12 +00:00
|
|
|
shift
|
2020-10-21 16:16:32 +00:00
|
|
|
if [[ -n ${1} ]]
|
|
|
|
then
|
2020-11-05 10:41:18 +00:00
|
|
|
|
2020-10-21 16:16:32 +00:00
|
|
|
for JAIL in "${JAILS[@]}"
|
|
|
|
do
|
2020-11-05 11:40:24 +00:00
|
|
|
RESULT="$(fail2ban-client set "${JAIL}" unbanip "${@}" 2>&1)"
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2021-12-21 16:01:40 +00:00
|
|
|
[[ ${RESULT} != *"is not banned"* ]] && [[ ${RESULT} != *"NOK"* ]] && echo -e "Unbanned IP from ${JAIL}: ${RESULT}"
|
2018-02-13 07:31:12 +00:00
|
|
|
done
|
2020-11-05 10:41:18 +00:00
|
|
|
|
2018-02-13 07:31:12 +00:00
|
|
|
else
|
2022-03-26 08:30:09 +00:00
|
|
|
_log 'warn' "You need to specify an IP address: Run './setup.sh debug fail2ban' to get a list of banned IP addresses"
|
2020-11-05 10:41:18 +00:00
|
|
|
exit 0
|
2018-02-13 07:31:12 +00:00
|
|
|
fi
|
|
|
|
;;
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
( * )
|
|
|
|
__usage
|
|
|
|
_exit_with_error "Unknown command '${1}'"
|
2018-02-13 07:31:12 +00:00
|
|
|
;;
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2020-11-05 10:41:18 +00:00
|
|
|
esac
|
|
|
|
|
2018-02-13 07:31:12 +00:00
|
|
|
fi
|
2020-11-05 10:41:18 +00:00
|
|
|
|
|
|
|
exit 0
|