Run a script based on network / DHCP changes
Sep 29, '04 09:47:00AM • Contributed by: digid
I needed a way to run a Unix script any time my IP address changed, either when moving from one network to another, or when my DHCP-assigned IP refreshed. After doing a bit of hunting around, I discovered that the /System -> Library -> SystemConfiguration -> Kicker.bundle -> Contents -> Resources -> set-hostname script was run during all of these events. It is also run by just unplugging and replugging the Ethernet cable. I modified the script to call another script that I keep in /usr/local/bin called
networkchanged. I added this code to the end of
set-hostname:
if [ -x /usr/local/bin/networkchange ] ; then
/usr/local/bin/networkchange
fi
The only command that follows this addition is the
exit 0 from the original script. You could put all your new commands into the original
set-hostname, but it is easier for me to remember the script is in /usr/local/bin with all my other custom scripts. Don't forget to create a backup of the original script in case you need it later.
The script that I created does a bunch of different things based on the Network or IP address that the Mac is currently using. Things like mounting file systems, adding static routes, etc.