Friday, March 29, 2013

No-IP Automatic Update Bash Script for Mac OS X

I'll finish this article with background info, but I had a problem with No-IP because AT&T never updated my public IP address through U-Verse.  So the No-IP DNS Update Client (DUC) never submitted an update record, and thus my No-IP host expired.

Enter a quick solution using Unix shell script and cron to schedule.  Thanks to AntonioCS's GitHub post, he provided a good bash script, but did not work on the Mac.  So, with a small modification, we are golden!

First problem, is the script doesn't do an update if the IP address doesn't change.  So, I force a change, like this, to a dummy IP address:


#!/bin/bash

# No-IP uses emails as passwords, so make sure that you encode the @ as %40
USERNAME=--account name--
PASSWORD=--my password--
HOST=--my host name--
LOGFILE=logdir/noip.log
STOREDIPFILE=configdir/current_ip
USERAGENT="Simple Bash No-IP Updater/0.4 antoniocs@gmail.com"

if [ ! -e $STOREDIPFILE ]; then 
touch $STOREDIPFILE
fi

NEWIP=1.1.1.1
STOREDIP=$(cat $STOREDIPFILE)
RESULT=$(curl -o "$LOGFILE" -s --user-agent "$USERAGENT" "https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOST&myip=$NEWIP")

LOGLINE="[$(date +"%Y-%m-%d %H:%M:%S")] $RESULT"
echo $NEWIP > $STOREDIPFILE

echo $LOGLINE >> $LOGFILE

exit 0


Then, I use the No-IP DUC for Mac.  I suppose I could get rid of the GUI, and use the modified AntonioCS script that gathers the current IP address instead, but this suffices nicely, and keeps a GUI up so I can always easily monitor the No-IP status.  If we want, we can do something similar to get the real IP:


#!/bin/bash

# No-IP uses emails as passwords, so make sure that you encode the @ as %40
USERNAME=--account name--
PASSWORD=--my password--
HOST=--my host name--
LOGFILE=logdir/noip.log
STOREDIPFILE=configdir/current_ip
USERAGENT="Simple Bash No-IP Updater/0.4 antoniocs@gmail.com"

if [ ! -e $STOREDIPFILE ]; then 
touch $STOREDIPFILE
fi

NEWIP=$(curl http://icanhazip.com/)
STOREDIP=$(cat $STOREDIPFILE)

if [ "$NEWIP" != "$STOREDIP" ]; then
RESULT=$(curl -o "$LOGFILE" -s --user-agent "$USERAGENT" "https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOST&myip=$NEWIP")

LOGLINE="[$(date +"%Y-%m-%d %H:%M:%S")] $RESULT"
echo $NEWIP > $STOREDIPFILE
else
LOGLINE="[$(date +"%Y-%m-%d %H:%M:%S")] No IP change"
fi

echo $LOGLINE >> $LOGFILE

exit 0


Background

No-IP is a Dynamic DNS updater (or DDNS).  The reason I need such a thing is to access my home network from the Internet.  Because my Internet Provider (AT&T or whoever) assigns me an address that may change, I can't simply go to that address when I am traveling.  DDNS gives me a host name where I can pick an Internet domain, and go to it just like any other web site.  If the IP address changes, it is detected and updated by the DUC.  Some routers support certain DDNS providers, but No-IP is both free and has no limits on usage, whereas others may limit you to an account that expires unless you "touch" it periodically, an annoyance.

No comments:

Post a Comment