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 launchd, cron 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