network – Run script every time the system’s default gateway changes?

I’d like to run a script every time my mac’s default gateway changes. For example, I’d like it to run if my internet connection changes from Wi-Fi to ethernet, if it disconnects or reconnects, or if I connect or disconnect from a VPN.

On a more technical level, I’d like the script to run when an entry is added, removed, or modified in the system routing table for default or 0/1, and possibly a few other relevant entries. Something that runs any time the routing table changes would be acceptable too, since I could just detect if the change is something I care about.

Is there any way to do this, other than just polling at some short interval and checking to see if the default gateway has changed?