Wednesday, 28 August 2013

unblock-us and DynDNS

My new ISP (Vodafone) provided me a new router (a Huawei HG658c) with their own firmware on it.  They also don't guarantee me a static IP address.  Unblockus says you can handle this with a DynDNS account.

 However, my router does not seem to be able to correctly update DynDNS, and even if it did I'm not entirely convinced that these magical "IP update" packets would get picked up by the good folks at Unblockus.  I've solved the DynDNS issue by installing and configuring ddclient, but Unblockus explicitly don't update your IP address when done by software DynDNS clients.

With a little bit of googling around and finding some decent partial solutions, I've devised what seems to be a pretty straight-forward shell-scripting mechanism that will keep track of your public IP address (it does assume you have only one public IP address) and if it changes will log you back in at unblock-us.com.  This script is also cron-able, should you so desire.  It should work on any Unix (Linux, OSX) with wget installed.

Also, this can entirely avoid a DynDNS account if all the more you want it for is to update Unblockus.  If you don't have a Linux machine, let me suggest getting a RaspberryPi and sticking that onto your network!

#!/bin/bash

usage() {
  echo "usage: $(basename $0) -u unblock-us_userid -p unblock-us_password"
  exit 1
}

unblock_user=""
unblock_pass=""
while getopts "u:p:" name; do
 case $name in
      u) unblock_user="$OPTARG" ;;
      p) unblock_pass="$OPTARG" ;;
 esac
done

if [ -z "$unblock_user" -o -z "$unblock_pass" ]; then
  usage
fi

lastIP_file=lastip.public
log_file=updater.log
ipechoURL="http://ipecho.net/plain"
unblockURL="https://api.unblock-us.com/login?${unblock_user}:${unblock_pass}"

cd $(dirname $0)

echo "Updating at $(date)" >> $log_file

if [ ! -f $lastIP_file ]; then
  touch $lastIP_file
fi
lastIP=$(cat $lastIP_file)
if [ -z "$lastIP" ]; then
  lastIP=0.0.0.0
fi

currentIP=$(wget -qO- $ipechoURL)
if [ ! -z "$currentIP" ]; then
  ping -q -c 1 $currentIP >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "ping of currentIP=[${currentIP}] obtained from $ipechoURL was unsuccessful" >> $log_file
    exit 1
  fi
else
  echo "Empty IP address from $ipechoURL" >> $log_file
  exit 1
fi

if [ "$lastIP" != "$currentIP" ]; then
  echo "IP has changed from $lastIP to $currentIP" >> $log_file
  echo $currentIP > $lastIP_file
  rm -f unblock.out
  wget -qO- $unblockURL > unblock.out
  if [ ! -f unblock.out -o "$(cat unblock.out)" != "active" ]; then
    echo "Output from $unblockURL is not as expected, may not be logged in. Content follows:" >> $log_file
    cat unblock.out >> $log_file
    exit 1
  fi 
else
  echo "IP unchanged" >> $log_file
fi

2 comments:

  1. Complete newbie question probably but I've copied and pasted your script into the OS X (10.10.3) Script Editor but when I hit play it reports a syntax error at the first }

    What am I doing wrong?

    ReplyDelete
  2. You can't use Bash scripts in OSX Kieran Hogan.

    ReplyDelete