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