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-04-19 08:44:51 +00:00
|
|
|
function __usage { echo "Usage: ./setup.sh fail2ban [<ban|unban> <IP>]" ; }
|
2018-02-13 07:31:12 +00:00
|
|
|
|
2022-06-07 11:42:06 +00:00
|
|
|
fail2ban-client ping &>/dev/null || _exit_with_error "Fail2ban not running"
|
|
|
|
|
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
|
|
|
|
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-07-29 16:14:35 +00:00
|
|
|
BANNED_IPS=$(fail2ban-client status "${JAIL}" | grep -oP '(?<=Banned IP list:\s).+')
|
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-07-29 16:14:35 +00:00
|
|
|
echo "Banned in ${JAIL}: ${BANNED_IPS}"
|
2022-04-05 13:13:59 +00:00
|
|
|
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-04-19 08:44:51 +00:00
|
|
|
( 'help' ) __usage ; exit ;;
|
|
|
|
|
|
|
|
( 'ban' )
|
|
|
|
shift
|
|
|
|
if [[ -n ${1} ]]
|
|
|
|
then
|
|
|
|
RESULT=$(fail2ban-client set custom banip "${@}")
|
|
|
|
if [[ ${RESULT} -gt 0 ]]
|
|
|
|
then
|
|
|
|
echo "Banned custom IP: ${RESULT}"
|
|
|
|
else
|
|
|
|
_log 'error' "Banning '${*}' failed. Already banned?"
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
_log 'warn' "You need to specify an IP address: Run './setup.sh fail2ban ban <IP>'"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
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
|
2022-04-19 08:44:51 +00:00
|
|
|
RESULT=$(fail2ban-client set "${JAIL}" unbanip "${@}" 2>&1)
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2022-04-19 08:44:51 +00:00
|
|
|
[[ ${RESULT} != *"is not banned"* ]] && [[ ${RESULT} != *"NOK"* ]] && echo "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-04-19 08:44:51 +00:00
|
|
|
_log 'warn' "You need to specify an IP address: Run './setup.sh 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
|