Dynamic DNS Update Script for DNSoMatic.com

From MikroTik Wiki
Revision as of 21:00, 4 January 2012 by Hjoelr (talk | contribs) (Added multiple host version.)
Jump to: navigation, search

This script is a solution made of others solutions (nothing new). Much of this was adapted from the deprecated version of Dynamic DNS Update Script for DNSoMatic.com behind NAT.

The goal is to update your account on DNSoMatic.com. The main advantage of this solution is that DNSoMatic offers the possibility of propagating DNS updates to thirth party DNSlike systems like OpenDNS, DynDNS, Change IP and other 27 more.

Note: The script below is RouterOS 5.11 Tested!

# DNSoMatic automatic DNS updates

#--------------- Change Values in this section to match your setup ------------------

# User account info of DNSoMatic

:local maticuser "username"
:local maticpass "password"

# Set the hostname or label of network to be updated. This is the part after the double colon (::) on the DNSoMatic services page.
# Hostnames with spaces are unsupported. Replace the value in the quotations below with your host names.
# To specify multiple hosts, separate them with commas. 
# Use "all.dnsomatic.com" for the matichost to update all items in dnsomatic with this IP.

:local matichost "hostname1,hostname2"

# Change to the name of interface that gets the changing IP address

:local inetinterface "ether1-gateway"

#------------------------------------------------------------------------------------

# No more changes need

:global previousIP;

:if ([/interface get $inetinterface value-name=running]) do={
# Get the current IP on the interface
    :local currentIP [/ip address get [find interface="$inetinterface" disabled=no] address];
    
# Strip the net mask off the IP address
    :for i from=( [:len $currentIP] - 1) to=0 do={
        :if ( [:pick $currentIP $i] = "/") do={ 
            :set currentIP [:pick $currentIP 0 $i]
        } 
    }

# The update URL. Note the "\3F" is hex for question mark (?). Required since ? is a special character in commands.
    :local url "http://updates.dnsomatic.com/nic/update\3Fmyip=$currentIP&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
    :local matichostarray;
    :set matichostarray [:toarray $matichost];
    :foreach host in=$matichostarray do={   
        :set url ($url . "&hostname=$host");
    }
    
    :if ([:len $matichostarray] >= 1) do={

        :if ($currentIP != $previousIP) do={
            :log info "DNSoMatic: Update needed"
            :set previousIP $currentIP
            :log info "DNSoMatic: Sending update $currentIP"
            /tool fetch url=$url user=$maticuser password=$maticpass mode=http dst-path=dnsomaticupdate.txt
            :log info "DNSoMatic: Host $matichost updated on DNSoMatic with IP $currentIP"
        }  else={
            :log info "DNSoMatic: Previous IP $previousIP and current IP equal, no update need"
        }
        
    } else={
        :log info "No hosts specified to be updated. Skipping update."
    }
} else={
    :log info "DNSoMatic: $inetinterface is not currently running, so therefore will not update."
}
This will also need you to configure scheduler entry for periodical runs.

hjoelr 04/jan/2011