-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract-banned-ips_V.1.1.0.sh
62 lines (54 loc) · 1.48 KB
/
extract-banned-ips_V.1.1.0.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
#!/bin/sh
# Bash script to xxtract already fail-banned ips
# by Martin Wolfert
# https://wp-loft.de
# V.1.1.0
# 2014/08/28
# Original from - methurt:
# http://www.fail2ban.org/wiki/index.php/Fail2ban:Community_Portal#Question_about_persistent_IP_address_bans_over_restart
### Some variables ###
GREP=`which grep`
SED=`which sed`
F2BCL=`which fail2ban-client`
CHMOD=`which chmod`
RUNDIR="/root/fail2ban-adds"
BANNED_IPS="${RUNDIR}/fail2ban-manual.list"
# Function to save the fail2ban rules
do_save()
{
if [ -f ${BANNED_IPS} ]; then
rm ${BANNED_IPS}
fi
jails=$(${F2BCL} status | ${GREP} Jail list: | ${SED} 's/.*Jail list:t+//;s/,//g')
for jail in ${jails}; do
for ip in $(${F2BCL} status ${jail}| ${GREP} IP list| ${SED} 's/.*IP list:t//'); do
echo "fail2ban-client set ${jail} banip ${ip} ">> ${BANNED_IPS}
done
done
${CHMOD} 755 ${BANNED_IPS}
return 0
}
# Function to restore the fail2ban rules
do_restore()
{
if [ ! -f ${BANNED_IPS} ]; then
echo "Hey dude ... first you have to save the fail2ban rules ! :-)"
exit 3
fi
${BANNED_IPS} >/dev/null 2>&1
return 0
}
# Doing
case "$1" in
save)
do_save
;;
restore)
do_restore
;;
*)
echo "Usage $0 {save|restore}"
exit 3
;;
esac
exit 0