-
Notifications
You must be signed in to change notification settings - Fork 6
/
filter.sh
executable file
·226 lines (207 loc) · 5.04 KB
/
filter.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# filter.sh: Generates a filter list for a router based on an input AS-SET or ASN.
# Copyright 2009-2011 Matthew Walster
# Distributed under the terms of the GNU General Public Licence
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Define a usage statement
usage()
{
echo "$0: A filterlist generator"
echo "Usage: $0 [OPTS] AS-SET"
echo " -t | --type [ juniper | cisco | brocade | force10 | redback | quagga | iosxr ]"
echo " -n | --name [ Filter Name ]"
echo " -h | --host [ WHOIS server ]"
echo " --ipv4"
echo " --ipv6"
}
# Initialise some variables, to make it safe to use
FILTERNAME="filter"
SEQNUM=10
INC=5
IP_LIST=""
WHOISSERVER="whois.radb.net"
IP_VERSION="4"
CRAZY="0"
# Parse the command line options
while [[ $1 = -* ]]; do
case "$1" in
-t|--type)
TYPE="$2"
shift 2
;;
-n|--name)
FILTERNAME="$2"
shift 2
;;
-h|--host)
WHOISSERVER="$2"
shift 2
;;
-s|--seq)
SEQNUM="$2"
INC="$3"
shift 3
;;
--ipv4)
IP_VERSION="4"
shift
;;
--ipv6)
IP_VERSION="6"
shift
;;
--crazy)
CRAZY="1"
shift
;;
--help)
usage
exit 1
;;
*)
echo "Error: Unknown option: $1" >&2
usage
exit 1
;;
esac
done
# If no arguments, then just show the usage statement
if [[ $# -lt 1 ]]
then usage
exit 1
fi
# Do we have an AS-SET or an ASN?
IS_SET=$(whois -h whois.radb.net $1 | grep -i ^as-set: | awk -F: '{print $1}')
# If we've got an AS-SET, use the handy !i and ,1 commands on RADB
if [[ "as-set" == "$IS_SET" ]]
then
AS_LIST=$(whois -h whois.radb.net \!i$1,1 | sed '/^\[/d' | sed 2\!d)
else
AS_LIST=$1
fi
if [[ $CRAZY == 0 ]]
then
# Find out which prefixes are contained within that AS number
for i in $AS_LIST
do
case "$IP_VERSION" in
4)
IP_LIST_UNSORTED+=$(whois -h $WHOISSERVER -- "-i origin $i" | grep ^route: | cut -f 2 -d: | sed 's/ //g')
;;
6)
IP_LIST_UNSORTED+=$(whois -h $WHOISSERVER -- "-i origin $i" | grep ^route6: | cut -f 2- -d: | sed 's/ //g')
;;
esac
IP_LIST_UNSORTED+=$(echo " ")
done
elif [[ $CRAZY == 1 ]]
then
tmpfile=$(mktemp /tmp/filter.XXXXXXXX) || exit 1
case "$IP_VERSION" in
4)
curl ftp://ftp.ripe.net/ripe/dbase/split/ripe.db.route.gz \
| gunzip -dc \
| grep -e '^route:' -e '^origin:' \
| sed -e 'N;s/\n/ /' -re 's/route:\s+//g;s/origin:\s+//g' \
>$tmpfile
GREP_OPTS=$(sed -re 's/AS/ -e AS/g' <<<$AS_LIST)
IP_LIST_UNSORTED=$(grep $GREP_OPTS $tmpfile | cut -f1 -d" ")
;;
6)
curl ftp://ftp.ripe.net/ripe/dbase/split/ripe.db.route6.gz \
| gunzip -dc \
| grep -e '^route:' -e '^origin:' \
| sed -e 'N;s/\n/ /' -re 's/route6:\s+//g;s/origin:\s+//g' \
>$tmpfile
GREP_OPTS=$(sed -re 's/AS/ -e AS/g' <<<$AS_LIST)
IP_LIST_UNSORTED=$(grep $GREP_OPTS $tmpfile | cut -f1 -d" ")
;;
esac
rm $tmpfile
fi
# Remove duplicate routes
IP_LIST=$(printf "%s\n" $IP_LIST_UNSORTED | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n | uniq)
# If we're on Force10 or Redback (which uses similar syntax), create the prefix-list
if [[ "$TYPE" == "force10" || "$TYPE" == "redback" ]]
then
echo "ip prefix-list $FILTERNAME"
elif [[ "$TYPE" == "iosxr" ]]
then
echo "prefix-set $FILTERNAME"
fi
FIRST=1
# Format the output nicely
for i in $IP_LIST
do
case "$TYPE" in
juniper)
echo "set policy-options policy-statement $FILTERNAME term auto-generated from route-filter $i exact"
;;
cisco)
if [[ "$IP_VERSION" == "4" ]]
then
echo "ip prefix-list $FILTERNAME seq $SEQNUM permit $i"
let SEQNUM=SEQNUM+$INC
elif [[ "$IP_VERSION" == "6" ]]
then
echo "ipv6 prefix-list $FILTERNAME seq $SEQNUM permit $i"
let SEQNUM=SEQNUM+$INC
fi
;;
brocade)
echo "ip prefix-list $FILTERNAME permit $i"
;;
force10)
echo " seq $SEQNUM permit $i"
let SEQNUM=SEQNUM+$INC
;;
redback)
echo " seq $SEQNUM permit $i"
let SEQNUM=SEQNUM+$INC
;;
iosxr)
if [[ $FIRST -eq 0 ]];
then
echo ","
fi
echo -n "$i"
;;
quagga)
if [[ "$IP_VERSION" == "4" ]]
then
echo "ip prefix-list $FILTERNAME seq $SEQNUM permit $i"
let SEQNUM=SEQNUM+$INC
elif [[ "$IP_VERSION" == "6" ]]
then
echo "ipv6 prefix-list $FILTERNAME seq $SEQNUM permit $i"
let SEQNUM=SEQNUM+$INC
fi
;;
*)
echo $i
;;
esac
FIRST=0
done
if [[ "$TYPE" == "iosxr" ]]
then
echo ""
echo "end-set"
echo "!"
fi
# Tell the Juniper router to accept those prefixes
if [[ "$TYPE" == "juniper" ]]
then
echo "set policy-options policy-statement $FILTERNAME term auto-generated then accept"
fi