jablonka.czprosek.czf

sedlo

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

 

Line No. Rev Author Line
12simandl#!/bin/bash
2# author : Petr Simandl www.simandl.cz
34simandl# release date : 07/09/2004
42simandl# name : sedlo
5# description : dynamic side routing tables tool
6# license : GPL
7 
85simandlsl_version="0.0.3pre4"
92simandl 
104simandlPATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
112simandl 
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 
315simandlif [ -e $sl_sedlocnf ]
322simandl then
33 sl_nop=1
34 else
355simandl echo "$sl_sedlocnf not found"
362simandl exit 1
37fi
38 
395simandlif [ -e $sl_rttab ]
40 then
412simandl sl_nop=1
42 else
435simandl echo "$sl_rttab not found"
442simandl exit 1
45fi
46 
475simandlsl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2}'`
482simandl 
49######################################################################
505simandls_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######################################################################
644simandls_flru()
65{
665simandlif [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi
674simandl 
685simandlsl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'`
694simandl 
705simandlfor 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 
774simandl} # s_flru
78######################################################################
793simandls_fillrules()
802simandl{
815simandls_flru
824simandl 
833simandlif [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi
842simandl 
855simandlsl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`
862simandl 
875simandlfor sl_ip in $sl_ips
883simandldo
895simandl 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"
923simandl for sl_ipgw in $sl_ipgws
932simandl do
945simandl sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw`
95 if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ]
962simandl then
975simandl 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"
1003simandl else
1015simandl 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
1073simandl fi
108 done
109done
1102simandl 
1115simandl$sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main
1124simandl 
1133simandl} # s_fillrules
114######################################################################
115s_filltables()
116{
117if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi
118 
1195simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'`
1203simandl 
121for sl_igw in $sl_igws
122 do
1235simandl 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 if [ "$sl_igwgt x" = " x" ]
127 then
128 if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi
129 else
130 sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
131 if [ "$sl_tbl x" != " x" ]
132 then
133 $sl_ipcmd ro fl ta $sl_igwn
134 fi
135 $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn
136 $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn
137 if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi
1382simandl fi
1393simandl done
1402simandl 
1413simandl} # s_filltables
142######################################################################
143s_mktables()
144{
145if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi
1465simandlsl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'`
1473simandlfor sl_igw in $sl_igws
148 do
1495simandl sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw `
150 if [ "$sl_igwrttb x" = " x" ]
1513simandl then
1525simandl if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi
153 sl_cnt="$sl_rtnmax"
1543simandl sl_ok="no"
155 until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ]
156 do
1575simandl #space is used to recognized two and three digit numbers
158 sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " `
159 if [ "$sl_igwrttb x" = " x" ]
1603simandl then
161 sl_ok="yes"
162 echo "$sl_cnt $sl_igw" >> $sl_rttab
163 fi
164 sl_cnt=$(($sl_cnt - 1 ))
165 done
166 else
1675simandl if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi
1683simandl fi
169 
1702simandl done
1713simandl} # s_mktables
1722simandl######################################################################
173s_getcfg()
174{
1753simandlif [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
1765simandlif [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
1773simandlif [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi
1784simandl 
1795simandlwget $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" -q
1804simandl 
181if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ]
1823simandl then
1834simandl date > $sl_sedlocache/last_getcnf.txt
184 cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main
185 if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi
1865simandl else
1874simandl if [ $scm_info -gt 1 ]; then echo "Main config not accepted - using cached config" ; fi
1885simandl echo -n "Main config not found " > $sl_sedlocache/last_getcnf.txt
1894simandl date >> $sl_sedlocache/last_getcnf.txt
1903simandlfi
1912simandl 
1925simandl# preparing cached config from local and main
193# the local config should be processed as the second to have
194# higher priority for rules from local config
1954simandl 
1965simandlecho "# generated file" > $sl_sedlocache/$sl_nmcnf
197for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf`
198do
199cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
200cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
201cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
202done
2034simandl#cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq
204#mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf
2055simandl 
2062simandl}
207######################################################################
208s_version()
209{
210 echo sedlo $sl_version
2113simandl} # s_version
2122simandl######################################################################
2133simandls_report()
214{
215 echo "##### SEDLO #####"
2164simandl echo "date : `date`"
2173simandl echo "version : $sl_version"
218 echo "local_config : $sl_sedlocnf"
2195simandl echo "main_config : $sl_murlcfg"
2204simandl echo "last update : `cat $sl_sedlocache/last_getcnf.txt`"
2213simandl echo "##### TABLES #####"
2225simandl cat $sl_rttab
2233simandl echo "##### RULES #####"
2245simandl $sl_ipcmd ru ls
2253simandl} # s_report
226######################################################################
2272simandls_help()
228{
2295simandl echo Pouziti: sedlo [param]
230 echo param:
231 echo -V vypise verzi
232 echo -help vypise napovedu
233 echo -v malo upovidany
234 echo -vv hodne upovidany
235 echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
236 echo -report vypise prehled pravidel a tabulek
237 echo -flru odstrani vsechny pravidla
2383simandl} # s_help
2392simandl######################################################################
240######################################################################
241 
2423simandlsl_unknown=""
243scm_nogetcfg=0
2444simandlscm_flru=0
2453simandlscm_info=0
2462simandl 
2473simandl# parsing input parameters
248while [ "a$1" != "a" ]
249do
250 case $1 in
2514simandl -V)
252 s_version
253 exit 0
2543simandl ;;
255 -h)
2564simandl s_help
257 exit 0
2583simandl ;;
259 -report)
2604simandl s_report
261 exit 0
2623simandl ;;
2634simandl -flru)
264 scm_flru=1
265 shift
266 ;;
2673simandl -help)
2684simandl s_help
269 exit 0
2703simandl ;;
271 -nogetcfg)
272 scm_nogetcfg=1
273 shift
274 ;;
2754simandl -v)
2763simandl scm_info=1
277 shift
278 ;;
2794simandl -vv)
2803simandl scm_info=2
281 shift
282 ;;
283 *)
284 sl_unknown="$sl_unknown$1 "
285 shift
286 esac
287done
288 
289# printing the list of bad parameters (if there are some)
290if [ "a$sl_unknown" != "a" ]
291then
292 echo "$slm_unknown $sl_unknown"
293 s_help
2944simandl exit 0
2953simandlfi
296 
2974simandlif [ $scm_flru -eq 1 ]
298then
2995simandl s_flqq
300 exit 0
3014simandlfi
302 
3033simandlif [ $scm_nogetcfg -eq 0 ]
304then
305 s_getcfg
306fi
307 
3082simandls_mktables
3093simandls_filltables
310s_fillrules
311 
3122simandlexit 0
3135simandl 

Powered by WebSVN 2.2.1