Community discussions

MikroTik App
 
hobbes1069
newbie
Topic Author
Posts: 29
Joined: Sun Aug 16, 2015 3:43 pm

DynDNS script that works!!! (without using the legacy GET method)  [SOLVED]

Fri Feb 16, 2018 5:37 pm

I have pieced together a working script but I'm sure it could still use some improvement.

It uses the recommended https method[1] instead of the legacy http method so not only are you not transmitting your password in plain text, but it uses an API token which only allows update of the IP address. It also parses the actual response from members.dyndns.org.
### Settings ###
:global ddnsuser <username>
:global ddnstoken "<token>"
:global ddnshost <dyndnshost>
:global theinterface "<wan interface>"

### Script Begins ###
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:global ipddns [:resolve $ddnshost]

# All return codes from members.dyndns.org
:global resultcodes {good="Update successful";
    nochg="WARNING: Update not needed, repeated attempts may cause you to be blocked.";
    badauth="The username and password pair do not match a real user.";
    notfqdn="The hostname specified is not a fully-qualified domain name.";
    nohost="The hostname specified does not exist in this user account.";
    numhost="Too many hosts (more than 20) specified in an update.";
    abuse="The hostname specified is blocked for update abuse.";
    badagent="The user agent was not sent or HTTP method is not permitted.";
    dnserr="DNS error encountered.";
    911="There is a problem or scheduled maintenance on our side."
}

# Check ip address and remove subnet.
:if ([ :typeof $ipfresh ] = nil ) do={
    :log info ("DynDNS: No ip address on $theinterface .")
} else={
    :for i from=( [:len $ipfresh] - 1) to=0 do={ 
        :if ( [:pick $ipfresh $i] = "/") do={ 
            :set ipfresh [:pick $ipfresh 0 $i];
        }
    }
}

# Determine if an update is required.
:if ($ipddns != $ipfresh) do={
    :local logname "dyndns.$ddnshost"
    :log info ("DynDNS: IP-DynDNS = $ipddns")
    :log info ("DynDNS: IP-Fresh = $ipfresh")
    :log info "DynDNS: Update IP needed, saving UPDATE...!"
    /tool fetch url="https://$ddnsuser:$ddnstoken@members.dyndns.org/v3/update?hostname=$ddnshost&myip=$ipfresh" keep-result=yes dst-path=$logname;
    :local result [/file get [find name="$logname" ] contents ]
    /file remove [ find name="$logname" ]
    :put "Server returned: $result";
    # Separate return code from IP address
    :for i from=0 to=( [:len $result] - 1) do={ 
        :if ( [:pick $result $i] = " ") do={
        :set result [:pick $result 0 $i]
        }
    }
    :put "Return code is: $result";
    :foreach resultcode,desc in=$resultcodes do={
        :if ( $result = $resultcode ) do={
        :log info ("DynDNS: $desc")
        }
    }
    :log info ("DynDNS: IP updated to $ipfresh!")
} else={
    :log info ("DynDNS: IP-DynDNS = $ipddns")
    :log info ("DynDNS: IP-Fresh = $ipfresh")
    :log info "DynDNS: no update required"
}
Thanks,
Richard

[1] https://help.dyn.com/remote-access-api/perform-update/

Who is online

Users browsing this forum: No registered users and 30 guests