jablonka.czprosek.czf

sedlo

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

 

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 
8sl_version="0.0.3pre6"
9 
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 
47sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2}'`
48 
49######################################################################
50s_flqq()
51{
52if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi
53 
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 
62} # s_flru
63######################################################################
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 
77} # s_flru
78######################################################################
79s_fillrules()
80{
81s_flru
82 
83if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi
84 
85sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`
86 
87for sl_ip in $sl_ips
88do
89 sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'`
90 sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'`
91 sl_ok="no"
92 for sl_ipgw in $sl_ipgws
93 do
94 sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw`
95 if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ]
96 then
97 if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi
98 $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw
99 sl_ok="yes"
100 else
101 if [ "$sl_ok" = "no" ]
102 then
103 if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi
104 else
105 if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi
106 fi
107 fi
108 done
109done
110 
111$sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main
112 
113} # s_fillrules
114######################################################################
115s_filltables()
116{
117if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi
118 
119sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'`
120 
121for sl_igw in $sl_igws
122 do
123 sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'`
124 sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'`
125 sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'`
126 
127 # equal cost multipath detection - just first is taken
128 if [ "$sl_igwgt x" = "zebra x" ]
129 then
130 sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'`
131 fi
132 
133 if [ "$sl_igwgt x" = " x" ]
134 then
135 if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi
136 else
137 sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
138 if [ "$sl_tbl x" != " x" ]
139 then
140 $sl_ipcmd ro fl ta $sl_igwn
141 fi
142 $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn
143 $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn
144 if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi
145 fi
146 done
147 
148} # s_filltables
149######################################################################
150s_mktables()
151{
152if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi
153sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'`
154for sl_igw in $sl_igws
155 do
156 sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw `
157 if [ "$sl_igwrttb x" = " x" ]
158 then
159 if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi
160 sl_cnt="$sl_rtnmax"
161 sl_ok="no"
162 until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ]
163 do
164 #space is used to recognized two and three digit numbers
165 sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " `
166 if [ "$sl_igwrttb x" = " x" ]
167 then
168 sl_ok="yes"
169 echo "$sl_cnt $sl_igw" >> $sl_rttab
170 fi
171 sl_cnt=$(($sl_cnt - 1 ))
172 done
173 else
174 if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi
175 fi
176 
177 done
178} # s_mktables
179######################################################################
180s_getcfg()
181{
182if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
183if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
184if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi
185 
186wget $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" -q
187 
188if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ]
189 then
190 date > $sl_sedlocache/last_getcnf.txt
191 cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main
192 if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi
193 else
194 if [ $scm_info -gt 1 ]; then echo "Main config not accepted - using cached config" ; fi
195 echo -n "Main config not found " > $sl_sedlocache/last_getcnf.txt
196 date >> $sl_sedlocache/last_getcnf.txt
197fi
198 
199# preparing cached config from local and main
200# the local config should be processed as the second to have
201# higher priority for rules from local config
202 
203echo "# generated file" > $sl_sedlocache/$sl_nmcnf
204for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf`
205do
206cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
207cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
208cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
209done
210#cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq
211#mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf
212 
213}
214######################################################################
215s_version()
216{
217 echo sedlo $sl_version
218} # s_version
219######################################################################
220s_report()
221{
222 echo "##### SEDLO #####"
223 echo "date : `date`"
224 echo "version : $sl_version"
225 echo "local_config : $sl_sedlocnf"
226 echo "main_config : $sl_murlcfg"
227 echo "last update : `cat $sl_sedlocache/last_getcnf.txt`"
228 echo "##### TABLES #####"
229 cat $sl_rttab
230 echo "##### RULES #####"
231 $sl_ipcmd ru ls
232} # s_report
233######################################################################
234s_help()
235{
236 echo Pouziti: sedlo [param]
237 echo param:
238 echo -V vypise verzi
239 echo -help vypise napovedu
240 echo -v malo upovidany
241 echo -vv hodne upovidany
242 echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
243 echo -report vypise prehled pravidel a tabulek
244 echo -flru odstrani vsechny pravidla
245} # s_help
246######################################################################
247######################################################################
248 
249sl_unknown=""
250scm_nogetcfg=0
251scm_flru=0
252scm_info=0
253 
254# parsing input parameters
255while [ "a$1" != "a" ]
256do
257 case $1 in
258 -V)
259 s_version
260 exit 0
261 ;;
262 -h)
263 s_help
264 exit 0
265 ;;
266 -report)
267 s_report
268 exit 0
269 ;;
270 -flru)
271 scm_flru=1
272 shift
273 ;;
274 -help)
275 s_help
276 exit 0
277 ;;
278 -nogetcfg)
279 scm_nogetcfg=1
280 shift
281 ;;
282 -v)
283 scm_info=1
284 shift
285 ;;
286 -vv)
287 scm_info=2
288 shift
289 ;;
290 *)
291 sl_unknown="$sl_unknown$1 "
292 shift
293 esac
294done
295 
296# printing the list of bad parameters (if there are some)
297if [ "a$sl_unknown" != "a" ]
298then
299 echo "$slm_unknown $sl_unknown"
300 s_help
301 exit 0
302fi
303 
304if [ $scm_flru -eq 1 ]
305then
306 s_flru
307 exit 0
308fi
309 
310if [ $scm_nogetcfg -eq 0 ]
311then
312 s_getcfg
313fi
314 
315s_mktables
316s_filltables
317s_fillrules
318 
319exit 0
320 
321 

Powered by WebSVN 2.2.1