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