-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblock_ip.sh
86 lines (71 loc) · 1.54 KB
/
block_ip.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/sh
#VERSION=1.0
BF=/root/blocked_ips.txt
EF=/root/exempt_ips.txt
# This is the default chain where shorewall puts it's manual entries
BLOCK_CHAIN=dynamic
curriptables()
{
echo "<br><br><textarea cols=160 rows=60>";
/usr/sbin/shorewall show dynamic
/usr/sbin/shorewall6 show dynamic
echo "</textarea>";
}
if [ "$ip" = "" ]; then
echo "No ip has been passed via env.";
exit 1;
fi
VALID4=`ip -4 route get $ip > /dev/null 2>&1`
RET4=$?
VALID6=`ip -6 route get $ip > /dev/null 2>&1`
RET6=$?
if [ $RET4 == "0" ]
then
IPVERSION="ipv4"
elif [ $RET6 == "0" ]
then
IPVERSION="ipv6"
else
IPVERSION="0"
fi
### Do we have a block file?
if [ ! -e "$BF" ]; then
echo "Cannot find $BF";
exit 1;
fi
### Do we have an exempt file?
if [ ! -e "$EF" ]; then
echo "Cannot find $EF";
exit 1;
fi
### Make sure it's not exempt
COUNT=`grep -c "^${ip}\$" $EF`;
if [ "$COUNT" -ne 0 ]; then
echo "$ip in the exempt list ($EF). Not blocking.";
curriptables
exit 2;
fi
### Make sure it's not alreaday blocked
COUNT=`grep -c "^${ip}=" $BF`;
if [ "$COUNT" -ne 0 ]; then
echo "$ip already exists in $BF ($COUNT). Not blocking.";
curriptables
exit 2;
fi
echo "Blocking $ip ...<br>";
echo "$ip=dateblocked=`date +%s`" >> $BF;
echo "Adding $ip into ${BLOCK_CHAIN} chain...";
if [ $IPVERSION == "ipv6" ]
then
/usr/sbin/shorewall6 drop $ip
elif [ $IPVERSION == "ipv4" ]
then
/usr/sbin/shorewall drop $ip
else
echo "$ip is not valid. Not blocking.";
curriptables
exit 2;
fi
echo "<br><br>Result:";
curriptables
exit 0;