# DDNS update script for WAN with DHCP (e.g. dnsomatic.com)



## arabesc (Oct 2, 2014)

I would like to share a script which I wrote to update DDNS at the DNS-O-Matic service.
You can easily modify following script for your needs.
First of all, I'm getting wan-ip from the provider's DHCP server, so I will use _/etc/dhclient-exit-hooks_ script as a handler for ip-address changes.
Here it is:

```
#!/bin/sh

if [ -x /usr/bin/logger ]; then
    LOGGER="/usr/bin/logger -s -p user.notice -t dhclient"
else
    LOGGER=echo
fi

# you can use any DNS-server here
OPENDNS_ADDR="208.67.220.220:53"
DNSOMATIC_UPDATE_URL="updates.dnsomatic.com"
DNSOMATIC_CREDITIONALS="<your_login>:<your_password>"

update_ddns()
{
    $LOGGER "Fetch ip[$DNSOMATIC_UPDATE_URL] from DNS[$OPENDNS_ADDR]"

    # hostip is a tool from the dns/dnscrypt-proxy package
    # you don't need such difficulties if you have access to DNS, just use conventional domain name
    dnsomatic_ip="`/usr/local/bin/hostip -r $OPENDNS_ADDR $DNSOMATIC_UPDATE_URL`"

    $LOGGER "$DNSOMATIC_UPDATE_URL[$dnsomatic_ip]"

    user_agent=`uname -i -p -r -s -K`
    update_params="myip=$new_ip_address&mx=NOCHG&backmx=NOCHG&wildcard=NOCHG"

    dns_update_result=`/usr/local/bin/curl --silent --show-error -4 \
                                           --resolve $DNSOMATIC_UPDATE_URL:443:$dnsomatic_ip \
                                           --user-agent "$user_agent" \
                                           --user "$DNSOMATIC_CREDITIONALS" \
                                           "https://@$DNSOMATIC_UPDATE_URL/nic/update?$update_params"`

    $LOGGER "DNS update results:"
    $LOGGER "$dns_update_result"
}

if [ -z "${reason:-$1}" ]; then
    $LOGGER "No DHCP reason"
    exit 0
fi

$LOGGER "DHCP reason: ${reason:-$1}"

case ${reason:-$1} in

BOUND|RENEW|REBIND|REBOOT)
    update_ddns
    ;;

EXPIRE|FAIL)
    ;;

TIMEOUT)
    ;;
esac

exit 0
```


----------

