jablonka.czprosek.czf

sedlo

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

 

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

Powered by WebSVN 2.2.1