Create a DIY dynamic DNS service, for free

I’m one of the unlucky people with a provider who doesn’t provide a static IP address. One solution would be to subscribe to a Dynamic DNS (or DDNS) service like DynDNS. Those services are not free (anymore), and although the subscription price is not too steep, it does not justify for the limited use I have.

A little scripting provided a solution which works for me too. I previously ran this on a Mac, but since I have a Raspberry Pi running anyway, the script is now scheduled on the Raspberry Pi with the Raspbian OS  installed.

First, I need to obtain my external IP address. There are plenty of free services. I found a little script that does the job for me:

curl -L -s --max-time 10 http://checkip.dyndns.org | 
      egrep -o -m 1 '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'

It fetches the webpage from the DynDNS site and strips out  the IP address using a regular expression.

I created a quick and dirty shell script that creates a file that contains some simple HTML with a redirect to my public IP. The generated file is then uploaded to my hosting provider:

printf "<html><head><meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://" > redirect.html
curl -L -s --max-time 10 http://checkip.dyndns.org | egrep -o -m 1 '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' >>redirect.html
printf "\"></head><body>Redirecting ...</body></html>" >> redirect.html
curl -u user:password -T redirect.html ftp://ftp.mysite:21

Not the most elegant code, but it works. I can run this manually or schedule it with launchdcron or something similar. If necessary you can even manually create the HTML file and upload it yourself from time to time.

The final step I took was make sure the page redirects me to a webserver running locally in a virtual machine. I set one up in Virtualbox, using a Bridged Network so it acts as a full network citizen in my local network. Creating a port forwarding entry in my router ensures that all incoming requests on port 80 are redirected to port 7780 on the IP address of the virtual machine.

And so I’m done. Opening the page on my hosted site automatically redirects me to my local web server. At no cost and hardly any coding at all!

Addition January 1st., 2014:

I’ve found another trick that might not always work, but it rules out the need for an external service to display your external IP address.

The following line returns your external IP adres in OS X:

curl ifconfig.me

The script then becomes as simple as:

printf "<html><head><meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://" > redirect.html
curl ifconfig.me >>redirect.html
printf "\"></head><body>Redirecting ...</body></html>" >> redirect.html
curl -u user:password -T redirect.html ftp://ftp.mysite:21


3 Reacties

  1. Slightly above novice here, so let me try to understand what you are doing. Please correct me if I misunderstand. You run a short script on your home “Mac” that periodically updates your remote server with your current home IP address? Looking at what you wrote, all it’s doing is creating a simple html redirect back to your IP:port at home?
    So when you visit: “http://mysite.com/redirect.html” you get your local server?
    Sounds simple enough, think I’ll try it. Thanks

  2. That’s exactly the procedure. I should have explained that too in the blog. Hope it works for you!

Laat een reactie achter

Het e-mailadres wordt niet gepubliceerd.

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.

By using this site you acknowledge the use of cookies (which are mostly harmless, btw) More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below you are agreeing to these settings.

Close