-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunfail2ban.sh
executable file
·66 lines (57 loc) · 1.66 KB
/
unfail2ban.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# ------------------------------------------------------------------------------
# Unbans the given IPs from all Fail2Ban jails.
#
# Usage: sudo unfail2ban.sh ip [ip ...]
#
# Author : Esa Jokinen (oh2fih)
# Home : https://github.com/oh2fih/Misc-Scripts
# ------------------------------------------------------------------------------
IPS=()
for ARG in "$@"; do
if [[ "$ARG" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
IPS+=("$ARG")
fi
if [[ "${ARG,,}" =~ ^([0-9a-f]{1,4}:+){3,7}[0-9a-f]{1,4}$ ]]; then
IPS+=("${ARG,,}")
fi
done
if [ ${#IPS[@]} -eq 0 ]; then
printf "\n%s\n" "Usage: sudo $0 ip [ip ...]" >&2
exit 1
fi
if [ "$EUID" -ne 0 ]; then
printf "\n%s\n" "This script requires sudo privileges." >&2
exit 1
fi
if ! command -v fail2ban-client > /dev/null 2>&1; then
printf "\n%s\n" "This script requires fail2ban-client!" >&2
exit 1
fi
JAILS=$(
fail2ban-client status \
| grep "Jail list" \
| sed -E 's/^[^:]+:[ \t]+//' \
| sed 's/,//g'
)
for JAIL in $JAILS; do
JAILSTATUS=$(fail2ban-client status "$JAIL" | grep -v File | grep -v "\\s0")
for IP in "${IPS[@]}"; do
if [[ "$JAILSTATUS" =~ .*[[:space:]]+"$IP"([[:space:]]|$)+.* ]]; then
RESULT=$(fail2ban-client set "$JAIL" unbanip "$IP")
if [ "$RESULT" = "1" ] || [ "$RESULT" = "$IP" ]; then
printf "Unbanned %s from jail %s\\n" "$IP" "$JAIL"
else
printf "Failed to unban %s from jail %s\\n" "$IP" "$JAIL" >&2
fi
fi
done
done
printf "\\n"
REJECTS=$(
iptables -L -n | awk '$1=="REJECT" && $4!="0.0.0.0/0"' \
&& ip6tables -L -n | awk '$1=="REJECT"'
)
for IP in "${IPS[@]}"; do
echo "$REJECTS" | grep " $IP "
done