Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run a script based on network / DHCP changes Network
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.
    •    
  • Currently 2.50 / 5
  You rated: 2 / 5 (6 votes cast)
 
[20,236 views]  

Run a script based on network / DHCP changes | 16 comments | Create New Account
Click here to return to the 'Run a script based on network / DHCP changes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Run a script based on network / DHCP changes
Authored by: weeksatinsead on Sep 29, '04 12:26:45PM

Interesting. Does this point to a way to tell a daemon like DNSUpdate to update the DNS servers whenever your dynamic IP address is modified?



[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: Anonymous on Sep 29, '04 03:11:55PM
Does this point to a way to tell a daemon like DNSUpdate to update the DNS servers whenever your dynamic IP address is modified?

This hint is really about modifying specific properties on a host based on a set of network changes. My understanding is that DNSUpdate is about updating DynDNS.org servers when running public services on a non-static IP. You wouldn't want it run everytime the kicker ran. You only want it to run when your ISP gives you a new IP address.

The real way to update internal DNS is to let the DHCP daemon contact BIND and update the zones via DDNS when a host changes.

[ Reply to This | # ]

Run a script based on network / DHCP changes
Authored by: sapporo on Sep 29, '04 02:20:20PM
Instead of modifying set-hostname, I guess you could also add the following to /System/Library/SystemConfiguration/Kicker.bundle/Contents/Resources/Kicker.xml:


	<dict>
		<key>execCommand</key>
		<string>/usr/local/bin/networkchange</string>
		<key>execUID</key>
		<integer>0</integer>
		<key>keys</key>
		<array>
			<string>Setup:/Network/HostNames</string>
			<string>State:/Network/Global/DNS</string>
		</array>
		<key>name</key>
		<string>networkchange</string>
	</dict>


[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: avengerpenguin on Sep 29, '04 06:07:57PM

That's pretty neat - do you have an URL which lists all the keys which may trigger a change for Kicker?



[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: sapporo on Sep 30, '04 06:20:12AM
Unfortunately not, but since Kicker is open source, someone should be able to figure it out.

[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: unixgeek on Oct 01, '04 11:53:52PM

This hint is exactly what I have been looking for. This is excellent information.

Is anyone able to expand the keys node? I recognize that the values are keys from configd, but in once case here it is State: rather than Setup:. I found another example on the net similar to this one on it used slightly different keys.

It would really be great if someone could explain the keys node.

Thanks!



[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: tickell on Apr 29, '05 12:20:52PM

Just an "I learned it the hard way" item:

When you update the Kicker.xml file to reflect the addition of your new script, you must kill the configd process, and restart it. (A reboot will do this for you, but there is no need to waste the time)

If you're new script exits with a non-zero status at any point, it appears configd/Kicker will not use it for future state changes. So, if you had a typo or some other issue with it, repeat the restart process after you change it.

Regards



[ Reply to This | # ]
Please post your script?
Authored by: alderete on Sep 29, '04 03:07:40PM

Would you mind posting the script you're running to illustrate some of the useful things that you can do with this hook? It seems like a neat thing, but I'm not clear what I would do myself with this...



[ Reply to This | # ]
Please post your script?
Authored by: jstripli on Oct 01, '04 03:30:36PM

This is nice for several reasons. For instance if you use your powerbook on a private (home) and public network (school, work) you can kick of a "secure" "unsecure" script... This would could do things like turn on the firewall, enable password protection on the screen saver etc...



[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: tofergregg on Sep 29, '04 08:37:44PM

Unless I'm reading this hint wrong, this could be used to change the default printer (a big one for me) when the network is changed. Cool! I'm going to work on it now...



[ Reply to This | # ]
network dis/re-connect problems
Authored by: CuriousOne on Sep 29, '04 11:36:46PM

Something tangetially related - I have recently found whenever the network connection is broken (intentionally or otherwise, even when sleeping then awakening) I rarely get re-connected. I have to restart the computer. My thought is something is wrong with the daemon that checks network connections. I was trying to ascertain how to find which daemon this is, and to check the appropriate log file to find out the problem. Anyone with suggestions how to troubleshoot this problem?



[ Reply to This | # ]
network dis/re-connect problems
Authored by: mwalker on Oct 01, '04 03:20:02PM

I have seen this, too, on my dual G5. I thought it was related to my DSL dropping (Qwest seems to like doing that in my area), but the Airport-connected machines come back up when the DSL does, but the ethernet-connected G5 doesn't. I've tried all sorts of network configurations, all to no avail.



[ Reply to This | # ]
Environment
Authored by: kholburn on Sep 30, '04 02:02:16AM

Are there any useful environment variables or calling arguments that you know about here?



[ Reply to This | # ]
Environment
Authored by: sapporo on Sep 30, '04 06:23:12AM
I guess you could find out by calling export in set-hostname or the script that gets triggered.

[ Reply to This | # ]
Run a script based on network / DHCP changes
Authored by: jamieo on Sep 30, '04 06:30:27AM

Since converting to macos I am really pleased how well it handles moving between networks and being able to hook into this makes it even better! How macos handles things like proxies based on your Location is very useful for switching between home and work. However not all apps use the system proxy settings - e.g. mozilla

What I'd like to do is, on a network switch be able to change my mozilla proxy settings. I know how to change the settings for mozilla, but how to tie in the calling of this script (which is effectively at system level) with my user?

I could just have the script update proxy settings for specific users, or I could look to see who is logged in and run some scripts in their home directory. Any ideas?



[ Reply to This | # ]
Is there a Tiger (10.4) analog to this?
Authored by: leono on May 02, '05 08:30:07AM

I used this functionality in Panther. Now that I've upgraded to Tiger, there's no clear analog for the now-missing set-hostname script. There's a promisingly-named script called enable-network in the same location, but it doesn't seem to be triggered by VPN connects/disconnects, which is when I need my script to fire. I haven't tested it for Location changes.

For now I'm running my script manually, but it's a pain to remember to do this every time I connect. I thought about trying to make a script to connect/disconnect from my VPN and then fire my custom routing script, but I can't find a CLI utility to do the VPN stuff. Arg.

Anyone have any help for me?

Thanks!



[ Reply to This | # ]