base-stack/sbin/fail2ban-unban.sh

36 lines
951 B
Bash
Raw Permalink Normal View History

2022-11-17 09:47:11 -08:00
#!/bin/bash
# IP should be first arg
if [ -n "$1" ]; then
IP=$1
else
echo "IP not set"
exit 1
fi
LAST_LINE=`grep -i "ban $IP" /var/log/fail2ban.log|tail -1`
if [ -z "$LAST_LINE" ]; then
# no bans found, doing search for Found
LAST_LINE=`grep "Found $IP" /var/log/fail2ban.log|tail -1`
if [ -z "$LAST_LINE" ]; then
echo "No matches for $IP"
exit
fi
fi
ACTION=`echo $LAST_LINE|awk '{ print $7 }'`
FILTER=`echo $LAST_LINE|awk -F'[][]' '{ print $4 }'`
if [ $ACTION = 'Ban' ] || [ $ACTION = 'Restore' ]; then
echo "unbanning & whitelisting $FILTER:$IP"
fail2ban-client set $FILTER unbanip $IP
fail2ban-client set $FILTER addignoreip $IP
elif [ $ACTION = 'Unban' ]; then
echo "$FILTER:$IP already autounbanned, whitelisting now"
fail2ban-client set $FILTER addignoreip $IP
elif [ $ACTION = 'Found' ]; then
echo "$IP not banned, but Found $FILTER:$IP, whitelisting now"
fail2ban-client set $FILTER addignoreip $IP
fi