jablonka.czprosek.czf

sedlo

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

 

Line No. Rev Author Line
12simandl#!/bin/bash
2# author : Petr Simandl www.simandl.cz
33simandl# release date : 29/08/2004
42simandl# name : sedlo
5# description : dynamic side routing tables tool
6# license : GPL
7 
83simandlsl_version="0.0.2"
92simandl 
10PATH=$PATH:$HOME/bin:/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
183simandlsl_rtnmax=200
19sl_ipnodef="10.0.0.0/8"
202simandl 
213simandlslm_unknown="Nezname parametry : "
22 
23sl_ipcmd=`which ip`
24sl_trcmd=`which tr`
25sl_wgetcmd=`which wget`
262simandlsl_hnmcmd=`which hostname`
27sl_awkcmd=`which awk`
283simandlsl_catcmd=`which cat`
29sl_grepcmd=`which grep`
302simandl 
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######################################################################
503simandls_fillrules()
512simandl{
523simandlif [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi
532simandl 
543simandlsl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`
552simandl 
563simandlfor sl_ip in $sl_ips
57do
58 sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'`
59 sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'`
60 sl_ok="no"
61 for sl_ipgw in $sl_ipgws
622simandl do
633simandl sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw`
64 if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ]
652simandl then
663simandl sl_empty="no"
67 sl_cnt=3
68#removing until we have some rules in table but the maximum of cycles is limited
69#to avoid neverending loop
70 while [ "$sl_empty" = "no" ]
71 do
72 sl_ipru=`$sl_ipcmd ru ls | $sl_grepcmd $sl_ipn`
73 if [ "$sl_ipru x" != " x" ]
742simandl then
753simandl if [ $scm_info -gt 1 ]; then echo "Removing old rules for $sl_ipn" ; fi
76 $sl_ipcmd ru del from $sl_ipn
77 $sl_ipcmd ru del from $sl_ipn to $sl_ipnodef
78 else
79 sl_empty="yes"
802simandl fi
81 sl_cnt=$(($sl_cnt - 1 ))
823simandl if [ "$sl_cnt" -eq -1 ]
83 then
84 sl_empty="yes"
85 echo "Too much cycles"
86 fi
87 done
88 if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi
89 $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw
90 $sl_ipcmd ru add from $sl_ipn to $sl_ipnodef lookup main
91 sl_ok="yes"
92 else
93 if [ "$sl_ok" = "no" ]
94 then
95 if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi
96 else
97 if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi
98 fi
99 fi
100 done
101done
1022simandl 
1033simandl} # s_fillrules
104######################################################################
105s_filltables()
106{
107if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi
108 
109sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'`
110 
111for sl_igw in $sl_igws
112 do
113 sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'`
114 sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'`
115 sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'`
116 if [ "$sl_igwgt x" = " x" ]
117 then
118 if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table empty" ; fi
119 sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
120 if [ "$sl_tbl x" != " x" ]
121 then
122 $sl_ipcmd ro fl ta $sl_igwn
123 fi
1242simandl else
1253simandl sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
126 if [ "$sl_tbl x" != " x" ]
127 then
128 $sl_ipcmd ro fl ta $sl_igwn
129 fi
130 $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn
131 $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn
132 if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi
1332simandl fi
1343simandl done
1352simandl 
1363simandl} # s_filltables
137######################################################################
138s_mktables()
139{
140if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi
141sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'`
142for sl_igw in $sl_igws
143 do
144 sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw `
145 if [ "$sl_igwrttb x" = " x" ]
146 then
147 if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi
148 sl_cnt="$sl_rtnmax"
149 sl_ok="no"
150 until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ]
151 do
152 #space is used to recognized two and three digit numbers
153 sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " `
154 if [ "$sl_igwrttb x" = " x" ]
155 then
156 sl_ok="yes"
157 echo "$sl_cnt $sl_igw" >> $sl_rttab
158 fi
159 sl_cnt=$(($sl_cnt - 1 ))
160 done
161 else
162 if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi
163 fi
164 
1652simandl done
1663simandl} # s_mktables
1672simandl######################################################################
168s_getcfg()
169{
1703simandlif [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
171if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
172if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi
1732simandlwget $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main" -q
1743simandlif [ -s $sl_sedlocache/$sl_nmcnf.main ]
175 then
176 sl_nop=1
177 else
178 if [ $scm_info -gt 1 ]; then echo "Main config not found $sl_sedlocache/$sl_nmcnf.main" ; fi
179fi
1802simandlecho "# generated file" > $sl_sedlocache/$sl_nmcnf
181for sl_file in `ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main `
182do
183cat $sl_file | grep "^mcnf" | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
184cat $sl_file | grep "^igw" | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
185cat $sl_file | grep "^ip" | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
186done
187cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq
188mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf
189 
190}
191######################################################################
192s_version()
193{
194 echo sedlo $sl_version
195 exit 0
1963simandl} # s_version
1972simandl######################################################################
1983simandls_report()
199{
200 echo "##### SEDLO #####"
201 echo "version : $sl_version"
202 echo "local_config : $sl_sedlocnf"
203 echo "main_config : $sl_murlcfg"
204 echo "##### TABLES #####"
205 cat $sl_rttab
206 echo "##### RULES #####"
207 $sl_ipcmd ru ls
208 exit 0
209} # s_report
210######################################################################
2112simandls_help()
212{
213 echo Pouziti: sedlo [param]
214 echo param:
215 echo -v vypise verzi
2163simandl echo -help vypise napovedu
217 echo -info1 malo upovidany
218 echo -info2 hodne upovidany
219 echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
220 echo -report vypise prehled pravidel a tabulek
2212simandl exit 0
2223simandl} # s_help
2232simandl######################################################################
224######################################################################
225 
2263simandlsl_unknown=""
227scm_nogetcfg=0
228scm_info=0
2292simandl 
2303simandl# parsing input parameters
231while [ "a$1" != "a" ]
232do
233 case $1 in
234 -v)
235 s_version
236 ;;
237 -h)
238 s_help
239 ;;
240 -report)
241 s_report
242 ;;
243 -help)
244 s_help
245 ;;
246 -nogetcfg)
247 scm_nogetcfg=1
248 shift
249 ;;
250 -info1)
251 scm_info=1
252 shift
253 ;;
254 -info2)
255 scm_info=2
256 shift
257 ;;
258 *)
259 sl_unknown="$sl_unknown$1 "
260 shift
261 esac
262done
263 
264# printing the list of bad parameters (if there are some)
265if [ "a$sl_unknown" != "a" ]
266then
267 echo "$slm_unknown $sl_unknown"
268 s_help
269fi
270 
271if [ $scm_nogetcfg -eq 0 ]
272then
273 s_getcfg
274fi
275 
2762simandls_mktables
2773simandls_filltables
278s_fillrules
279 
2802simandlexit 0
2813simandl 

Powered by WebSVN 2.2.1