1 | 7 | simandl | #!/bin/bash |
2 | | | # author : Petr Simandl www.simandl.cz |
3 | | | # release date : 07/09/2004 |
4 | | | # name : sedlo |
5 | | | # description : dynamic side routing tables tool |
6 | | | # license : GPL |
7 | | | |
8 | 9 | simandl | sl_version="0.0.3pre9" |
9 | 7 | simandl | |
10 | | | PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin |
11 | | | |
12 | | | sl_nmcnf="sedlo.conf" |
13 | | | sl_sedlocnf="/etc/$sl_nmcnf" |
14 | | | sl_sedlocache="/var/cache/sedlo" |
15 | | | |
16 | | | sl_rttab="/etc/iproute2/rt_tables" |
17 | | | sl_rtnmin=110 |
18 | | | sl_rtnmax=200 |
19 | | | sl_ipnodef="10.0.0.0/8" |
20 | | | |
21 | | | slm_unknown="Nezname parametry : " |
22 | | | |
23 | | | sl_ipcmd=`which ip` |
24 | | | sl_trcmd=`which tr` |
25 | | | sl_wgetcmd=`which wget` |
26 | | | sl_hnmcmd=`which hostname` |
27 | | | sl_awkcmd=`which awk` |
28 | | | sl_catcmd=`which cat` |
29 | | | sl_grepcmd=`which grep` |
30 | | | |
31 | | | if [ -e $sl_sedlocnf ] |
32 | | | then |
33 | | | sl_nop=1 |
34 | | | else |
35 | | | echo "$sl_sedlocnf not found" |
36 | | | exit 1 |
37 | | | fi |
38 | | | |
39 | | | if [ -e $sl_rttab ] |
40 | | | then |
41 | | | sl_nop=1 |
42 | | | else |
43 | | | echo "$sl_rttab not found" |
44 | | | exit 1 |
45 | | | fi |
46 | | | |
47 | 9 | simandl | sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2" "$3" "$4}'` |
48 | 7 | simandl | |
49 | | | ###################################################################### |
50 | 8 | simandl | s_maru() |
51 | 7 | simandl | { |
52 | | | if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi |
53 | | | |
54 | | | sl_rules=`($sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_awkcmd '{print $3"*ru"}' ; \ |
55 | | | $sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*ip"}') | sort | uniq` |
56 | | | |
57 | | | sl_merged=`echo $sl_rules $sl_ips | sort | uniq` |
58 | | | |
59 | | | echo $sl_merged |
60 | | | |
61 | 8 | simandl | } # s_maru |
62 | 7 | simandl | ###################################################################### |
63 | | | s_flru() |
64 | | | { |
65 | | | if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi |
66 | | | |
67 | | | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'` |
68 | | | |
69 | | | for sl_rule in $sl_rules |
70 | | | do |
71 | | | # sl_ipn=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2}'` |
72 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
73 | | | $sl_ipcmd ru del $sl_ipgws |
74 | | | done |
75 | | | |
76 | 8 | simandl | #this should make faster applying of new routing tables |
77 | | | $sl_ipcmd ro flush cache |
78 | | | |
79 | 7 | simandl | } # s_flru |
80 | 8 | simandl | |
81 | 7 | simandl | ###################################################################### |
82 | | | s_fillrules() |
83 | | | { |
84 | 8 | simandl | |
85 | | | #flush old rules before filling new ones |
86 | | | #not so nice solution - it is planned to change just what's necessary |
87 | | | #by s_maru |
88 | 7 | simandl | s_flru |
89 | | | |
90 | | | if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi |
91 | | | |
92 | | | sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
93 | | | |
94 | | | for sl_ip in $sl_ips |
95 | | | do |
96 | | | sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
97 | | | sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
98 | | | sl_ok="no" |
99 | | | for sl_ipgw in $sl_ipgws |
100 | | | do |
101 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
102 | | | if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
103 | | | then |
104 | | | if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi |
105 | | | $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw |
106 | | | sl_ok="yes" |
107 | | | else |
108 | | | if [ "$sl_ok" = "no" ] |
109 | | | then |
110 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
111 | | | else |
112 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
113 | | | fi |
114 | | | fi |
115 | | | done |
116 | | | done |
117 | | | |
118 | | | $sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main |
119 | | | |
120 | 8 | simandl | #this should make faster applying of new routing tables |
121 | | | $sl_ipcmd ro flush cache |
122 | | | |
123 | 7 | simandl | } # s_fillrules |
124 | 8 | simandl | |
125 | 7 | simandl | ###################################################################### |
126 | | | s_filltables() |
127 | | | { |
128 | | | if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
129 | | | |
130 | 9 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3"*"$2"*"$1}'` |
131 | 7 | simandl | |
132 | | | for sl_igw in $sl_igws |
133 | | | do |
134 | | | sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
135 | | | sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
136 | 9 | simandl | sl_igwtype=`echo $sl_igw | $sl_awkcmd -F '*' '{print $3}'` |
137 | 7 | simandl | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'` |
138 | | | |
139 | 9 | simandl | # equal cost multipath detection - just first IP is taken as way to igw |
140 | 7 | simandl | if [ "$sl_igwgt x" = "zebra x" ] |
141 | | | then |
142 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'` |
143 | | | fi |
144 | | | |
145 | 9 | simandl | #if myigw then fill table for local gateway with single ip from config |
146 | | | if [ "$sl_igwtype x" = "myigw x" ] |
147 | | | then |
148 | | | sl_igwgt=$sl_igwip |
149 | | | fi |
150 | | | |
151 | 7 | simandl | if [ "$sl_igwgt x" = " x" ] |
152 | | | then |
153 | | | if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi |
154 | | | else |
155 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
156 | | | if [ "$sl_tbl x" != " x" ] |
157 | | | then |
158 | | | $sl_ipcmd ro fl ta $sl_igwn |
159 | | | fi |
160 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
161 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
162 | 9 | simandl | if [ "$sl_igwtype x" = "myigw x" ] |
163 | | | then |
164 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for myigw $sl_igwn" ; fi |
165 | | | else |
166 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi |
167 | | | fi |
168 | 7 | simandl | fi |
169 | | | done |
170 | | | |
171 | 8 | simandl | #this should make faster applying of new routing tables |
172 | | | $sl_ipcmd ro flush cache |
173 | | | |
174 | 7 | simandl | } # s_filltables |
175 | 8 | simandl | |
176 | 7 | simandl | ###################################################################### |
177 | | | s_mktables() |
178 | | | { |
179 | | | if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi |
180 | 9 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3}'` |
181 | 7 | simandl | for sl_igw in $sl_igws |
182 | | | do |
183 | | | sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
184 | | | if [ "$sl_igwrttb x" = " x" ] |
185 | | | then |
186 | | | if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
187 | | | sl_cnt="$sl_rtnmax" |
188 | | | sl_ok="no" |
189 | | | until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
190 | | | do |
191 | | | #space is used to recognized two and three digit numbers |
192 | | | sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
193 | | | if [ "$sl_igwrttb x" = " x" ] |
194 | | | then |
195 | | | sl_ok="yes" |
196 | | | echo "$sl_cnt $sl_igw" >> $sl_rttab |
197 | | | fi |
198 | | | sl_cnt=$(($sl_cnt - 1 )) |
199 | | | done |
200 | | | else |
201 | | | if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
202 | | | fi |
203 | 9 | simandl | done |
204 | 7 | simandl | |
205 | | | } # s_mktables |
206 | 8 | simandl | |
207 | 7 | simandl | ###################################################################### |
208 | | | s_getcfg() |
209 | | | { |
210 | | | if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
211 | | | if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
212 | | | if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
213 | | | |
214 | 8 | simandl | wget -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" |
215 | 7 | simandl | |
216 | | | if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ] |
217 | | | then |
218 | | | date > $sl_sedlocache/last_getcnf.txt |
219 | | | cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main |
220 | | | if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi |
221 | | | else |
222 | 8 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi |
223 | | | echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt |
224 | 7 | simandl | date >> $sl_sedlocache/last_getcnf.txt |
225 | | | fi |
226 | | | |
227 | | | # preparing cached config from local and main |
228 | | | # the local config should be processed as the second to have |
229 | | | # higher priority for rules from local config |
230 | | | |
231 | | | echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
232 | | | for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf` |
233 | | | do |
234 | | | cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
235 | | | cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
236 | | | cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
237 | | | done |
238 | 8 | simandl | |
239 | | | #local gateways taken from local config |
240 | | | cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
241 | | | |
242 | 7 | simandl | #cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
243 | | | #mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
244 | | | |
245 | | | } |
246 | 8 | simandl | |
247 | 7 | simandl | ###################################################################### |
248 | | | s_version() |
249 | | | { |
250 | | | echo sedlo $sl_version |
251 | | | } # s_version |
252 | 8 | simandl | |
253 | 7 | simandl | ###################################################################### |
254 | | | s_report() |
255 | | | { |
256 | 9 | simandl | echo Content-type: text/html |
257 | | | echo |
258 | | | echo "Sedlo na routeru `hostname`" |
259 | | | echo "<pre>" |
260 | 7 | simandl | echo "##### SEDLO #####" |
261 | | | echo "date : `date`" |
262 | | | echo "version : $sl_version" |
263 | | | echo "local_config : $sl_sedlocnf" |
264 | 9 | simandl | echo "main_config : <a href=\"$sl_murlcfg\">$sl_murlcfg</a>" |
265 | 7 | simandl | echo "last update : `cat $sl_sedlocache/last_getcnf.txt`" |
266 | | | echo "##### TABLES #####" |
267 | | | cat $sl_rttab |
268 | 9 | simandl | echo ; echo "##### DEFAULT ROUTES IN TABLES #####" |
269 | | | $sl_ipcmd ro ls ta all | $sl_grepcmd table | $sl_grepcmd -v local | $sl_trcmd " " "\t" |
270 | | | echo ; echo "##### RULES FOR IPS #####" |
271 | | | $sl_ipcmd ru ls | $sl_trcmd " " "\t" |
272 | | | echo "</pre>" |
273 | 7 | simandl | } # s_report |
274 | 8 | simandl | |
275 | 9 | simandl | |
276 | | | |
277 | 7 | simandl | ###################################################################### |
278 | | | s_help() |
279 | | | { |
280 | | | echo Pouziti: sedlo [param] |
281 | | | echo param: |
282 | | | echo -V vypise verzi |
283 | | | echo -help vypise napovedu |
284 | | | echo -v malo upovidany |
285 | | | echo -vv hodne upovidany |
286 | | | echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
287 | | | echo -report vypise prehled pravidel a tabulek |
288 | | | echo -flru odstrani vsechny pravidla |
289 | | | } # s_help |
290 | 8 | simandl | |
291 | 7 | simandl | ###################################################################### |
292 | | | ###################################################################### |
293 | | | |
294 | | | sl_unknown="" |
295 | | | scm_nogetcfg=0 |
296 | | | scm_flru=0 |
297 | | | scm_info=0 |
298 | | | |
299 | | | # parsing input parameters |
300 | | | while [ "a$1" != "a" ] |
301 | | | do |
302 | | | case $1 in |
303 | | | -V) |
304 | | | s_version |
305 | | | exit 0 |
306 | | | ;; |
307 | | | -h) |
308 | | | s_help |
309 | | | exit 0 |
310 | | | ;; |
311 | | | -report) |
312 | | | s_report |
313 | | | exit 0 |
314 | | | ;; |
315 | | | -flru) |
316 | | | scm_flru=1 |
317 | | | shift |
318 | | | ;; |
319 | | | -help) |
320 | | | s_help |
321 | | | exit 0 |
322 | | | ;; |
323 | | | -nogetcfg) |
324 | | | scm_nogetcfg=1 |
325 | | | shift |
326 | | | ;; |
327 | | | -v) |
328 | | | scm_info=1 |
329 | | | shift |
330 | | | ;; |
331 | | | -vv) |
332 | | | scm_info=2 |
333 | | | shift |
334 | | | ;; |
335 | | | *) |
336 | | | sl_unknown="$sl_unknown$1 " |
337 | | | shift |
338 | | | esac |
339 | | | done |
340 | | | |
341 | | | # printing the list of bad parameters (if there are some) |
342 | | | if [ "a$sl_unknown" != "a" ] |
343 | | | then |
344 | | | echo "$slm_unknown $sl_unknown" |
345 | | | s_help |
346 | | | exit 0 |
347 | | | fi |
348 | | | |
349 | | | if [ $scm_flru -eq 1 ] |
350 | | | then |
351 | | | s_flru |
352 | | | exit 0 |
353 | | | fi |
354 | | | |
355 | | | if [ $scm_nogetcfg -eq 0 ] |
356 | | | then |
357 | | | s_getcfg |
358 | | | fi |
359 | | | |
360 | | | s_mktables |
361 | | | s_filltables |
362 | | | s_fillrules |
363 | | | |
364 | | | exit 0 |
365 | | | |
366 | | | |