14 January 2020

Mikrotik no-ip update

tested 6.45.x

# No-IP account credentials.
:local noipUsername "username"
:local noipPassword "password"

# Set the hostname or label of network to be updated.
# Hostnames with spaces are unsupported. Replace the value in the quotations below with your host names.
# To specify multiple hosts, separate them with commas.
:local noipHostname "host.ddns.net"

# The interface name with the assigned dynamic IP address (usually the WAN interface).
:local wanInterface "eth-wan"

# Log destination
:local logDestination "/disk1/logs/"

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

:log warning message="START: No-IP DDNS Update"

:if ([/interface get $wanInterface value-name=running] = true) do={

#   Get the previous IP via DNS resolution.
    :local previousIP [:resolve "$noipHostname"]

#   Get the current IP on the WAN interface.
    :local currentIP [/ip address get [find interface="$wanInterface" disabled=no] address]

#   Strip net mask from IP address.
    :for i from=([:len $currentIP] - 1) to=0 do={
        :if ([:pick $currentIP $i] = "/") do={
            :set currentIP [:pick $currentIP 0 $i]
        }
    }

    :log info "No-IP: DNS IP ($previousIP), interface IP ($currentIP)"
   
    :if ($currentIP != $previousIP) do={
        :log info "No-IP: Current IP $currentIP is not equal to previous IP, update needed"

#       The update URL. The "\3F" is hex for question mark (?). This is required since ? is a special character in the command.
        :local url "http://dynupdate.no-ip.com/nic/update\3Fmyip=$currentIP"
        :local noipHostnames
        :set noipHostnames [:toarray $noipHostname]
        :foreach hostname in=$noipHostnames do={
            :log info "No-IP: Sending update for $hostname"
            /tool fetch url=($url . "&hostname=$hostname") user=$noipUsername password=$noipPassword mode=http dst-path=($logDestination . "no-ip_ddns_update-" . $hostname . ".txt")
            :log info "No-IP: Host $hostname updated on No-IP with IP $currentIP"
        }
    }   else={
        :log info "No-IP: Previous IP $previousIP is equal to current IP, no update needed"
        }

}   else={
    :log info "No-IP: $wanInterface is not currently running, unable to verify and/or update IP."
    }
   
:log warning message="END: No-IP DDNS Update"