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