1 | 2 | simandl | #!/bin/bash |
2 | | | # author : Petr Simandl www.simandl.cz |
3 | 3 | simandl | # release date : 29/08/2004 |
4 | 2 | simandl | # name : sedlo |
5 | | | # description : dynamic side routing tables tool |
6 | | | # license : GPL |
7 | | | |
8 | 3 | simandl | sl_version="0.0.2" |
9 | 2 | simandl | |
10 | | | PATH=$PATH:$HOME/bin:/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 | 3 | simandl | sl_rtnmax=200 |
19 | | | sl_ipnodef="10.0.0.0/8" |
20 | 2 | simandl | |
21 | 3 | simandl | slm_unknown="Nezname parametry : " |
22 | | | |
23 | | | sl_ipcmd=`which ip` |
24 | | | sl_trcmd=`which tr` |
25 | | | sl_wgetcmd=`which wget` |
26 | 2 | simandl | sl_hnmcmd=`which hostname` |
27 | | | sl_awkcmd=`which awk` |
28 | 3 | simandl | sl_catcmd=`which cat` |
29 | | | sl_grepcmd=`which grep` |
30 | 2 | simandl | |
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 | 3 | simandl | s_fillrules() |
51 | 2 | simandl | { |
52 | 3 | simandl | if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi |
53 | 2 | simandl | |
54 | 3 | simandl | sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'` |
55 | 2 | simandl | |
56 | 3 | simandl | for sl_ip in $sl_ips |
57 | | | do |
58 | | | sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'` |
59 | | | sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'` |
60 | | | sl_ok="no" |
61 | | | for sl_ipgw in $sl_ipgws |
62 | 2 | simandl | do |
63 | 3 | simandl | sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw` |
64 | | | if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ] |
65 | 2 | simandl | then |
66 | 3 | simandl | sl_empty="no" |
67 | | | sl_cnt=3 |
68 | | | #removing until we have some rules in table but the maximum of cycles is limited |
69 | | | #to avoid neverending loop |
70 | | | while [ "$sl_empty" = "no" ] |
71 | | | do |
72 | | | sl_ipru=`$sl_ipcmd ru ls | $sl_grepcmd $sl_ipn` |
73 | | | if [ "$sl_ipru x" != " x" ] |
74 | 2 | simandl | then |
75 | 3 | simandl | if [ $scm_info -gt 1 ]; then echo "Removing old rules for $sl_ipn" ; fi |
76 | | | $sl_ipcmd ru del from $sl_ipn |
77 | | | $sl_ipcmd ru del from $sl_ipn to $sl_ipnodef |
78 | | | else |
79 | | | sl_empty="yes" |
80 | 2 | simandl | fi |
81 | | | sl_cnt=$(($sl_cnt - 1 )) |
82 | 3 | simandl | if [ "$sl_cnt" -eq -1 ] |
83 | | | then |
84 | | | sl_empty="yes" |
85 | | | echo "Too much cycles" |
86 | | | fi |
87 | | | done |
88 | | | if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi |
89 | | | $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw |
90 | | | $sl_ipcmd ru add from $sl_ipn to $sl_ipnodef lookup main |
91 | | | sl_ok="yes" |
92 | | | else |
93 | | | if [ "$sl_ok" = "no" ] |
94 | | | then |
95 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi |
96 | | | else |
97 | | | if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi |
98 | | | fi |
99 | | | fi |
100 | | | done |
101 | | | done |
102 | 2 | simandl | |
103 | 3 | simandl | } # s_fillrules |
104 | | | ###################################################################### |
105 | | | s_filltables() |
106 | | | { |
107 | | | if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi |
108 | | | |
109 | | | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'` |
110 | | | |
111 | | | for sl_igw in $sl_igws |
112 | | | do |
113 | | | sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'` |
114 | | | sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'` |
115 | | | sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'` |
116 | | | if [ "$sl_igwgt x" = " x" ] |
117 | | | then |
118 | | | if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table empty" ; fi |
119 | | | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
120 | | | if [ "$sl_tbl x" != " x" ] |
121 | | | then |
122 | | | $sl_ipcmd ro fl ta $sl_igwn |
123 | | | fi |
124 | 2 | simandl | else |
125 | 3 | simandl | sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn` |
126 | | | if [ "$sl_tbl x" != " x" ] |
127 | | | then |
128 | | | $sl_ipcmd ro fl ta $sl_igwn |
129 | | | fi |
130 | | | $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn |
131 | | | $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn |
132 | | | if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi |
133 | 2 | simandl | fi |
134 | 3 | simandl | done |
135 | 2 | simandl | |
136 | 3 | simandl | } # s_filltables |
137 | | | ###################################################################### |
138 | | | s_mktables() |
139 | | | { |
140 | | | if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi |
141 | | | sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'` |
142 | | | for sl_igw in $sl_igws |
143 | | | do |
144 | | | sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` |
145 | | | if [ "$sl_igwrttb x" = " x" ] |
146 | | | then |
147 | | | if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi |
148 | | | sl_cnt="$sl_rtnmax" |
149 | | | sl_ok="no" |
150 | | | until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] |
151 | | | do |
152 | | | #space is used to recognized two and three digit numbers |
153 | | | sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` |
154 | | | if [ "$sl_igwrttb x" = " x" ] |
155 | | | then |
156 | | | sl_ok="yes" |
157 | | | echo "$sl_cnt $sl_igw" >> $sl_rttab |
158 | | | fi |
159 | | | sl_cnt=$(($sl_cnt - 1 )) |
160 | | | done |
161 | | | else |
162 | | | if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi |
163 | | | fi |
164 | | | |
165 | 2 | simandl | done |
166 | 3 | simandl | } # s_mktables |
167 | 2 | simandl | ###################################################################### |
168 | | | s_getcfg() |
169 | | | { |
170 | 3 | simandl | if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi |
171 | | | if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi |
172 | | | if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi |
173 | 2 | simandl | wget $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main" -q |
174 | 3 | simandl | if [ -s $sl_sedlocache/$sl_nmcnf.main ] |
175 | | | then |
176 | | | sl_nop=1 |
177 | | | else |
178 | | | if [ $scm_info -gt 1 ]; then echo "Main config not found $sl_sedlocache/$sl_nmcnf.main" ; fi |
179 | | | fi |
180 | 2 | simandl | echo "# generated file" > $sl_sedlocache/$sl_nmcnf |
181 | | | for sl_file in `ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main ` |
182 | | | do |
183 | | | cat $sl_file | grep "^mcnf" | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf |
184 | | | cat $sl_file | grep "^igw" | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf |
185 | | | cat $sl_file | grep "^ip" | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf |
186 | | | done |
187 | | | cat $sl_sedlocache/$sl_nmcnf | sort | uniq > $sl_sedlocache/$sl_nmcnf.uniq |
188 | | | mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf |
189 | | | |
190 | | | } |
191 | | | ###################################################################### |
192 | | | s_version() |
193 | | | { |
194 | | | echo sedlo $sl_version |
195 | | | exit 0 |
196 | 3 | simandl | } # s_version |
197 | 2 | simandl | ###################################################################### |
198 | 3 | simandl | s_report() |
199 | | | { |
200 | | | echo "##### SEDLO #####" |
201 | | | echo "version : $sl_version" |
202 | | | echo "local_config : $sl_sedlocnf" |
203 | | | echo "main_config : $sl_murlcfg" |
204 | | | echo "##### TABLES #####" |
205 | | | cat $sl_rttab |
206 | | | echo "##### RULES #####" |
207 | | | $sl_ipcmd ru ls |
208 | | | exit 0 |
209 | | | } # s_report |
210 | | | ###################################################################### |
211 | 2 | simandl | s_help() |
212 | | | { |
213 | | | echo Pouziti: sedlo [param] |
214 | | | echo param: |
215 | | | echo -v vypise verzi |
216 | 3 | simandl | echo -help vypise napovedu |
217 | | | echo -info1 malo upovidany |
218 | | | echo -info2 hodne upovidany |
219 | | | echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache |
220 | | | echo -report vypise prehled pravidel a tabulek |
221 | 2 | simandl | exit 0 |
222 | 3 | simandl | } # s_help |
223 | 2 | simandl | ###################################################################### |
224 | | | ###################################################################### |
225 | | | |
226 | 3 | simandl | sl_unknown="" |
227 | | | scm_nogetcfg=0 |
228 | | | scm_info=0 |
229 | 2 | simandl | |
230 | 3 | simandl | # parsing input parameters |
231 | | | while [ "a$1" != "a" ] |
232 | | | do |
233 | | | case $1 in |
234 | | | -v) |
235 | | | s_version |
236 | | | ;; |
237 | | | -h) |
238 | | | s_help |
239 | | | ;; |
240 | | | -report) |
241 | | | s_report |
242 | | | ;; |
243 | | | -help) |
244 | | | s_help |
245 | | | ;; |
246 | | | -nogetcfg) |
247 | | | scm_nogetcfg=1 |
248 | | | shift |
249 | | | ;; |
250 | | | -info1) |
251 | | | scm_info=1 |
252 | | | shift |
253 | | | ;; |
254 | | | -info2) |
255 | | | scm_info=2 |
256 | | | shift |
257 | | | ;; |
258 | | | *) |
259 | | | sl_unknown="$sl_unknown$1 " |
260 | | | shift |
261 | | | esac |
262 | | | done |
263 | | | |
264 | | | # printing the list of bad parameters (if there are some) |
265 | | | if [ "a$sl_unknown" != "a" ] |
266 | | | then |
267 | | | echo "$slm_unknown $sl_unknown" |
268 | | | s_help |
269 | | | fi |
270 | | | |
271 | | | if [ $scm_nogetcfg -eq 0 ] |
272 | | | then |
273 | | | s_getcfg |
274 | | | fi |
275 | | | |
276 | 2 | simandl | s_mktables |
277 | 3 | simandl | s_filltables |
278 | | | s_fillrules |
279 | | | |
280 | 2 | simandl | exit 0 |
281 | 3 | simandl | |