forked from robynhub/kit-censura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-routes-linux
executable file
·68 lines (55 loc) · 2.3 KB
/
install-routes-linux
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
#!/bin/bash
# This script assumes a Linux system and something (like quagga) which will
# propagate as null routes the local static routes having a specific nexthop.
# It will sync these static routes with the ones in the list(s) by adding and
# removing local routes as needed.
# Set $DRY_RUN to only get a summary of the changes which would be applied.
source config.sh
test $LOGGING_ENABLE == true && echo "$(date '+%d/%m/%y %H:%m:%S') - Install Routes Linux Started" >> $LOGFILE
##############################################################################
if [ -t 0 ]; then VERBOSE=1; fi
if [ "$DRY_RUN" ]; then VERBOSE=1; fi
dprintf() {
[ "$VERBOSE" ] || return 0
printf "$*"
}
##############################################################################
list_routes() {
local ip="$1"
ip -o route list | awk "/ via $ip / {print \$1}"
}
##############################################################################
list_routes $BLACKHOLE_NEXTHOP | sort > tmp/routes.installed
for list in $LISTS; do
test -e $LISTS_DIR/${list}-ip && grep '^[^#]' $LISTS_DIR/${list}-ip
done | sort | uniq > tmp/routes.new
comm -13 tmp/routes.installed tmp/routes.new > tmp/routes.added
comm -23 tmp/routes.installed tmp/routes.new > tmp/routes.removed
if [ -s tmp/routes.added ]; then
dprintf "Routes to add:\n"
while read ip; do
dprintf "$ip\n"
test $LOGGING_ENABLE == true && echo "$(date '+%d/%m/%y %H:%m:%S') - Added route for IP $ip" >> $LOGFILE
[ "$DRY_RUN" ] && continue
ip route add "$ip" via $BLACKHOLE_NEXTHOP
if [ $? != 0 ] ; then
test $LOGGING_ENABLE == true && echo "$(date '+%d/%m/%y %H:%m:%S') - ERROR: Cannot add a route for IP $ip" >> $LOGFILE
echo "Cannot add a route for $ip!" >&2
fi
done < tmp/routes.added
fi
if [ -s tmp/routes.removed ]; then
dprintf "Routes to remove:\n"
while read ip; do
dprintf "$ip\n"
test $LOGGING_ENABLE == true && echo "$(date '+%d/%m/%y %H:%m:%S') - Removed route for IP $ip" >> $LOGFILE
[ "$DRY_RUN" ] && continue
ip route del "$ip" via $BLACKHOLE_NEXTHOP
if [ $? != 0 ] ; then
test $LOGGING_ENABLE == true && echo "$(date '+%d/%m/%y %H:%m:%S') - ERROR: Cannot remove a route for IP $ip" >> $LOGFILE
echo "Cannot remote the route for $ip!" >&2
fi
done < tmp/routes.removed
fi
rm -f tmp/routes.*
exit 0