2 # Copyright 2013 Manuel Munz <freifunk at somakoma dot de>
3 # Licensed under the GNU General Public License (GPL) v3
4 # This script monitors the local internet gateway
6 .
/lib
/functions
/network.sh
8 #Exit if this script is already running
9 pid
="$(pidof ff_olsr_test_gw.sh)"
10 if [ ${#pid} -gt 5 ]; then
11 logger
-t gwcheck
"Gateway check script is already running, exit now"
15 #check if dyngw_plain is installed and enabled, else exit
16 dyngwplainlib
=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}`
17 if [ -n "$dyngwplainlib" ]; then
18 if [ "$(uci -q get $dyngwplainlib.ignore)" == 1 ]; then
25 #Exit if this script is already running
26 pid
="$(pidof ff_olsr_test_gw.sh)"
27 if [ ${#pid} -gt 5 ]; then
28 logger
-p debug
-t gwcheck
"Gateway check script is already running, exit now"
32 # exit if there is no defaultroute with metric=0 in main or gw-check table.
33 defroutemain
="$(ip r s |grep default |grep -v metric)"
34 defroutegwcheck
="$(ip r s t gw-check |grep default |grep -v metric)"
35 if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then
39 # get and shuffle list of testservers
40 testserver
="$(uci -q get freifunk-gwcheck.hosts.host)"
41 [ -z "$testserver" ] && echo "No testservers found, exit" && exit
43 testserver
="$(for t in $testserver; do echo $t; done | awk 'BEGIN {
51 for (i = 1; i <= NR; i++) {
52 n = int(rand() * (NR - i + 1)) + i;
59 for t
in $testserver; do
61 test=$
(wget
-q http
://$t/conntest.html
-O -|
grep "Internet_works")
62 if [ "$test" == "Internet_works" ]; then
66 logger
-p debug
-t gw-check
"Could not fetch http://$t/conntest.html"
72 echo "$(nslookup $1 2>/dev/null |grep 'Address' |grep -v '127.0.0.1' |awk '{ print $3 }')"
76 # this gets all dns servers for the wan interface. If ubus is not present (like on older
77 # openwrt versions before Attitude fallback to get these from /var/state/network.
80 if [ -x /bin
/ubus
]; then
81 network_get_dnsserver dns wan
83 dns
="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)"
90 if [ "$iw" == 0 ]; then
91 # check if we have a seperate routing table for our tests.
92 # If yes, move defaultroute to normal table and delete table gw-check
93 # Also delete ip rules to use table gw-check for our testhosts and wan dns servers
95 if [ -n "$defroutegwcheck" ]; then
96 ip r a
$defroutegwcheck
97 ip r d
$defroutegwcheck t gw-check
98 for host in $testserver; do
99 ips
="$(resolve $host)"
101 [ -n "$(ip ru s | grep "to
$ip lookup gw-check
")" ] && ip rule del to
$ip table gw-check
107 [ -n "$(ip ru s | grep "to
$d lookup gw-check
")" ] && ip rule del to
$d table gw-check
110 #ip r d default via 127.0.0.1 metric 100000
111 logger
-t gw-check
"Internet is available again, restoring default route ( $defroutegwcheck)"
115 # Check failed. If we have a defaultroute with metric=0 and it is already in table gw-check then do nothing.
116 # If there is a defaultroute with metric=0 then remove it from the main routing table and add to table gw-check.
117 # Also setup ip rules to use table gw-check for our testhosts and wan dns servers
119 if [ -z "$(ip ru s | grep gw-check)" -a -n "$defroutemain" ]; then
120 ip r a
$defroutemain table gw-check
123 for host in $testserver; do
124 ips
="$(resolve $host)"
126 [ -z "$(ip ru s | grep "to
$ip lookup gw-check
")" ] && ip rule add to
$ip table gw-check
131 [ -z "$(ip ru s | grep "to
$d lookup gw-check
")" ] && ip rule add to
$d table gw-check
133 #ip r a default via 127.0.0.1 metric 100000
134 logger
-t gw-check
"Internet is not available, deactivating the default route ( $defroutemain)"