Script to stop the WiFI radio if WAN connection is down

I'd like to have a script that shuts off the WiFi radio if the WAN in unreachable. I am thinking that I could could a ping command to establish if the WAN is reachable and then use a call to /sbin/wifi to stop or start depending on the result.

To be clear, the ping command is probably a poor choice. I'd prefer some direct measure of WAN (on my router is eth0.2).

  1. Is there is better test for WAN connectivity?
  2. Is there a better toggle of the radio?
    2b) If not, how can I call /sbin/wifi to check the status of the radio and bring it up/down depending on the current state?

Something like this:

#!/bin/ash

if ! ping -q -W1 -c1 1.1.1.1 1>/dev/null; then
  # WAN is down
  /sbin/wifi down
else
  /bin/true
  # ping is successful so take no action
  # need to add a check to see radio status and turn on if not
fi 

The travelmate package does this based on wifi connected. It can also scan for several configured APs / networks and connect to the one that you've given highest priority which is in radio range.

I also think same.

#!/bin/sh
[ "$INTERFACE" = wwan ] || exit 0
[ "$ACTION" = ifup -o "$ACTION" = ifupdate ] || exit 0
if [ -n "$(iw wlan1 link)" ]; then
	uci set wireless.wifi_sta.disabled=1
	uci commit wireless
	wifi
fi