1 | 7 | simandl | #!/bin/bash |
2 | | | # author : Petr Simandl www.simandl.cz |
3 | | | # release date : 07/09/2004 |
4 | | | # name : sedlo |
5 | | | # description : dynamic side routing tables tool |
6 | | | # license : GPL |
7 | | | |
8 | 8 | simandl | #changelog |
9 | | | # - ip ro fl ca |
10 | | | # - myigw |
11 | 7 | simandl | |
12 | 8 | simandl | sl_version="0.0.3pre7" |
13 | | | |
14 | 7 | simandl | PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin |
15 | | | |
16 | | | sl_nmcnf="sedlo.conf" |
17 | | | sl_sedlocnf="/etc/$sl_nmcnf" |
18 | | | sl_sedlocache="/var/cache/sedlo" |
19 | | | |
20 | | | sl_rttab="/etc/iproute2/rt_tables" |
21 | | | sl_rtnmin=110 |
22 | | | sl_rtnmax=200 |
23 | | | sl_ipnodef="10.0.0.0/8" |
24 | | | |
25 | | | slm_unknown="Nezname parametry : " |
26 | | | |
27 | | | sl_ipcmd=`which ip` |
28 | | | sl_trcmd=`which tr` |
29 | | | sl_wgetcmd=`which wget` |
30 | | | sl_hnmcmd=`which hostname` |
31 | | | sl_awkcmd=`which awk` |
32 | | | sl_catcmd=`which cat` |
33 | | | sl_grepcmd=`which grep` |
34 | | | |
35 | | | if [ -e $sl_sedlocnf ] |
36 | | | then |
37 | | | sl_nop=1 |
38 | | | else |
39 | | | echo "$sl_sedlocnf not found" |
40 | | | exit 1 |
41 | | | fi |
42 | | | |
43 | | | if [ -e $sl_rttab ] |
44 | | | then |
45 | | | sl_nop=1 |
46 | | | else |
47 | | | echo "$sl_rttab not found" |
48 | | | exit 1 |
49 | | | fi |
50 | | | |
51 | | | sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2}'` |
52 | | | |
53 | | | ###################################################################### |
54 | 8 | simandl | s_maru() |
55 | 7 | simandl | { |
56 | | | if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi |
57 | | | |
58 | | | sl_rules=`($sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_awkcmd '{print $3"*ru"}' ; \ |
59 | | | $sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*ip"}') | sort | uniq` |
60 | | | |
61 | | | sl_merged=`echo $sl_rules $sl_ips | sort | uniq` |
62 | | | |
63 | | | echo $sl_merged |
64 | | | |
65 | 8 | simandl | } # s_maru |
66 | 7 | simandl | ###################################################################### |
67 | | | s_flru() |
68 | | | { |
69 | | | if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi |
70 | | | |
71 | | | sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'` |
72 | | | |
73 | | | for sl_rule in $sl_rules |
74 | | | do |
75 | | | # sl_ipn=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2}'` |
76 | | | sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'` |
77 | | | $sl_ipcmd ru del $sl_ipgws |
78 | | | done |
79 | | | |
80 | 8 | simandl | #this should make faster applying of new routing tables |
81 | | | $sl_ipcmd ro flush cache |
82 | | | |
83 | 7 | simandl | } # s_flru |
84 | 8 | simandl | |
85 | 7 | simandl | ###################################################################### |
86 | | | s_fillrules() |
87 | | | { |
88 | 8 | simandl | |
89 | | | #flush old rules before filling new ones |
90 | | | #not so nice solution - it is planned to change just what's necessary |
91 | | | #by s_maru |
92 | 7 | simandl | s_flru |
93 | | | |
94 | | | if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi |
95 | | | |
96 | | | sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
97 | | | |
98 | | | for sl_ip in $sl_ips |
99 | | | do |
100 | | | sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
101 | | | sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
102 | | | sl_ok="no" |
103 | | | for sl_ipgw in $sl_ipgws |
104 | | | do |
105 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
106 | | | if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
107 | | | then |
108 | | | if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi |
109 | | | $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw |
110 | | | sl_ok="yes" |
111 | | | else |
112 | | | if [ "$sl_ok" = "no" ] |
113 | | | then |
114 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
115 | | | else |
116 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
117 | | | fi |
118 | | | fi |
119 | | | done |
120 | | | done |
121 | | | |
122 | | | $sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main |
123 | | | |
124 | 8 | simandl | #this should make faster applying of new routing tables |
125 | | | $sl_ipcmd ro flush cache |
126 | | | |
127 | 7 | simandl | } # s_fillrules |
128 | 8 | simandl | |
129 | 7 | simandl | ###################################################################### |
130 | | | s_filltables() |
131 | | | { |
132 | | | if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
133 | | | |
134 | | | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'` |
135 | | | |
136 | | | for sl_igw in $sl_igws |
137 | | | do |
138 | | | sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
139 | | | sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
140 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'` |
141 | | | |
142 | | | # equal cost multipath detection - just first is taken |
143 | | | if [ "$sl_igwgt x" = "zebra x" ] |
144 | | | then |
145 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'` |
146 | | | fi |
147 | | | |
148 | | | if [ "$sl_igwgt x" = " x" ] |
149 | | | then |
150 | | | if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi |
151 | | | else |
152 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
153 | | | if [ "$sl_tbl x" != " x" ] |
154 | | | then |
155 | | | $sl_ipcmd ro fl ta $sl_igwn |
156 | | | fi |
157 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
158 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
159 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi |
160 | | | fi |
161 | | | done |
162 | | | |
163 | 8 | simandl | #this should make faster applying of new routing tables |
164 | | | $sl_ipcmd ro flush cache |
165 | | | |
166 | 7 | simandl | } # s_filltables |
167 | 8 | simandl | |
168 | 7 | simandl | ###################################################################### |
169 | | | s_mktables() |
170 | | | { |
171 | | | if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi |
172 | | | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'` |
173 | | | for sl_igw in $sl_igws |
174 | | | do |
175 | | | sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
176 | | | if [ "$sl_igwrttb x" = " x" ] |
177 | | | then |
178 | | | if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
179 | | | sl_cnt="$sl_rtnmax" |
180 | | | sl_ok="no" |
181 | | | until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
182 | | | do |
183 | | | #space is used to recognized two and three digit numbers |
184 | | | sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
185 | | | if [ "$sl_igwrttb x" = " x" ] |
186 | | | then |
187 | | | sl_ok="yes" |
188 | | | echo "$sl_cnt $sl_igw" >> $sl_rttab |
189 | | | fi |
190 | | | sl_cnt=$(($sl_cnt - 1 )) |
191 | | | done |
192 | | | else |
193 | | | if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
194 | | | fi |
195 | | | |
196 | | | done |
197 | | | } # s_mktables |
198 | 8 | simandl | |
199 | 7 | simandl | ###################################################################### |
200 | | | s_getcfg() |
201 | | | { |
202 | | | if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
203 | | | if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
204 | | | if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
205 | | | |
206 | 8 | simandl | wget -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" |
207 | 7 | simandl | |
208 | | | if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ] |
209 | | | then |
210 | | | date > $sl_sedlocache/last_getcnf.txt |
211 | | | cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main |
212 | | | if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi |
213 | | | else |
214 | 8 | simandl | if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi |
215 | | | echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt |
216 | 7 | simandl | date >> $sl_sedlocache/last_getcnf.txt |
217 | | | fi |
218 | | | |
219 | | | # preparing cached config from local and main |
220 | | | # the local config should be processed as the second to have |
221 | | | # higher priority for rules from local config |
222 | | | |
223 | | | echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
224 | | | for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf` |
225 | | | do |
226 | | | cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
227 | | | cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
228 | | | cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
229 | | | done |
230 | 8 | simandl | |
231 | | | #local gateways taken from local config |
232 | | | cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
233 | | | |
234 | 7 | simandl | #cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
235 | | | #mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
236 | | | |
237 | | | } |
238 | 8 | simandl | |
239 | 7 | simandl | ###################################################################### |
240 | | | s_version() |
241 | | | { |
242 | | | echo sedlo $sl_version |
243 | | | } # s_version |
244 | 8 | simandl | |
245 | 7 | simandl | ###################################################################### |
246 | | | s_report() |
247 | | | { |
248 | | | echo "##### SEDLO #####" |
249 | | | echo "date : `date`" |
250 | | | echo "version : $sl_version" |
251 | | | echo "local_config : $sl_sedlocnf" |
252 | | | echo "main_config : $sl_murlcfg" |
253 | | | echo "last update : `cat $sl_sedlocache/last_getcnf.txt`" |
254 | | | echo "##### TABLES #####" |
255 | | | cat $sl_rttab |
256 | | | echo "##### RULES #####" |
257 | | | $sl_ipcmd ru ls |
258 | | | } # s_report |
259 | 8 | simandl | |
260 | 7 | simandl | ###################################################################### |
261 | | | s_help() |
262 | | | { |
263 | | | echo Pouziti: sedlo [param] |
264 | | | echo param: |
265 | | | echo -V vypise verzi |
266 | | | echo -help vypise napovedu |
267 | | | echo -v malo upovidany |
268 | | | echo -vv hodne upovidany |
269 | | | echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
270 | | | echo -report vypise prehled pravidel a tabulek |
271 | | | echo -flru odstrani vsechny pravidla |
272 | | | } # s_help |
273 | 8 | simandl | |
274 | 7 | simandl | ###################################################################### |
275 | | | ###################################################################### |
276 | | | |
277 | | | sl_unknown="" |
278 | | | scm_nogetcfg=0 |
279 | | | scm_flru=0 |
280 | | | scm_info=0 |
281 | | | |
282 | | | # parsing input parameters |
283 | | | while [ "a$1" != "a" ] |
284 | | | do |
285 | | | case $1 in |
286 | | | -V) |
287 | | | s_version |
288 | | | exit 0 |
289 | | | ;; |
290 | | | -h) |
291 | | | s_help |
292 | | | exit 0 |
293 | | | ;; |
294 | | | -report) |
295 | | | s_report |
296 | | | exit 0 |
297 | | | ;; |
298 | | | -flru) |
299 | | | scm_flru=1 |
300 | | | shift |
301 | | | ;; |
302 | | | -help) |
303 | | | s_help |
304 | | | exit 0 |
305 | | | ;; |
306 | | | -nogetcfg) |
307 | | | scm_nogetcfg=1 |
308 | | | shift |
309 | | | ;; |
310 | | | -v) |
311 | | | scm_info=1 |
312 | | | shift |
313 | | | ;; |
314 | | | -vv) |
315 | | | scm_info=2 |
316 | | | shift |
317 | | | ;; |
318 | | | *) |
319 | | | sl_unknown="$sl_unknown$1 " |
320 | | | shift |
321 | | | esac |
322 | | | done |
323 | | | |
324 | | | # printing the list of bad parameters (if there are some) |
325 | | | if [ "a$sl_unknown" != "a" ] |
326 | | | then |
327 | | | echo "$slm_unknown $sl_unknown" |
328 | | | s_help |
329 | | | exit 0 |
330 | | | fi |
331 | | | |
332 | | | if [ $scm_flru -eq 1 ] |
333 | | | then |
334 | | | s_flru |
335 | | | exit 0 |
336 | | | fi |
337 | | | |
338 | | | if [ $scm_nogetcfg -eq 0 ] |
339 | | | then |
340 | | | s_getcfg |
341 | | | fi |
342 | | | |
343 | | | s_mktables |
344 | | | s_filltables |
345 | | | s_fillrules |
346 | | | |
347 | | | exit 0 |
348 | | | |
349 | | | |