-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist2bans.sh
executable file
·60 lines (54 loc) · 1.43 KB
/
list2bans.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
#!/bin/bash
# ------------------------------------------------------------------------------
# Lists all Fail2Ban jail statuses or jails banning an ip.
#
# Usage: sudo list2bans.sh [ip]
#
# Author : Esa Jokinen (oh2fih)
# Home : https://github.com/oh2fih/Misc-Scripts
# ------------------------------------------------------------------------------
if [ "$EUID" -ne 0 ]; then
echo "*** ERROR! This script requires sudo privileges."
exit 1
fi
if [ "$#" -gt 0 ]; then
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
IP="$1"
BANNED=()
else
if [[ ${1,,} =~ ^([0-9a-f]{1,4}:+){3,7}[0-9a-f]{1,4}$ ]]; then
IP="${1,,}"
BANNED=()
else
echo "Usage: sudo $0 [ip]"
exit 1
fi
fi
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")
if [ -z ${IP+x} ]; then
printf "\\n%s\\n" "$JAILSTATUS"
else
if [[ "$JAILSTATUS" =~ .*[[:space:]]+"$IP"([[:space:]]|$)+.* ]]; then
BANNED+=("$JAIL")
fi
fi
done
if [ -z ${IP+x} ]; then
printf "\\n"
else
if [ ${#BANNED[@]} -gt 0 ]; then
printf "\\n%s banned by jails: %s\\n\\n" "$IP" "${BANNED[*]}"
else
printf "\\n%s not banned\\n\\n" "$IP"
fi
fi
iptables -L -n | awk '$1=="REJECT" && $4!="0.0.0.0/0"' | grep " $IP "
ip6tables -L -n | awk '$1=="REJECT"' | grep " $IP "