Run script on wifi/lan connection change on MacOS

Veröffentlicht von

TLDR; Upon network change, update dyndns (afraid.org) by running a curl command

Since my servers access are additionally secured by allowing access only from a specific hostname, it is necessary to update my dynamic dns (I am using afraid.org which is working flawless and is cheap too) whenever my network connection changes (and apparently if other system configuration settings are changed, which also might involve VPNs, Proxies or non-network related settings).

It might also be used to run an external script, but in my case it is sufficient to do a curl request to a specific URL from my laptop.

--BEGIN org.afraid.freedns.plist--
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>org.afraid.freedns</string>
        <key>Program</key>
        <string>/usr/bin/curl</string>
        <key>ProgramArguments</key>
        <array>
                <string>curl</string>
                <string>-s</string>
                <string>http://sync.afraid.org/u/YOURTOKEN/</string>
        </array>
        <key>WatchPaths</key>
        <array>
                <string>/Library/Preferences/SystemConfiguration</string>
        </array>
</dict>
</plist>
--END org.afraid.freedns.plist--

Save this as org.afraid.freedns.plist to Library/LaunchAgents/com.afraid.dyndns.updater.plist and you’re good to go.