2022-10-17 08:40:09 +00:00
|
|
|
#!/bin/bash
|
2018-02-13 07:31:12 +00:00
|
|
|
|
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
|
|
|
|
2023-05-25 23:01:41 +00:00
|
|
|
function __usage() {
|
2023-05-02 22:13:44 +00:00
|
|
|
echo "Usage: ./setup.sh fail2ban [<ban|unban> <IP>]"
|
|
|
|
echo " ./setup.sh fail2ban log"
|
2023-08-02 00:09:01 +00:00
|
|
|
echo " ./setup.sh fail2ban status"
|
2023-05-02 22:13:44 +00:00
|
|
|
}
|
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
|
|
|
|
2023-05-26 12:00:40 +00:00
|
|
|
for LIST in $(fail2ban-client status | grep "Jail list" | cut -f2- | sed 's/,/ /g'); do
|
2020-10-21 16:16:32 +00:00
|
|
|
JAILS+=("${LIST}")
|
|
|
|
done
|
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -z ${1} ]]; then
|
2022-04-05 13:13:59 +00:00
|
|
|
IPS_BANNED=0
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2023-05-26 12:00:40 +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
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -n ${BANNED_IPS} ]]; 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
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -n ${1} ]]; then
|
2022-04-19 08:44:51 +00:00
|
|
|
RESULT=$(fail2ban-client set custom banip "${@}")
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${RESULT} -gt 0 ]]; then
|
2022-04-19 08:44:51 +00:00
|
|
|
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
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ -n ${1} ]]; then
|
2020-11-05 10:41:18 +00:00
|
|
|
|
2023-05-26 12:00:40 +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
|
|
|
|
2023-05-02 22:13:44 +00:00
|
|
|
( 'log' )
|
|
|
|
cat /var/log/mail/fail2ban.log
|
|
|
|
;;
|
|
|
|
|
2023-08-02 00:09:01 +00:00
|
|
|
( 'status' )
|
|
|
|
for JAIL in "${JAILS[@]}"; do
|
|
|
|
printf '%s\n\n' "$(fail2ban-client status "${JAIL}" 2>&1)"
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
|
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
|