#!/bin/sh . /lib/functions.sh . /lib/network/utils.sh NETCON=0 WANIP=0 LASTSTATUS="" CURSTATUS="" SLEEP_TIME=30 test_connection() { local addr="$1" local defroute="$(ip r | grep default | awk '{print$3}' | head -1)" local def6route="$(ip -f inet6 r | grep default | awk '{print$3}')" local ping6dev="$(ip -f inet6 r | grep default | awk '{print$5}')" if [ -n "$addr" ]; then ping -q -w 5 -c 1 $addr >/dev/null 2>&1 && return 0 elif [ -n "$defroute" ]; then ping -q -w 5 -c 1 $defroute >/dev/null 2>&1 && return 0 for nmsrv in $(grep nameserver /var/resolv.conf.auto | awk '{print$2}'); do ping -q -w 5 -c 1 $nmsrv >/dev/null 2>&1 && return 0 done elif [ -n "$def6route" -a -n "$ping6dev" ]; then ndisc6 -w 5 -1 $def6route $ping6dev >/dev/null 2>&1 && return 0 fi return 1 } uplink_test() { uplink_phy_connected if [ $? -eq 0 ]; then ubus call led.wan set '{"state" : "ok"}' || ubus call led.broadband set '{"state" : "ok"}' else ubus call led.wan set '{"state" : "off"}' || ubus call led.broadband set '{"state" : "off"}' fi } internet_test() { local link dest dest="$(uci -q get diagnostics.@connectivity[0].destination)" test_connection $dest if [ "$?" -eq 0 ]; then NETCON=1 ubus call led.internet set '{"state" : "ok"}' echo "{ \"online\" : true }" > /tmp/internet_connection_status else NETCON=0 ubus call led.internet set '{"state" : "error"}' echo "{ \"online\" : false }" > /tmp/internet_connection_status fi } connectivity_test() { uplink_test internet_test if [ $NETCON -eq 1 ]; then CURSTATUS=1 [ "$CURSTATUS" == "$LASTSTATUS" ] || ubus send internet '{"status" : "online"}' LASTSTATUS=1 else CURSTATUS=0 [ "$CURSTATUS" == "$LASTSTATUS" ] || ubus send internet '{"status" : "offline"}' LASTSTATUS=0 fi } if [ "$1" == "once" ]; then connectivity_test exit 0 fi while true; do connectivity_test sleep $SLEEP_TIME done