jablonka.czprosek.czf

sedlo

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

 

Line No. Rev Author Line
17simandl#!/bin/bash
2# author : Petr Simandl www.simandl.cz
311simandl# release date : 06/08/2006
47simandl# name : sedlo
5# description : dynamic side routing tables tool
6# license : GPL
7 
811simandlsl_version="0.0.4pre2"
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
16111simandl 
162 #testing if the igw has not a route in global routing table
1637simandl if [ "$sl_igwgt x" = " x" ]
164 then
16511simandl if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn" ; fi
166 sl_myigw=`cat $sl_sedlocnf | $sl_grepcmd "^myigw" | $sl_grepcmd $sl_igwn | $sl_awkcmd '{print $3}'`
167 #testing if the igw without route is in local config
168 #if not we go to flush its table and set flag to redo rules
169 if [ "$sl_myigw x" = " x" ]
170 then
171 #getting num of routes of igw
172 sl_igwnr=`$sl_ipcmd ro ls ta all | $sl_grepcmd -c $sl_igwn`
173 if [ "$sl_igwnr x" = "0 x" ]
174 then
175 if [ $scm_info -gt 1 ]; then echo "Igw $sl_igwn is already empty - no action taken" ; fi
176 else
177 if [ $scm_info -gt 1 ]; then echo "Igw $sl_igwn will be flushed and rules rearranged" ; fi
178 $sl_ipcmd ro fl ta $sl_igwn
179 #because this igw dissapeared we set a flag for rules recreation
180 sl_diffigw=1
181 fi
182 else
183 echo "Igw $sl_igwn found in local config - leaving table as is"
184 fi
185 
1867simandl else
187 sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
188 if [ "$sl_tbl x" != " x" ]
189 then
190 $sl_ipcmd ro fl ta $sl_igwn
191 fi
192 $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn
193 $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn
1949simandl if [ "$sl_igwtype x" = "myigw x" ]
195 then
196 if [ $scm_info -gt 1 ]; then echo "Table filled for myigw $sl_igwn" ; fi
197 else
198 if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi
199 fi
2007simandl fi
201 done
202 
203} # s_filltables
2048simandl 
2057simandl######################################################################
20610simandl# filling rttab with tables from config
207# only new tables are created with a new uniq number that is not important because
208# usually we handle tables just by their names
209# this routine acts only when a new iGW appears - only adding a table is supported
210# no deleting is implemented because it seems to be not necessary to delete an old table
211# because there is space enough and after reboot table will not be created
2127simandls_mktables()
213{
21410simandlif [ $scm_info -gt 0 ]; then echo "Checking tables" ; fi
215 
2169simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3}'`
2177simandlfor sl_igw in $sl_igws
218 do
219 sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw `
220 if [ "$sl_igwrttb x" = " x" ]
221 then
222 if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi
223 sl_cnt="$sl_rtnmax"
224 sl_ok="no"
225 until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ]
226 do
227 #space is used to recognized two and three digit numbers
228 sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " `
229 if [ "$sl_igwrttb x" = " x" ]
230 then
231 sl_ok="yes"
232 echo "$sl_cnt $sl_igw" >> $sl_rttab
233 fi
234 sl_cnt=$(($sl_cnt - 1 ))
23511simandl done
236 # a new table was created so we should set a flag for rules creation
237 sl_difftbl=1
2387simandl else
239 if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi
240 fi
2419simandl done
2427simandl} # s_mktables
2438simandl 
2447simandl######################################################################
245s_getcfg()
246{
247if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
248if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
249if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi
250 
25110simandlrm -f "$sl_sedlocache/$sl_nmcnf.main.tmp"
2527simandl 
25310simandl$sl_wgetcmd -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp"
254 
2557simandlif [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ]
256 then
257 date > $sl_sedlocache/last_getcnf.txt
258 cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main
25910simandl if [ $scm_info -gt 1 ]; then echo "Main config downloaded and accepted" ; fi
2607simandl else
2618simandl if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi
262 echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt
2637simandl date >> $sl_sedlocache/last_getcnf.txt
264fi
265 
26610simandl# before generating a new cached config we store the old one for
267# comparison with the new one
268rm -f "$sl_sedlocache/$sl_nmcnf.old"
269if [ -s $sl_sedlocache/$sl_nmcnf ]
270 then
271 cp $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old
272 else
273 touch $sl_sedlocache/$sl_nmcnf.old
274fi
275 
2767simandl# preparing cached config from local and main
277# the local config should be processed as the second to have
278# higher priority for rules from local config
279echo "# generated file" > $sl_sedlocache/$sl_nmcnf
280for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf`
281do
282cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
283cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
284cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
285done
2868simandl 
287#local gateways taken from local config
288cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
289 
2907simandl#cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq
291#mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf
292 
29310simandlsl_diffcfg=`diff $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old | grep -c .`
294if [ $sl_diffcfg -gt 0 ]
295 then
296 if [ $scm_info -gt 0 ]; then echo "New config is different than the old one" ; fi
297 else
298 if [ $scm_info -gt 0 ]; then echo "New config is the same as the old one" ; fi
299fi
300 
30111simandl# showing number of rules in config and system
30210simandlsl_numru=`ip ru ls | grep -c lookup`
303sl_numip=`grep -c ^ip $sl_sedlocache/$sl_nmcnf`
304sl_numru=$(($sl_numru - 3 ))
305if [ $sl_numip -gt $sl_numru ]
306 then
307 if [ $scm_info -gt 0 ]; then echo "We have less rules ($sl_numru) than new config has ips ($sl_numip)" ; fi
30811simandl# sl_diffcfg="1"
30910simandl else
310 if [ $scm_info -gt 0 ]; then echo "We have $sl_numru rules and $sl_numip ips" ; fi
311fi
312 
3137simandl}
3148simandl 
3157simandl######################################################################
316s_version()
317{
318 echo sedlo $sl_version
319} # s_version
3208simandl 
3217simandl######################################################################
322s_report()
323{
3249simandl echo Content-type: text/html
325 echo
326 echo "Sedlo na routeru `hostname`"
327 echo "<pre>"
3287simandl echo "##### SEDLO #####"
329 echo "date : `date`"
330 echo "version : $sl_version"
331 echo "local_config : $sl_sedlocnf"
3329simandl echo "main_config : <a href=\"$sl_murlcfg\">$sl_murlcfg</a>"
3337simandl echo "last update : `cat $sl_sedlocache/last_getcnf.txt`"
334 echo "##### TABLES #####"
335 cat $sl_rttab
3369simandl echo ; echo "##### DEFAULT ROUTES IN TABLES #####"
337 $sl_ipcmd ro ls ta all | $sl_grepcmd table | $sl_grepcmd -v local | $sl_trcmd " " "\t"
338 echo ; echo "##### RULES FOR IPS #####"
339 $sl_ipcmd ru ls | $sl_trcmd " " "\t"
340 echo "</pre>"
3417simandl} # s_report
3428simandl 
3439simandl 
344 
3457simandl######################################################################
346s_help()
347{
348 echo Pouziti: sedlo [param]
349 echo param:
350 echo -V vypise verzi
351 echo -help vypise napovedu
352 echo -v malo upovidany
353 echo -vv hodne upovidany
354 echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
355 echo -report vypise prehled pravidel a tabulek
356 echo -flru odstrani vsechny pravidla
35711simandl echo -force bezpodminecne znovu obnovi vsechny pravidla
3587simandl} # s_help
3598simandl 
3607simandl######################################################################
361######################################################################
362 
363sl_unknown=""
364scm_nogetcfg=0
365scm_flru=0
366scm_info=0
36711simandlscm_force=0
368sl_diffigw=0
369sl_difftbl=0
3707simandl 
371# parsing input parameters
372while [ "a$1" != "a" ]
373do
374 case $1 in
375 -V)
376 s_version
377 exit 0
378 ;;
379 -h)
380 s_help
381 exit 0
382 ;;
383 -report)
384 s_report
385 exit 0
386 ;;
387 -flru)
388 scm_flru=1
389 shift
390 ;;
39111simandl -force)
392 scm_force=1
393 shift
394 ;;
3957simandl -help)
396 s_help
397 exit 0
398 ;;
399 -nogetcfg)
400 scm_nogetcfg=1
401 shift
402 ;;
403 -v)
404 scm_info=1
405 shift
406 ;;
407 -vv)
408 scm_info=2
409 shift
410 ;;
411 *)
412 sl_unknown="$sl_unknown$1 "
413 shift
414 esac
415done
416 
417# printing the list of bad parameters (if there are some)
418if [ "a$sl_unknown" != "a" ]
419then
420 echo "$slm_unknown $sl_unknown"
421 s_help
422 exit 0
423fi
424 
425if [ $scm_flru -eq 1 ]
426then
427 s_flru
428 exit 0
429fi
430 
431if [ $scm_nogetcfg -eq 0 ]
432then
433 s_getcfg
434fi
435 
436s_mktables
437s_filltables
43811simandl#echo $sl_difftbl
439#echo $sl_diffcfg
440#echo $sl_diffigw
441#echo $scm_force
4427simandl 
44311simandl#flushing and filling rules is done only when
444#new table is created
445#config is changed
446#some igw dissapears or appears
447#-force command line parameter was used
448if [ $sl_difftbl -gt 0 ] || [ $sl_diffcfg -gt 0 ] || [ $sl_diffigw -gt 0 ] || [ $scm_force -gt 0 ]
44910simandlthen
450 s_fillrules
451fi
452 
4537simandlexit 0

Powered by WebSVN 2.2.1