jablonka.czprosek.czf

sedlo

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

 

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

Powered by WebSVN 2.2.1