The worst time for an IP address change to happen at home is while you are traveling. That is exactly what happened to me. I am in Florida for a week and I noticed I couldn’t connect to my websites, my email, or PBX system. I knew my server was up and it was communicating with the outside because I saw my APRS script check-ins posting to Twitter. So I called Time Warner Cable and explained the situation and the technical support representative was able to provide me my external IP address. First I updated all my domains to the new IP address and verified the phone system was back online. Then I started to think about this little problem. It is not the first time I have encountered this issue. When it does happen, it is usually while I am traveling. So I started to think about the best way to handle this situation. What I ended up doing is signing up with a free dynamic DNS service called No-IP. If you are going to follow this tutorial, I suggest you do the same to make it easier to follow along. Once the verification email came through and I created my dynamic DNS hostname, I found a Linux program that will update the IP address at No-IP for me. The problem with that is you have to run the program manually. I could just stick it into a cron job to run every 60 minutes, but why force an update unless it is needed? So I created a bash script that will compare the IP address of one of my domain names to my known external IP address. If they are different, it will update the dynamic DNS IP address and send me an email to let me know that there’s a discrepancy between the last known IP address and the current new IP address. So first you will want to download the No-IP Linux IP updater, untar the file, and make it.
cd /usr/local/src wget http://www.no-ip.com/client/linux/noip-duc-linux.tar.gz tar xzf noip-duc-linux.tar.gz cd no-ip-2.1.9-1 make make install
When you make install, it will run a basic guided setup to enter your username and password for No-IP and that’s all there is to that step. To test the program enter
/usr/local/bin/noip2 -i <your IP>
then verify your No-IP domain resolves to the correct IP address.
Now the script to do an hourly check and email if the IP address is not correct.
#!/bin/bash # Set old IP grabbed from domain and new IP grabbed from iCanHazIP.com oldIP=$(resolveip -s mytestbox.org) newIP=$(curl -s icanhazip.com) #This section sends the email if the IPs don't match. #Also it updates the IP of the dynamic domain if [ $oldIP != $newIP ]; then #Create blank email and add to and from headers in the email rm /tmp/ipcheck.mail touch /tmp/ipcheck.mail echo 'To: email@example.com' >> /tmp/ipcheck.mail echo 'From: firstname.lastname@example.org' >> /tmp/ipcheck.mail echo 'Subject: Action Required-Hourly IP Check FAILED' >> /tmp/ipcheck.mail echo 'Your new IP address is:' $newIP >> /tmp/ipcheck.mail echo '' >> /tmp/ipcheck.mail echo '' >> /tmp/ipcheck.mail cat /tmp/ipcheck.mail | msmtp -a default email@example.com #Update the No-IP address to the new one /usr/local/bin/noip2 -i $newIP #This section is optional. It will send an email to confirm there are no changes. #If you only want the email if the IPs are different delete the following lines, but #leave fi at the end. else #Create blank email and add to and from headers in the email rm /tmp/ipcheck.mail touch /tmp/ipcheck.mail echo 'To: firstname.lastname@example.org' >> /tmp/ipcheck.mail echo 'From: email@example.com' >> /tmp/ipcheck.mail echo 'Subject: Hourly IP Check - No Changes Detected' >> /tmp/ipcheck.mail #Add current IP to body of email echo 'Your IP address is:' $newIP >> /tmp/ipcheck.mail echo '' >> /tmp/ipcheck.mail echo '' >> /tmp/ipcheck.mail #Send email cat /tmp/ipcheck.mail | msmtp -a default firstname.lastname@example.org fi