1 | 7 | simandl | #!/bin/bash |
2 | | | # author : Petr Simandl www.simandl.cz |
3 | 21 | simandl | # release date : 03/06/2007 |
4 | 7 | simandl | # name : sedlo |
5 | | | # description : dynamic side routing tables tool |
6 | | | # license : GPL |
7 | | | |
8 | 22 | simandl | sl_version="0.0.4pre10" |
9 | 7 | simandl | |
10 | | | PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin |
11 | | | |
12 | | | sl_nmcnf="sedlo.conf" |
13 | | | sl_sedlocnf="/etc/$sl_nmcnf" |
14 | | | sl_sedlocache="/var/cache/sedlo" |
15 | | | |
16 | | | sl_rttab="/etc/iproute2/rt_tables" |
17 | | | sl_rtnmin=110 |
18 | | | sl_rtnmax=200 |
19 | 16 | simandl | #all traffic that is handled as internal (CZF traffic) |
20 | 7 | simandl | sl_ipnodef="10.0.0.0/8" |
21 | 22 | simandl | #this will specify base priority in rule table |
22 | | | sl_priobase=10000 |
23 | | | #this will specify base priority in rule table select mask |
24 | | | sl_priorulesmask="100.." |
25 | 16 | simandl | #no default traffic will have rule at higher priority |
26 | 22 | simandl | sl_prionodef=$(($sl_priobase - 1)) |
27 | 7 | simandl | |
28 | | | slm_unknown="Nezname parametry : " |
29 | | | |
30 | | | sl_ipcmd=`which ip` |
31 | | | sl_trcmd=`which tr` |
32 | | | sl_wgetcmd=`which wget` |
33 | | | sl_hnmcmd=`which hostname` |
34 | | | sl_awkcmd=`which awk` |
35 | | | sl_catcmd=`which cat` |
36 | | | sl_grepcmd=`which grep` |
37 | 10 | simandl | sl_diffcmd=`which diff` |
38 | 7 | simandl | |
39 | | | if [ -e $sl_sedlocnf ] |
40 | | | then |
41 | | | sl_nop=1 |
42 | | | else |
43 | | | echo "$sl_sedlocnf not found" |
44 | | | exit 1 |
45 | | | fi |
46 | | | |
47 | | | if [ -e $sl_rttab ] |
48 | | | then |
49 | | | sl_nop=1 |
50 | | | else |
51 | | | echo "$sl_rttab not found" |
52 | | | exit 1 |
53 | | | fi |
54 | | | |
55 | 9 | simandl | sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2" "$3" "$4}'` |
56 | 7 | simandl | |
57 | | | ###################################################################### |
58 | 22 | simandl | #this will delete all rules at sl_prionodef and sl_priorulesmask priorities |
59 | 16 | simandl | s_flruall() |
60 | | | { |
61 | | | if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi |
62 | | | |
63 | | | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd "^"$sl_prionodef":" | $sl_trcmd '[:blank:]' '*'` |
64 | | | for sl_rule in $sl_rules |
65 | | | do |
66 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
67 | | | $sl_ipcmd ru del $sl_ipgws |
68 | | | #this should make faster applying of new routing tables |
69 | | | $sl_ipcmd ro flush cache |
70 | | | done |
71 | | | |
72 | 22 | simandl | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd "^"$sl_priorulesmask":" | $sl_trcmd '[:blank:]' '*'` |
73 | 16 | simandl | for sl_rule in $sl_rules |
74 | | | do |
75 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
76 | | | $sl_ipcmd ru del $sl_ipgws |
77 | | | #this should make faster applying of new routing tables |
78 | | | $sl_ipcmd ro flush cache |
79 | | | done |
80 | | | |
81 | | | } # s_flruall |
82 | | | |
83 | | | ###################################################################### |
84 | | | s_checknodefru() |
85 | | | { |
86 | | | #checking if we have present nodef rule and if not we create it |
87 | | | sl_nodefrule=`$sl_ipcmd ru ls | $sl_grepcmd "^"$sl_prionodef":" | $sl_trcmd '[:blank:]' '*'` |
88 | | | #echo $sl_nodefrule |
89 | | | if [ "$sl_nodefrule x" == " x" ] |
90 | | | then |
91 | | | if [ $scm_info -gt 0 ]; then echo "Creating rule for nodef route" ; fi |
92 | | | $sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main prio $sl_prionodef |
93 | | | fi |
94 | | | } |
95 | | | |
96 | | | ###################################################################### |
97 | 10 | simandl | # here we get each ip and we create a rule to send this ip to a |
98 | | | # certain table |
99 | | | # this routine can be skipped when the number of ips and ip directions |
100 | | | # are still the same = old and new configs are the same |
101 | 7 | simandl | s_fillrules() |
102 | | | { |
103 | 8 | simandl | |
104 | 16 | simandl | #this will check no default rule if exists and if not it will be created |
105 | | | s_checknodefru |
106 | | | |
107 | 17 | simandl | if [ $scm_info -gt 0 ]; then echo "Checking rules for ips" ; fi |
108 | 7 | simandl | |
109 | | | sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
110 | 22 | simandl | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd ^$sl_priorulesmask":" | $sl_trcmd '[:blank:]' '*'` |
111 | 7 | simandl | |
112 | 17 | simandl | #for all IPs we check and leave, change or create rule |
113 | 7 | simandl | for sl_ip in $sl_ips |
114 | | | do |
115 | | | sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
116 | | | sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
117 | | | sl_ok="no" |
118 | 17 | simandl | #for all GWs we check rules |
119 | 7 | simandl | for sl_ipgw in $sl_ipgws |
120 | | | do |
121 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
122 | 17 | simandl | #check if table exists |
123 | 7 | simandl | if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
124 | | | then |
125 | 17 | simandl | #we have IP and GW table |
126 | | | #check if the rule alredady exists |
127 | | | sl_oldrule=`echo $sl_rules | $sl_trcmd " " "\n" | $sl_grepcmd "\*"$sl_ipn"\*"` |
128 | | | sl_exactrule=`echo $sl_oldrule | $sl_trcmd " " "\n" | $sl_grepcmd "\*"$sl_ipgw"\*"` |
129 | | | if [ "$sl_exactrule x" == " x" ] |
130 | | | then |
131 | | | #exact rule doesn't exist so we check if an old rule for this IP is present |
132 | | | if [ "$sl_oldrule x" != " x" ] |
133 | | | then |
134 | | | #some old rule(s) for IP is present so we delete it |
135 | | | for sl_rule in $sl_oldrule |
136 | | | do |
137 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
138 | | | if [ $scm_info -gt 0 ]; then echo "Deleting old rule $sl_ipgws" ; fi |
139 | | | $sl_ipcmd ru del $sl_ipgws |
140 | | | #this should make faster applying of new routing tables |
141 | | | $sl_ipcmd ro flush cache |
142 | | | done |
143 | | | fi |
144 | 22 | simandl | #getting subnet mask if exists |
145 | | | sl_subnet=`echo $sl_ip | $sl_awkcmd -F '/' '{print $2}' | $sl_awkcmd -F '*' '{print $1}' ` |
146 | | | if [ "$sl_subnet x" == " x" ] |
147 | | | then |
148 | | | #if subnet was not found we set subnet to 32 |
149 | | | sl_subnet=32 |
150 | | | else |
151 | | | #this is just to be sure to have subnet between 1 and 32 |
152 | | | if [ $sl_subnet -gt 32 ]; then sl_subnet=32 ; fi |
153 | | | if [ $sl_subnet -lt 1 ]; then sl_subnet=1 ; fi |
154 | | | fi |
155 | | | #bigger subnets have lower priority |
156 | | | sl_priorule=$(($sl_priobase + 32)) |
157 | | | sl_priorule=$(($sl_priorule - $sl_subnet)) |
158 | | | |
159 | 17 | simandl | if [ $scm_info -gt 0 ]; then echo "Creating new rule to send $sl_ipn to table $sl_ipgw" ; fi |
160 | 22 | simandl | $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw prio $sl_priorule |
161 | 17 | simandl | |
162 | | | else |
163 | | | if [ $scm_info -gt 1 ]; then echo "Rule to send $sl_ipn to table $sl_ipgw already exists" ; fi |
164 | | | fi |
165 | 7 | simandl | sl_ok="yes" |
166 | | | else |
167 | 17 | simandl | #we have no table |
168 | 7 | simandl | if [ "$sl_ok" = "no" ] |
169 | | | then |
170 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
171 | | | else |
172 | 16 | simandl | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
173 | 7 | simandl | fi |
174 | | | fi |
175 | | | done |
176 | | | done |
177 | | | |
178 | 17 | simandl | #finally we check all rules and if there is a rule without IP from config we delete it |
179 | | | for sl_rule in $sl_rules |
180 | | | do |
181 | | | sl_iprule=`echo $sl_rule | $sl_awkcmd -F '*' '{print $3}'` |
182 | | | sl_ipconf=`echo $sl_ips | $sl_trcmd " " "\n" | $sl_grepcmd "^"$sl_iprule"\*"` |
183 | 7 | simandl | |
184 | 17 | simandl | if [ "$sl_ipconf x" == " x" ] |
185 | | | then |
186 | | | #we have a rule without an IP in config so we delete this rule |
187 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
188 | | | if [ $scm_info -gt 0 ]; then echo "Deleting non config rule $sl_ipgws" ; fi |
189 | | | $sl_ipcmd ru del $sl_ipgws |
190 | | | #this should make faster applying of new routing tables |
191 | | | $sl_ipcmd ro flush cache |
192 | | | fi |
193 | | | done |
194 | | | |
195 | 7 | simandl | } # s_fillrules |
196 | 8 | simandl | |
197 | 7 | simandl | ###################################################################### |
198 | 10 | simandl | # here we look into the main routing table for path to our iGWs |
199 | | | # and we fill these tables with two halves default nets that |
200 | | | # point to appropriate direction |
201 | | | # this routine can be skipped when the routing table is the same |
202 | 7 | simandl | s_filltables() |
203 | | | { |
204 | 10 | simandl | if [ $scm_info -gt 0 ]; then echo "Checking main routing table" ; fi |
205 | | | |
206 | 7 | simandl | if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
207 | | | |
208 | 9 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3"*"$2"*"$1}'` |
209 | 7 | simandl | |
210 | | | for sl_igw in $sl_igws |
211 | | | do |
212 | | | sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
213 | | | sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
214 | 9 | simandl | sl_igwtype=`echo $sl_igw | $sl_awkcmd -F '*' '{print $3}'` |
215 | 15 | simandl | |
216 | 14 | simandl | #oprava falesneho routovani na lokalni iface - pokud jsme lokalni igw tak se nema najit ip |
217 | | | #protoze cesta dal neni - jsme totiz uz na lokalnim iface |
218 | 15 | simandl | #head je tam proto ze se pro prespolni(a bgp) muze objevit vice rout s ruznou metrikou tak vezmem jen prvni (head) |
219 | | | #s nejmensi metrikou (sort) |
220 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -v "proto kernel" | $sl_grepcmd "^$sl_igwip " | sort | $sl_awkcmd '{print $3}' | head -n 1` |
221 | 7 | simandl | |
222 | 9 | simandl | # equal cost multipath detection - just first IP is taken as way to igw |
223 | 7 | simandl | if [ "$sl_igwgt x" = "zebra x" ] |
224 | | | then |
225 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'` |
226 | | | fi |
227 | | | |
228 | 9 | simandl | #if myigw then fill table for local gateway with single ip from config |
229 | | | if [ "$sl_igwtype x" = "myigw x" ] |
230 | | | then |
231 | | | sl_igwgt=$sl_igwip |
232 | | | fi |
233 | 11 | simandl | #testing if the igw has not a route in global routing table |
234 | 7 | simandl | if [ "$sl_igwgt x" = " x" ] |
235 | | | then |
236 | 11 | simandl | if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn" ; fi |
237 | | | sl_myigw=`cat $sl_sedlocnf | $sl_grepcmd "^myigw" | $sl_grepcmd $sl_igwn | $sl_awkcmd '{print $3}'` |
238 | | | #testing if the igw without route is in local config |
239 | | | #if not we go to flush its table and set flag to redo rules |
240 | | | if [ "$sl_myigw x" = " x" ] |
241 | | | then |
242 | | | #getting num of routes of igw |
243 | 13 | simandl | sl_igwnr=`$sl_ipcmd ro ls ta all | $sl_grepcmd -c "table ${sl_igwn} "` |
244 | 11 | simandl | if [ "$sl_igwnr x" = "0 x" ] |
245 | | | then |
246 | 12 | simandl | if [ $scm_info -gt 1 ]; then echo "Table $sl_igwn is already empty - no action taken" ; fi |
247 | 11 | simandl | else |
248 | 12 | simandl | if [ $scm_info -gt 1 ]; then echo "Table $sl_igwn will be flushed and rules rearranged" ; fi |
249 | 11 | simandl | $sl_ipcmd ro fl ta $sl_igwn |
250 | | | #because this igw dissapeared we set a flag for rules recreation |
251 | | | sl_diffigw=1 |
252 | | | fi |
253 | | | else |
254 | 13 | simandl | if [ $scm_info -gt 1 ]; then echo "Igw $sl_igwn found in local config - leaving table as is" ; fi |
255 | 11 | simandl | fi |
256 | 7 | simandl | else |
257 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
258 | 12 | simandl | #if the table is empty we fill it and we set flag for rules recreation |
259 | | | if [ "$sl_tbl x" = " x" ] |
260 | 7 | simandl | then |
261 | 12 | simandl | sl_diffigw=1 |
262 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
263 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
264 | | | if [ "$sl_igwtype x" = "myigw x" ] |
265 | | | then |
266 | | | if [ $scm_info -gt 1 ]; then echo "Table $sl_igwn filled with default myigw $sl_igwgt" ; fi |
267 | | | else |
268 | | | if [ $scm_info -gt 1 ]; then echo "Table $sl_igwn filled with default gw $sl_igwgt" ; fi |
269 | | | fi |
270 | | | #the table is not empty so we check if routes are the same |
271 | 9 | simandl | else |
272 | 12 | simandl | #picking default gateway from the table |
273 | | | sl_igwogt=`$sl_ipcmd ro ls ta $sl_igwn | $sl_awkcmd '{print $3}' | uniq` |
274 | | | #checking if the old default is same as the new one |
275 | | | if [ "$sl_igwogt x" = "$sl_igwgt x" ] |
276 | | | then |
277 | | | if [ $scm_info -gt 1 ]; then echo "Table $sl_igwn will not be changed and default is $sl_igwgt" ; fi |
278 | | | else |
279 | | | #the new default is different so we will flush the table, fill new default |
280 | 15 | simandl | if [ $scm_info -gt 1 ]; then echo "Table $sl_igwn will be rewritten to default $sl_igwgt" ; fi |
281 | 12 | simandl | #flushing old default route in the table |
282 | | | $sl_ipcmd ro fl ta $sl_igwn |
283 | | | #filling new default |
284 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
285 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
286 | | | fi |
287 | 9 | simandl | fi |
288 | 7 | simandl | fi |
289 | | | done |
290 | | | |
291 | | | } # s_filltables |
292 | 8 | simandl | |
293 | 7 | simandl | ###################################################################### |
294 | 10 | simandl | # filling rttab with tables from config |
295 | | | # only new tables are created with a new uniq number that is not important because |
296 | | | # usually we handle tables just by their names |
297 | | | # this routine acts only when a new iGW appears - only adding a table is supported |
298 | | | # no deleting is implemented because it seems to be not necessary to delete an old table |
299 | | | # because there is space enough and after reboot table will not be created |
300 | 7 | simandl | s_mktables() |
301 | | | { |
302 | 10 | simandl | if [ $scm_info -gt 0 ]; then echo "Checking tables" ; fi |
303 | | | |
304 | 9 | simandl | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd -E "^igw|^myigw" | $sl_awkcmd '{print $3}'` |
305 | 7 | simandl | for sl_igw in $sl_igws |
306 | | | do |
307 | | | sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
308 | | | if [ "$sl_igwrttb x" = " x" ] |
309 | | | then |
310 | | | if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
311 | | | sl_cnt="$sl_rtnmax" |
312 | | | sl_ok="no" |
313 | | | until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
314 | | | do |
315 | | | #space is used to recognized two and three digit numbers |
316 | | | sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
317 | | | if [ "$sl_igwrttb x" = " x" ] |
318 | | | then |
319 | | | sl_ok="yes" |
320 | | | echo "$sl_cnt $sl_igw" >> $sl_rttab |
321 | | | fi |
322 | | | sl_cnt=$(($sl_cnt - 1 )) |
323 | 11 | simandl | done |
324 | | | # a new table was created so we should set a flag for rules creation |
325 | | | sl_difftbl=1 |
326 | 7 | simandl | else |
327 | | | if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
328 | | | fi |
329 | 9 | simandl | done |
330 | 7 | simandl | } # s_mktables |
331 | 8 | simandl | |
332 | 7 | simandl | ###################################################################### |
333 | | | s_getcfg() |
334 | | | { |
335 | | | if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
336 | | | if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
337 | | | if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
338 | | | |
339 | 10 | simandl | rm -f "$sl_sedlocache/$sl_nmcnf.main.tmp" |
340 | 7 | simandl | |
341 | 10 | simandl | $sl_wgetcmd -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" |
342 | | | |
343 | 7 | simandl | if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ] |
344 | | | then |
345 | | | date > $sl_sedlocache/last_getcnf.txt |
346 | | | cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main |
347 | 10 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config downloaded and accepted" ; fi |
348 | 7 | simandl | else |
349 | 8 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi |
350 | | | echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt |
351 | 7 | simandl | date >> $sl_sedlocache/last_getcnf.txt |
352 | | | fi |
353 | | | |
354 | 10 | simandl | # before generating a new cached config we store the old one for |
355 | | | # comparison with the new one |
356 | | | rm -f "$sl_sedlocache/$sl_nmcnf.old" |
357 | | | if [ -s $sl_sedlocache/$sl_nmcnf ] |
358 | | | then |
359 | | | cp $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old |
360 | | | else |
361 | | | touch $sl_sedlocache/$sl_nmcnf.old |
362 | | | fi |
363 | | | |
364 | 7 | simandl | # preparing cached config from local and main |
365 | 22 | simandl | # the local config should be processed as the first to have |
366 | 7 | simandl | # higher priority for rules from local config |
367 | | | echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
368 | 22 | simandl | for sl_file in `ls $sl_sedlocnf ; ls $sl_sedlocache/$sl_nmcnf.main ` |
369 | 7 | simandl | do |
370 | | | cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
371 | | | cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
372 | | | cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
373 | | | done |
374 | 8 | simandl | |
375 | | | #local gateways taken from local config |
376 | | | cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
377 | | | |
378 | 7 | simandl | #cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
379 | | | #mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
380 | | | |
381 | 10 | simandl | sl_diffcfg=`diff $sl_sedlocache/$sl_nmcnf $sl_sedlocache/$sl_nmcnf.old | grep -c .` |
382 | | | if [ $sl_diffcfg -gt 0 ] |
383 | | | then |
384 | | | if [ $scm_info -gt 0 ]; then echo "New config is different than the old one" ; fi |
385 | | | else |
386 | | | if [ $scm_info -gt 0 ]; then echo "New config is the same as the old one" ; fi |
387 | | | fi |
388 | | | |
389 | 11 | simandl | # showing number of rules in config and system |
390 | 10 | simandl | sl_numru=`ip ru ls | grep -c lookup` |
391 | | | sl_numip=`grep -c ^ip $sl_sedlocache/$sl_nmcnf` |
392 | | | sl_numru=$(($sl_numru - 3 )) |
393 | | | if [ $sl_numip -gt $sl_numru ] |
394 | | | then |
395 | | | if [ $scm_info -gt 0 ]; then echo "We have less rules ($sl_numru) than new config has ips ($sl_numip)" ; fi |
396 | 11 | simandl | # sl_diffcfg="1" |
397 | 10 | simandl | else |
398 | | | if [ $scm_info -gt 0 ]; then echo "We have $sl_numru rules and $sl_numip ips" ; fi |
399 | | | fi |
400 | 7 | simandl | } |
401 | 8 | simandl | |
402 | 7 | simandl | ###################################################################### |
403 | | | s_version() |
404 | | | { |
405 | | | echo sedlo $sl_version |
406 | | | } # s_version |
407 | 8 | simandl | |
408 | 7 | simandl | ###################################################################### |
409 | | | s_report() |
410 | | | { |
411 | 9 | simandl | echo Content-type: text/html |
412 | | | echo |
413 | | | echo "Sedlo na routeru `hostname`" |
414 | | | echo "<pre>" |
415 | 7 | simandl | echo "##### SEDLO #####" |
416 | | | echo "date : `date`" |
417 | | | echo "version : $sl_version" |
418 | | | echo "local_config : $sl_sedlocnf" |
419 | 9 | simandl | echo "main_config : <a href=\"$sl_murlcfg\">$sl_murlcfg</a>" |
420 | 7 | simandl | echo "last update : `cat $sl_sedlocache/last_getcnf.txt`" |
421 | | | echo "##### TABLES #####" |
422 | | | cat $sl_rttab |
423 | 9 | simandl | echo ; echo "##### DEFAULT ROUTES IN TABLES #####" |
424 | | | $sl_ipcmd ro ls ta all | $sl_grepcmd table | $sl_grepcmd -v local | $sl_trcmd " " "\t" |
425 | | | echo ; echo "##### RULES FOR IPS #####" |
426 | | | $sl_ipcmd ru ls | $sl_trcmd " " "\t" |
427 | | | echo "</pre>" |
428 | 7 | simandl | } # s_report |
429 | 8 | simandl | |
430 | 7 | simandl | ###################################################################### |
431 | | | s_help() |
432 | | | { |
433 | | | echo Pouziti: sedlo [param] |
434 | | | echo param: |
435 | | | echo -V vypise verzi |
436 | | | echo -help vypise napovedu |
437 | | | echo -v malo upovidany |
438 | | | echo -vv hodne upovidany |
439 | | | echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
440 | | | echo -report vypise prehled pravidel a tabulek |
441 | | | echo -flru odstrani vsechny pravidla |
442 | 11 | simandl | echo -force bezpodminecne znovu obnovi vsechny pravidla |
443 | 7 | simandl | } # s_help |
444 | 8 | simandl | |
445 | 7 | simandl | ###################################################################### |
446 | | | ###################################################################### |
447 | | | |
448 | | | sl_unknown="" |
449 | | | scm_nogetcfg=0 |
450 | | | scm_flru=0 |
451 | | | scm_info=0 |
452 | 11 | simandl | scm_force=0 |
453 | | | sl_diffigw=0 |
454 | | | sl_difftbl=0 |
455 | 7 | simandl | |
456 | | | # parsing input parameters |
457 | | | while [ "a$1" != "a" ] |
458 | | | do |
459 | | | case $1 in |
460 | | | -V) |
461 | | | s_version |
462 | | | exit 0 |
463 | | | ;; |
464 | | | -h) |
465 | | | s_help |
466 | | | exit 0 |
467 | | | ;; |
468 | | | -report) |
469 | | | s_report |
470 | | | exit 0 |
471 | | | ;; |
472 | | | -flru) |
473 | | | scm_flru=1 |
474 | | | shift |
475 | | | ;; |
476 | 11 | simandl | -force) |
477 | | | scm_force=1 |
478 | | | shift |
479 | | | ;; |
480 | 7 | simandl | -help) |
481 | | | s_help |
482 | | | exit 0 |
483 | | | ;; |
484 | | | -nogetcfg) |
485 | | | scm_nogetcfg=1 |
486 | | | shift |
487 | | | ;; |
488 | | | -v) |
489 | | | scm_info=1 |
490 | | | shift |
491 | | | ;; |
492 | | | -vv) |
493 | | | scm_info=2 |
494 | | | shift |
495 | | | ;; |
496 | | | *) |
497 | | | sl_unknown="$sl_unknown$1 " |
498 | | | shift |
499 | | | esac |
500 | | | done |
501 | | | |
502 | | | # printing the list of bad parameters (if there are some) |
503 | | | if [ "a$sl_unknown" != "a" ] |
504 | | | then |
505 | | | echo "$slm_unknown $sl_unknown" |
506 | | | s_help |
507 | | | exit 0 |
508 | | | fi |
509 | | | |
510 | | | if [ $scm_flru -eq 1 ] |
511 | | | then |
512 | 16 | simandl | s_flruall |
513 | 7 | simandl | exit 0 |
514 | | | fi |
515 | | | |
516 | | | if [ $scm_nogetcfg -eq 0 ] |
517 | | | then |
518 | | | s_getcfg |
519 | | | fi |
520 | | | |
521 | | | s_mktables |
522 | | | s_filltables |
523 | 15 | simandl | #toto je pro ladici ucely |
524 | 11 | simandl | #echo $sl_difftbl |
525 | | | #echo $sl_diffcfg |
526 | | | #echo $sl_diffigw |
527 | | | #echo $scm_force |
528 | 13 | simandl | #exit 0 |
529 | 15 | simandl | |
530 | 11 | simandl | #flushing and filling rules is done only when |
531 | | | #new table is created |
532 | | | #config is changed |
533 | | | #some igw dissapears or appears |
534 | | | #-force command line parameter was used |
535 | | | if [ $sl_difftbl -gt 0 ] || [ $sl_diffcfg -gt 0 ] || [ $sl_diffigw -gt 0 ] || [ $scm_force -gt 0 ] |
536 | 10 | simandl | then |
537 | | | s_fillrules |
538 | | | fi |
539 | | | |
540 | 16 | simandl | |
541 | 7 | simandl | exit 0 |