-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgeo-filter_ip_2_cidr.sh
71 lines (58 loc) · 1.76 KB
/
geo-filter_ip_2_cidr.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
#!/bin/bash
# Tested on Debian Bullseye (11)
# shellcheck disable=SC2207,SC2086
# This script catches and converts an ip adres to the full CIDR
# of the network its in.
# This script can be use with the geo-filter.sh to
# block countries and there ip's CIDR from whois info.
# This prevents for example lots of IP numbers in firewalls
# where hackers have control with the CIDR Range.
###
VERSION=20220310-1.1
# we need to be root or sudo
if [[ $EUID -ne 0 ]]
then
echo "This script must be run as root or with sudo"
exit 1
fi
if [ "$#" -ne 1 ]
then
echo "Usage: $(basename "$0") <ip>" 1>&2
exit 0 # return true in case of config issue
fi
if [ ! "$(command -v netmask)" ]
then
apt install -y -q=2 netmask
fi
if [ ! "$(command -v whois)" ]
then
apt install -y -q=2 whois
fi
IFS=$'\n'
GET_IP_INFO=( $(whois "${1}" |grep -viE "mnt-routes|descr" | grep -iE "inetnum|route|cidr") )
unset IFS
# Count the array's.
array_count="${#GET_IP_INFO[@]}"
for ((i = 0; i != array_count; i++))
do
if [ -n "${GET_IP_INFO[i]}" ]
then
INETNUM="$(echo "${GET_IP_INFO[i]}"|grep -i -m 1 inetnum)"
ROUTE="$(echo "${GET_IP_INFO[i]}"|grep -i -m 1 route)"
CIDR="$(echo "${GET_IP_INFO[i]}"|grep -i -m 1 cidr)"
if [ -n "$CIDR" ]
then
USE_CIDR="$(echo "$CIDR"|awk '{ print $NF }')"
elif [ -n "$ROUTE" ]
then
USE_CIDR="$(echo "$ROUTE"|awk '{ print $NF }')"
elif [ -n "$INETNUM" ]
then
CALC_INETNUM1="$(echo $INETNUM|cut -d":" -f2 |cut -d" " -f2)"
CALC_INETNUM2="$(echo $INETNUM|cut -d":" -f2 |cut -d" " -f4)"
USE_CIDR="$(netmask -c $CALC_INETNUM1:$CALC_INETNUM2|awk '{ print $NF }')"
fi
fi
done
BLOCK_CIDR="$USE_CIDR"
echo "$BLOCK_CIDR"