Auto run a shell script when connecting to a network? (wifi or utp)
I use a program named EventScripts which among the events it monitors, there are Network Events, i.e., Internet became available, Internet became unavailable, External IP address changed and Wireless network changed. Update: In the latest release there is also a Wireless network reconnected event not shown on the linked webpage.

It uses AppleScript scripts to preform the action(s) you want to have happen when a given event is triggered.

You can execute a shell command/script using the AppleScript do shell script command.

Not sure if any of these can be adopted for your use, and it will probably require some additional coding, either in AppleScript or shell script to validate the conditions are met for wanting the main shell script to run. Just wanted to offer a possible solution.



It is a paid app, $4.99 USD, but does have a demo version you can download to test with before purchasing.


Mac App Store Preview (US)
Download for Demo




Note: Other then being a paid user of this app, I have no affiliation with its developer.