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