See this for reference on a script that takes time spent pinging into account: Leds on based on ping through specific interface, possible? - #11 by Cthulhu88
You also need to set brightness to 0 after setting trigger to none. If brightness was left at 1 (or 255) by the timer (or previously default-on), simply setting the trigger to "none" won't turn it off.
You can also improve the way you fetch the gateway address with this:
route -n | awk '($4 == "UG") {print $2}'