3 . /lib/functions/network.sh
5 #Exit if this script is already running
6 pid="$(pidof ff_olsr_test_gw.sh)"
7 if [ ${#pid} -gt 5 ]; then
8 logger -t gwcheck "Gateway check script is already running, exit now"
12 #check if dyngw_plain is installed and enabled, else exit
13 dyngwplainlib=`uci show olsrd |grep dyn_gw_plain |awk {' FS="."; print $1"."$2 '}`
14 if [ -n "$dyngwplainlib" ]; then
15 if [ "$(uci -q get $dyngwplainlib.ignore)" == 1 ]; then
19 echo "dyngw_plain not found in olsrd config, exit"
24 # check if we have a defaultroute with metric=0 in one of these tables: main table and gw-check table.
26 defroutemain="$(ip r s |grep default |grep -v metric)"
27 defroutegwcheck="$(ip r s t gw-check |grep default |grep -v metric)"
28 if [ -z "$defroutegwcheck" -a -z "$defroutemain" ]; then
32 # get and shuffle list of testservers
33 testserver="$(uci -q get freifunk-gwcheck.hosts.host)"
34 [ -z "$testserver" ] && echo "No testservers found, exit" && exit
36 testserver="$(for t in $testserver; do echo $t; done | awk 'BEGIN {
44 for (i = 1; i <= NR; i++) {
45 n = int(rand() * (NR - i + 1)) + i;
52 for t in $testserver; do
54 test=$(wget -q http://$t/conntest.html -O -| grep "Internet_works")
55 if [ "$test" == "Internet_works" ]; then
59 logger -t gw-check "Could not get test file from http://$t/conntest.html"
65 echo "$(nslookup $1 2>/dev/null |grep 'Address' |grep -v '127.0.0.1' |awk '{ print $3 }')"
69 # this gets all dns servers for the wan interface. If ubus is not present (like on older
70 # openwrt versions before Attitude fallback to get these from /var/state/network.
73 if [ -x /bin/ubus ]; then
74 network_get_dnsserver dns wan
76 dns="$(grep network.wan.resolv_dns /var/state/network | cut -d "=" -f 2)"
83 if [ "$iw" == 0 ]; then
84 # check if we have a seperate routing table for our tests.
85 # If yes, move defaultroute to normal table and delete table gw-check
86 # Also delete ip rules to use table gw-check for our testhosts and wan dns servers
88 if [ -n "$defroutegwcheck" ]; then
89 ip r a $defroutegwcheck
90 ip r d $defroutegwcheck t gw-check
91 ip ru del fwmark 0x2 lookup gw-check
92 for host in $testserver; do
93 ips="$(resolve $host)"
95 [ -n "$(ip ru s | grep "to $ip lookup gw-check")" ] && ip rule del to $ip table gw-check
101 [ -n "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule del to $d table gw-check
104 #ip r d default via 127.0.0.1 metric 100000
105 logger -t gw-check "Internet is available again, restoring default route ( $defroutegwcheck)"
109 # Check failed. If we have a defaultroute with metric=0 and it is already in table gw-check then do nothing.
110 # If there is a defaultroute with metric=0 then remove it from the main routing table and add to table gw-check.
111 # Also setup ip rules to use table gw-check for our testhosts and wan dns servers
113 if [ -z "$(ip ru s | grep gw-check)" -a -n "$defroutemain" ]; then
114 ip rule add fwmark 0x2 lookup gw-check
115 ip r a $defroutemain table gw-check
118 for host in $testserver; do
119 ips="$(resolve $host)"
121 [ -z "$(ip ru s | grep "to $ip lookup gw-check")" ] && ip rule add to $ip table gw-check
126 [ -z "$(ip ru s | grep "to $d lookup gw-check")" ] && ip rule add to $d table gw-check
128 #ip r a default via 127.0.0.1 metric 100000
129 logger -t gw-check "Internet is not available, deactivating the default route ( $defroutemain)"