-
Notifications
You must be signed in to change notification settings - Fork 0
/
modem_check.sh
executable file
·228 lines (176 loc) · 5.65 KB
/
modem_check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/sh
# script found on the internet and heavily altered to fit my needs
# the script was written for a dd-wrt, I'm using tomato
# further more I encounter lots of problems which I totally wish to fix
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin:/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin
############################################################
# settings
############################################################
ident=modemcheck
pinghost=8.8.8.8
pinghostname=google-public-dns-a.google.com
# involved equipment
# wan
wanip=<your external ip address>
wanname=<fqdn of your external ip>
# modem
modemip=<ip address of your copperjet> # default: 172.19.3.1
modemname=<name of your copperjet>
modemuser=admin
modempass=admin # BBned Alice password: bb@l1cE322
# router
routerip=`ifconfig vlan1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
routername=<name of your wrt>
# threshold before reseting modem
zipb_threshold=5
inet_threshold=3
############################################################
# Load functions from seperate file
. f_zipb.sh
############################################################
# main script starts here
############################################################
# We'll have to figure out a way to make this setup stable. We're doing some checks
# - See if we can reach aapzak.nl website from here
# - See if we can reach external websites
# If external becomes unreachable, the modem probably has lost its connection
# the router won't be connecting automaticly till the next scheduled dhcp renew
# After modem reset:
# - %%ZIPB-0-ACTIVE%% Zipb is configured correctly
# - %%INET-5-UNREACABLE%% External websites are unreachable
#
# a dhcp renew should be performed on the router
# After dhcp renew inet is reachable but zipb seems to be disabled
# - %%ZIPB-5-INACTIVE%% Zipb is not configured correctly
# - %%INET-0-REACABLE%% External websites are reachable
#
# zipb should be reconfigured on the modem
# inet zipb result
# 0 0 check/fix modem, fix internet, fix zipb
# 0 1 modem lost connection, renew lease
# 1 0 check/fix modem, fix zipb
# 1 1 do nothing
# What should the checks be like?
# outside reachable?
# yes:are we external interface?
# yes:OK, exit
# no: can we reach modem?
# yes:fix zipb, exit
# no: dhcpc_renew, exit
# no: modem reachable?
# yes:fix internet, exit
# no: dhcpc_renew, exit
# do all checks first:
# create file to keep track of failures:
touch /tmp/fail_count
# check and set external reachability
result_ext=`ping -c 1 $pinghost | grep -c from`
if [ $result_ext == 1 ]; then
log "%%NET-0-REACHABILITY%% $pinghost reachable"
sed -i '/e/d' /tmp/fail_count
# see if dns is working
result_dns=`ping -c 1 $pinghostname | grep -c from`
if [ $result_dns == 1 ]; then
log "%%NET-0-REACHABILITY%% $pinghostname reachable"
sed -i '/d/d' /tmp/fail_count
else
# $pinghost not reachable
log "%%NET-5-REACHABILITY%% $pinghostname unreachable"
echo "d" >> /tmp/fail_count
fi
else
log "%%NET-5-REACHABILITY%% $pinghost unreachable"
echo "e" >> /tmp/fail_count
fi
# check and set modem reachability
result_modem=`ping -c 1 $modemip | grep -c from`
if [ $result_modem == 1 ]; then
log "%%ROUTER-0-REACHABILITY%% $modemname reachable"
sed -i '/m/d' /tmp/fail_count
else
log "%%ROUTER-5-REACHABILITY%% $modemname unreachable"
echo "m" >> /tmp/fail_count
fi
# check and set zipb status
if [ "$routerip" == "$wanip" ]; then
log "%%MODEM-0-ZIPB%% $routername has ip: $routerip"
sed -i '/z/d' /tmp/fail_count
else
log "%%MODEM-5-ZIPB%% $routername has ip: $routerip"
echo "z" >> /tmp/fail_count
fi
# outside reachable?
if [ $result_ext == 1 ]; then
# yes:are we external interface?
if [ "$routerip" == "$wanip" ]; then
# yes:OK, exit
log "all well, exit"
exit 0
else
# no: can we reach modem?
# check modem availability
if [ $result_modem == 1 ]; then
# yes:fix zipb, exit
log "fix zipb and get outta here"
# see how many failures we have
if [ `grep -c z /tmp/fail_count` -ge $zipb_threshold ]; then
# Three or more failures of trying to get zipb working
log "$zipb_threshold failures, resetting modem"
modem_reset
> /tmp/fail_count
else
log "didn't reach $zipb_threshold failures, lets try the easy way"
# show ourself to the modem
log "showing ourself to the modem"
dhcpc-renew ; sleep 5
# configure zipb completely
log "do a complete zipb config"
zipb_configure ; sleep 5
# reset zipb device
log "reset zipb"
zipb_reset ; sleep 5
# dhcp_renew
log "renew wan interface"
dhcpc-renew
fi
else
# no: dhcpc_renew, exit
log "dhcp renew and exit"
dhcpc-release; sleep 2 ; dhcpc-renew
exit 1
fi
fi
else
# no: modem reachable?
# start with renewing ip
log "no internet: renew dhcp"
dhcpc-release; sleep 5 ; dhcpc-renew; sleep 5
routerip=`ifconfig vlan1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
log "routerip after renew: $routerip"
# check and set modem reachability
result_modem=`ping -c 1 $modemip | grep -c from`
if [ $result_modem == 1 ]; then
log "%%ROUTER-0-REACHABILITY%% $modemname reachable"
sed -i '/m/d' /tmp/fail_count
else
log "%%ROUTER-5-REACHABILITY%% $modemname unreachable"
echo "m" >> /tmp/fail_count
fi
if [ $result_modem == 1 ]; then
# yes:fix internet, exit
log "wow, gtg fix the internet"
# be patient, no instant resetting:
# see how many failures we have
if [ `grep -c e /tmp/fail_count` -ge $inet_threshold ]; then
# Three or more failures of pinging the outside
log "$inet_threshold failures, resetting modem"
modem_reset
> /tmp/fail_count
else
log "didn't reach $inet_threshold failures, lets wait"
fi
else
exit 1
fi
fi
exit 0