jablonka.czprosek.czf

sedlo

Subversion Repositories:
[/] [trunk/] [sedlo] - Blame information for rev 10

 

Line No. Rev Author Line
17simandl#!/bin/bash
2# author : Petr Simandl www.simandl.cz
310simandl# release date : 26/01/2006
47simandl# name : sedlo
5# description : dynamic side routing tables tool
6# license : GPL
7 
810simandlsl_version="0.0.4pre1"
97simandl 
10PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
11 
12sl_nmcnf="sedlo.conf"
13sl_sedlocnf="/etc/$sl_nmcnf"
14sl_sedlocache="/var/cache/sedlo"
15 
16sl_rttab="/etc/iproute2/rt_tables"
17sl_rtnmin=110
18sl_rtnmax=200
19sl_ipnodef="10.0.0.0/8"
20 
21slm_unknown="Nezname parametry : "
22 
23sl_ipcmd=`which ip`
24sl_trcmd=`which tr`
25sl_wgetcmd=`which wget`
26sl_hnmcmd=`which hostname`
27sl_awkcmd=`which awk`
28sl_catcmd=`which cat`
29sl_grepcmd=`which grep`
3010simandlsl_diffcmd=`which diff`
317simandl 
32if [ -e $sl_sedlocnf ]
33 then
34 sl_nop=1
35 else
36 echo "$sl_sedlocnf not found"
37 exit 1
38fi
39 
40if [ -e $sl_rttab ]
41 then
42 sl_nop=1
43 else
44 echo "$sl_rttab not found"
45 exit 1
46fi
47 
489simandlsl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2" "$3" "$4}'`
497simandl 
50######################################################################
518simandls_maru()
527simandl{
53if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi
54 
55sl_rules=`($sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_awkcmd '{print $3"*ru"}' ; \
56$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*ip"}') | sort | uniq`
57 
58sl_merged=`echo $sl_rules $sl_ips | sort | uniq`
59 
60echo $sl_merged
61 
628simandl} # s_maru
637simandl######################################################################
64s_flru()
65{
66if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi
67 
68sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'`
69 
70for sl_rule in $sl_rules
71do
72# sl_ipn=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2}'`
73 sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'`
74 $sl_ipcmd ru del $sl_ipgws
75done
76 
778simandl#this should make faster applying of new routing tables
78$sl_ipcmd ro flush cache
79 
807simandl} # s_flru
818simandl 
827simandl######################################################################
8310simandl# here we get each ip and we create a rule to send this ip to a
84# certain table
85# this routine can be skipped when the number of ips and ip directions
86# are still the same = old and new configs are the same
877simandls_fillrules()
88{
898simandl 
90#flush old rules before filling new ones
91#not so nice solution - it is planned to change just what's necessary
92#by s_maru
937simandls_flru
94 
9510simandlif [ $scm_info -gt 0 ]; then echo "Creating rules for ips" ; fi
967simandl 
97sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`
98 
99for sl_ip in $sl_ips
100do
101 sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'`
102 sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'`
103 sl_ok="no"
104 for sl_ipgw in $sl_ipgws
105 do
106 sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw`
107 if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ]
108 then
109 if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi
110 $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw
111 sl_ok="yes"
112 else
113 if [ "$sl_ok" = "no" ]
114 then
115 if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi
116 else
117 if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi
118 fi
119 fi
120 done
121done
122 
123$sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main
124 
1258simandl#this should make faster applying of new routing tables
126$sl_ipcmd ro flush cache
127 
1287simandl} # s_fillrules
1298simandl 
1307simandl######################################################################
13110simandl# here we look into the main routing table for path to our iGWs
132# and we fill these tables with two halves default nets that
133# point to appropriate direction
134# this routine can be skipped when the routing table is the same
1357simandls_filltables()
136{
13710simandlif [ $scm_info -gt 0 ]; then echo "Checking main routing table" ; fi
138 
1397simandlif [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi
140 
1419simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3"*"$2"*"$1}'`
1427simandl 
143for sl_igw in $sl_igws
144 do
145 sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'`
146 sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'`
1479simandl sl_igwtype=`echo $sl_igw | $sl_awkcmd -F '*' '{print $3}'`
1487simandl sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'`
149 
1509simandl # equal cost multipath detection - just first IP is taken as way to igw
1517simandl if [ "$sl_igwgt x" = "zebra x" ]
152 then
153 sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'`
154 fi
155 
1569simandl #if myigw then fill table for local gateway with single ip from config
157 if [ "$sl_igwtype x" = "myigw x" ]
158 then
159 sl_igwgt=$sl_igwip
160 fi
161 
1627simandl if [ "$sl_igwgt x" = " x" ]
163 then
164 if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi
165 else
166 sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
167 if [ "$sl_tbl x" != " x" ]
168 then
169 $sl_ipcmd ro fl ta $sl_igwn
170 fi
171 $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn
172 $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn
1739simandl if [ "$sl_igwtype x" = "myigw x" ]
174 then
175 if [ $scm_info -gt 1 ]; then echo "Table filled for myigw $sl_igwn" ; fi
176 else
177 if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi
178 fi
1797simandl fi
180 done
181 
182} # s_filltables
1838simandl 
1847simandl######################################################################
18510simandl# filling rttab with tables from config
186# only new tables are created with a new uniq number that is not important because
187# usually we handle tables just by their names
188# this routine acts only when a new iGW appears - only adding a table is supported
189# no deleting is implemented because it seems to be not necessary to delete an old table
190# because there is space enough and after reboot table will not be created
1917simandls_mktables()
192{
19310simandlif [ $scm_info -gt 0 ]; then echo "Checking tables" ; fi
194 
195if [ $scm_info -gt 0 ]; then echo "Creating tables" ; fi
1969simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3}'`
1977simandlfor sl_igw in $sl_igws
198 do
199 sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw `
200 if [ "$sl_igwrttb x" = " x" ]
201 then
202 if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi
203 sl_cnt="$sl_rtnmax"
204 sl_ok="no"
205 until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ]
206 do
207 #space is used to recognized two and three digit numbers
208 sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " `
209 if [ "$sl_igwrttb x" = " x" ]
210 then
211 sl_ok="yes"
212 echo "$sl_cnt $sl_igw" >> $sl_rttab
213 fi
214 sl_cnt=$(($sl_cnt - 1 ))
215 done
216 else
217 if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi
218 fi
2199simandl done
2207simandl 
221} # s_mktables
2228simandl 
2237simandl######################################################################
224s_getcfg()
225{
226if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
227if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
228if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi
229 
23010simandlrm -f "$sl_sedlocache/$sl_nmcnf.main.tmp"
2317simandl 
23210simandl$sl_wgetcmd -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp"
233 
2347simandlif [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ]
235 then
236 date > $sl_sedlocache/last_getcnf.txt
237 cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main
23810simandl if [ $scm_info -gt 1 ]; then echo "Main config downloaded and accepted" ; fi
2397simandl else
2408simandl if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi
241 echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt
2427simandl date >> $sl_sedlocache/last_getcnf.txt
243fi
244 
24510simandl# before generating a new cached config we store the old one for
246# comparison with the new one
247rm -f "$sl_sedlocache/$sl_nmcnf.old"
248if [ -s $sl_sedlocache/$sl_nmcnf ]
249 then
250 cp $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old
251 else
252 touch $sl_sedlocache/$sl_nmcnf.old
253fi
254 
2557simandl# preparing cached config from local and main
256# the local config should be processed as the second to have
257# higher priority for rules from local config
258echo "# generated file" > $sl_sedlocache/$sl_nmcnf
259for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf`
260do
261cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
262cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
263cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
264done
2658simandl 
266#local gateways taken from local config
267cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
268 
2697simandl#cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq
270#mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf
271 
27210simandlsl_diffcfg=`diff $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old | grep -c .`
273if [ $sl_diffcfg -gt 0 ]
274 then
275 if [ $scm_info -gt 0 ]; then echo "New config is different than the old one" ; fi
276 else
277 if [ $scm_info -gt 0 ]; then echo "New config is the same as the old one" ; fi
278fi
279 
280# to detect first start after reboot or rule flushig we compare config with rule number
281# when we have less rules than ips in config then
282sl_numru=`ip ru ls | grep -c lookup`
283sl_numip=`grep -c ^ip $sl_sedlocache/$sl_nmcnf`
284sl_numru=$(($sl_numru - 3 ))
285if [ $sl_numip -gt $sl_numru ]
286 then
287 if [ $scm_info -gt 0 ]; then echo "We have less rules ($sl_numru) than new config has ips ($sl_numip)" ; fi
288 sl_diffcfg="1"
289 else
290 if [ $scm_info -gt 0 ]; then echo "We have $sl_numru rules and $sl_numip ips" ; fi
291fi
292 
2937simandl}
2948simandl 
2957simandl######################################################################
296s_version()
297{
298 echo sedlo $sl_version
299} # s_version
3008simandl 
3017simandl######################################################################
302s_report()
303{
3049simandl echo Content-type: text/html
305 echo
306 echo "Sedlo na routeru `hostname`"
307 echo "<pre>"
3087simandl echo "##### SEDLO #####"
309 echo "date : `date`"
310 echo "version : $sl_version"
311 echo "local_config : $sl_sedlocnf"
3129simandl echo "main_config : <a href=\"$sl_murlcfg\">$sl_murlcfg</a>"
3137simandl echo "last update : `cat $sl_sedlocache/last_getcnf.txt`"
314 echo "##### TABLES #####"
315 cat $sl_rttab
3169simandl echo ; echo "##### DEFAULT ROUTES IN TABLES #####"
317 $sl_ipcmd ro ls ta all | $sl_grepcmd table | $sl_grepcmd -v local | $sl_trcmd " " "\t"
318 echo ; echo "##### RULES FOR IPS #####"
319 $sl_ipcmd ru ls | $sl_trcmd " " "\t"
320 echo "</pre>"
3217simandl} # s_report
3228simandl 
3239simandl 
324 
3257simandl######################################################################
326s_help()
327{
328 echo Pouziti: sedlo [param]
329 echo param:
330 echo -V vypise verzi
331 echo -help vypise napovedu
332 echo -v malo upovidany
333 echo -vv hodne upovidany
334 echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
335 echo -report vypise prehled pravidel a tabulek
336 echo -flru odstrani vsechny pravidla
337} # s_help
3388simandl 
3397simandl######################################################################
340######################################################################
341 
342sl_unknown=""
343scm_nogetcfg=0
344scm_flru=0
345scm_info=0
346 
347# parsing input parameters
348while [ "a$1" != "a" ]
349do
350 case $1 in
351 -V)
352 s_version
353 exit 0
354 ;;
355 -h)
356 s_help
357 exit 0
358 ;;
359 -report)
360 s_report
361 exit 0
362 ;;
363 -flru)
364 scm_flru=1
365 shift
366 ;;
367 -help)
368 s_help
369 exit 0
370 ;;
371 -nogetcfg)
372 scm_nogetcfg=1
373 shift
374 ;;
375 -v)
376 scm_info=1
377 shift
378 ;;
379 -vv)
380 scm_info=2
381 shift
382 ;;
383 *)
384 sl_unknown="$sl_unknown$1 "
385 shift
386 esac
387done
388 
389# printing the list of bad parameters (if there are some)
390if [ "a$sl_unknown" != "a" ]
391then
392 echo "$slm_unknown $sl_unknown"
393 s_help
394 exit 0
395fi
396 
397if [ $scm_flru -eq 1 ]
398then
399 s_flru
400 exit 0
401fi
402 
403if [ $scm_nogetcfg -eq 0 ]
404then
405 s_getcfg
406fi
407 
40810simandl 
4097simandls_mktables
410s_filltables
411 
41210simandlif [ $sl_diffcfg -gt 0 ]
413then
414 s_fillrules
415fi
416 
4177simandlexit 0
418 
419 

Powered by WebSVN 2.2.1