faux-ipupdate

A simple script to update your dynamic DNS settings if your router is NAT:ed and your gateway doesn't support your DNS service.

It might be a good idea to run this from cron.

The script

#!/bin/sh
#
# faux-ipupdate 1.0.0
#
# If your server is NAT:ed this script can be useful to update
# your Dynamic DNS settings. It is hard-coded to use with LoopiaDNS.
#
# Required programs:
# curl
# /sbin/ifconfig


# Set variables
BEHIND_NAT=1
NIC=eth0 # Only relevant if BEHIND_NAT=0
DYNDNS_HOST=foo.bar.baz
CURL_CONFIG=/etc/faux-ipupdate/curl.conf
CACHED_IP_DIR=/var/cache/faux-ipupdate
CACHED_IP_FILE=${CACHED_IP_DIR}/external_ip

# Create a IP cache if there are none
if [[ -e $CACHED_IP_FILE ]] ; then
  true
else
  mkdir -p $CACHED_IP_DIR
  touch $CACHED_IP_FILE
fi

# Set more variables
CACHED_IP="`cat $CACHED_IP_FILE`"
CURRENT_IP="`curl --silent dns.loopia.se/checkip/checkip.php | sed 's/^.*: \([^<]*\).*$/\1/'`"

# Get current IP address
if [[ $BEHIND_NAT = 0 ]] ; then
  CURRENT_IP="`/sbin/ifconfig $NIC |grep inet|head -1|cut -c 21-36|sed 's/[^0-9]*$//'`"
else
  CURRENT_IP="`curl --silent https://dns.loopia.se/checkip/checkip.php | sed 's/^.*: \([^<]*\).*$/\1/'`"
fi

# Update DynDNS host if necessary
if [[ $CURRENT_IP != $CACHED_IP ]] ; then
  curl --silent --config $CURL_CONFIG "https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname="$DYNDNS_HOST"&myip="$CURRENT_IP ; echo
  echo $CURRENT_IP > $CACHED_IP_FILE
  echo "Dynamic IP updated."
else
  echo "No need to update dynamic IP."
fi

/etc/faux-ipupdate/curl.conf

In this file you just write the user name and password for the DynDNS URL, like this:

user = yourdomain.com:password

Make sure that this file is not world-readable!

Se även datorer.