Maintaining a Dynamic DNS automatically.

Aug 29, '11 07:30:00AM

Contributed by: SOX

Dynamic DNS services provide you with a domain name you can use with a non-static IP address, like most home computers have. To use these requires some action to periodically contact the DNS server and let it know if your Dynamic IP address changed. Usually there are complicated scripts provided to do this, and MacOSXHints has published a few.

However one can greatly simplify this with a launchDaemon and a single built in OSX unix command.

First, this assumes that you have set up an account with a Dynamic DNS service. I use the free service provided by FreeDNS. One updates this simply by loading a web page URL. The act of making that page request alerts the DynamicDNS of your IP address and it updates.

Thus all we need is a LaunchDaemon that runs every hour that reloads that page. Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>dynamic_dns_updater</string>
	<key>Disabled</key>
	<false/>
 <key>UserName</key>
 <string>nobody</string>
	<key>StartInterval</key>
		<integer>3600</integer>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/bin/curl</string>
		<string>--silent</string>
		<string>--insecure</string>
		<string>https://freedns.afraid.org/dynamic/update.php?YKIJUSiikf83kfj8dhfhuee9jdjd9ejfj99fj9jslRRRT</string>
	</array>
</dict>
</plist>
You will need to edit the above example an insert the key for your registered dynamic DNS account in the above string GET request. The one above is not for your site. You can find this at FreeDNS by simply manually refreshing your site online and then looking at the URL that it used. Other dynamicDNS services will have other URL structures that you need to query for the update.

You save this in the location:

/Library/LaunchDaemons/dynamic_dns_updater.plist

then load it:
sudo launchctl load /Library/LaunchDaemons/dynamic_dns_updater.plist
sudo launchctl start dynamic_dns_updater
And that's it. This will perpetually update your dynamic DNS whenever the computer is awake.

To remove it permanently:
sudo launchctl stop dynamic_dns_updater
sudo launchctl unload /Library/LaunchDaemons/dynamic_dns_updater.plist
rm /Library/LaunchDaemons/dynamic_dns_updater.plist
A few caveats on getting this to work. First, if you just download that plist file above, depending on how you do this, you may end up with some unwanted extended file attributes. The launch Daemon service won't run files with the wrong permissions or attributes. You can inspect these by listing the file like this:

ls -laeo@ /Library/LaunchDaemons/

if you see any below the file you can then manually delete these with the xattr command. For example, if there is quarantine from the download you can get rid of it like this.
sudo xattr -d com.apple.quarantine /Library/LaunchDaemons/dynamic_dns_update.plist
The plist should be owned by root:wheel.

[crarko adds: I don't use Dynamic DNS, so I haven't tried this. But stepping through the code makes it look safe to try if you do.]

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110812232611102