jablonka.czprosek.czf

sedlo

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

 

Line No. Rev Author Line
17simandl#!/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 
89simandlsl_version="0.0.3pre9"
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`
30 
31if [ -e $sl_sedlocnf ]
32 then
33 sl_nop=1
34 else
35 echo "$sl_sedlocnf not found"
36 exit 1
37fi
38 
39if [ -e $sl_rttab ]
40 then
41 sl_nop=1
42 else
43 echo "$sl_rttab not found"
44 exit 1
45fi
46 
479simandlsl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2" "$3" "$4}'`
487simandl 
49######################################################################
508simandls_maru()
517simandl{
52if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi
53 
54sl_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 
57sl_merged=`echo $sl_rules $sl_ips | sort | uniq`
58 
59echo $sl_merged
60 
618simandl} # s_maru
627simandl######################################################################
63s_flru()
64{
65if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi
66 
67sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'`
68 
69for sl_rule in $sl_rules
70do
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
74done
75 
768simandl#this should make faster applying of new routing tables
77$sl_ipcmd ro flush cache
78 
797simandl} # s_flru
808simandl 
817simandl######################################################################
82s_fillrules()
83{
848simandl 
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
887simandls_flru
89 
90if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi
91 
92sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`
93 
94for sl_ip in $sl_ips
95do
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
116done
117 
118$sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main
119 
1208simandl#this should make faster applying of new routing tables
121$sl_ipcmd ro flush cache
122 
1237simandl} # s_fillrules
1248simandl 
1257simandl######################################################################
126s_filltables()
127{
128if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi
129 
1309simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3"*"$2"*"$1}'`
1317simandl 
132for 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}'`
1369simandl sl_igwtype=`echo $sl_igw | $sl_awkcmd -F '*' '{print $3}'`
1377simandl sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'`
138 
1399simandl # equal cost multipath detection - just first IP is taken as way to igw
1407simandl 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 
1459simandl #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 
1517simandl 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
1629simandl 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
1687simandl fi
169 done
170 
1718simandl#this should make faster applying of new routing tables
172$sl_ipcmd ro flush cache
173 
1747simandl} # s_filltables
1758simandl 
1767simandl######################################################################
177s_mktables()
178{
179if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi
1809simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3}'`
1817simandlfor 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
2039simandl done
2047simandl 
205} # s_mktables
2068simandl 
2077simandl######################################################################
208s_getcfg()
209{
210if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
211if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
212if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi
213 
2148simandlwget -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp"
2157simandl 
216if [ -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
2228simandl 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
2247simandl date >> $sl_sedlocache/last_getcnf.txt
225fi
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 
231echo "# generated file" > $sl_sedlocache/$sl_nmcnf
232for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf`
233do
234cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
235cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
236cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
237done
2388simandl 
239#local gateways taken from local config
240cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
241 
2427simandl#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}
2468simandl 
2477simandl######################################################################
248s_version()
249{
250 echo sedlo $sl_version
251} # s_version
2528simandl 
2537simandl######################################################################
254s_report()
255{
2569simandl echo Content-type: text/html
257 echo
258 echo "Sedlo na routeru `hostname`"
259 echo "<pre>"
2607simandl echo "##### SEDLO #####"
261 echo "date : `date`"
262 echo "version : $sl_version"
263 echo "local_config : $sl_sedlocnf"
2649simandl echo "main_config : <a href=\"$sl_murlcfg\">$sl_murlcfg</a>"
2657simandl echo "last update : `cat $sl_sedlocache/last_getcnf.txt`"
266 echo "##### TABLES #####"
267 cat $sl_rttab
2689simandl 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>"
2737simandl} # s_report
2748simandl 
2759simandl 
276 
2777simandl######################################################################
278s_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
2908simandl 
2917simandl######################################################################
292######################################################################
293 
294sl_unknown=""
295scm_nogetcfg=0
296scm_flru=0
297scm_info=0
298 
299# parsing input parameters
300while [ "a$1" != "a" ]
301do
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
339done
340 
341# printing the list of bad parameters (if there are some)
342if [ "a$sl_unknown" != "a" ]
343then
344 echo "$slm_unknown $sl_unknown"
345 s_help
346 exit 0
347fi
348 
349if [ $scm_flru -eq 1 ]
350then
351 s_flru
352 exit 0
353fi
354 
355if [ $scm_nogetcfg -eq 0 ]
356then
357 s_getcfg
358fi
359 
360s_mktables
361s_filltables
362s_fillrules
363 
364exit 0
365 
366 

Powered by WebSVN 2.2.1