Dynu.com script for dynamic DNS

Hi,

My first post here and I’m also new to Router OS. I’m using Dynu for dynamic DNS so that I can remote to devices on my network which uses a dynamic IP for the WAN. Dynu provides this script but when I load it there are errors. Maybe someone here can see obvious problems with the syntax? Thanks in advance for your help.

/system script
add name=Dynu
policy=read,write,test
source=“:global ddnsuser your_Dynu_username
:global ddnspass “your_Dynu_password”
:global theinterface “WAN_Interface_Name”
:global ddnshost “your_Dynu_hostname”
:global ipddns [:resolve $ddnshost];
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
:log info (“DynuDDNS: 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];
}
}
:if ($ipddns != $ipfresh) do={
:log info (“DynuDDNS: IP-Dynu = $ipddns”)
:log info (“DynuDDNS: IP-Fresh = $ipfresh”)
:log info “DynuDDNS: Update IP needed, Sending UPDATE…!”
:global str “/nic/update?hostname=$ddnshost&myip=$ipfresh”
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=(”/Dynu.".$ddnshost)
:delay 1
:global str [/file find name=“Dynu.$ddnshost”];
/file remove $str
:global ipddns $ipfresh
:log info “DynuDDNS: IP updated to $ipfresh!”
} else={
:log info “DynuDDNS: dont need changes”;
} }

Change your data
Configure in scheduler every 30 minutes

:global ddnsuser "Usernam Dynu.com"
:global ddnspass "Password Dynu.com"
:global theinterface "Interface"
:global ddnshost "domain.dynu.com"
:global ipddns [:resolve $ddnshost];
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:if ([ :typeof $ipfresh ] = nil ) do={
:log info ("dynu: 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];
}
}
:if ($ipddns != $ipfresh) do={
:log info ("dynu: IP-dynu = $ipddns")
:log info ("dynu: IP-Fresh = $ipfresh")
:log info "dynu: Update IP needed, Sending UPDATE...!"
:global str "/nic/update?hostname=$ddnshost&myip=$ipfresh"
/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $ipfresh
:log info "dynu: IP updated to $ipfresh!"
} else={
:log info "dynu: dont need changes";
}
}

I know this is a two year old thread, but I’m in the same boat. I added info log entries all over the place and remarked every line and ran the script. Obviously it ran fine with just the log full of the log entries. I then started at the top and removed the remarks one line at a time until it bombed. In this configuration, I get the “Starting Dynu script”, “Globals set”, and “line 1” log entries and then nothing. Yes, the interface name is correct (in fact, I copied it directly from last night’s .rsc file). Doing this on a model = RouterBOARD 750 r2 running RouterOS 6.43.2

:log info “Starting Dynu script”
:global ddnsuser “user name redacted”
:global ddnspass “password redacted”
:global theinterface “E1-p10_DSL_Internet”
:global ddnshost “The domain I’m trying to apply this to”
:log info “Globals set”
:global ipddns [:resolve $ddnshost];
:log info “line 1”
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]
:log info “line 2”

:if ([ :typeof $ipfresh ] = nil ) do={

:log info “line 3”

:log info (“DynuDDNS: No IP address on $theinterface .”)

:log info “line 4”

} else={

:log info “line 5”

:for i from=( [:len $ipfresh] - 1) to=0 do={

:log info “line 6”

:if ( [:pick $ipfresh $i] = “/”) do={

:log info “line 7”

:set ipfresh [:pick $ipfresh 0 $i];

:log info “line 8”

}

:log info “line 9”

}

:log info “line 10”

:if ($ipddns != $ipfresh) do={

:log info “line 11”

:log info (“DynuDDNS: IP-Dynu = $ipddns”)

:log info “line 12”

:log info (“DynuDDNS: IP-Fresh = $ipfresh”)

:log info “line 13”

:log info “DynuDDNS: Update IP needed, Sending UPDATE…!”

:log info “line 14”

:global str “/nic/update?hostname=$ddnshost&myip=$ipfresh”

:log info “line 15”

/tool fetch address=api.dynu.com src-path=$str mode=http user=$ddnsuser password=$ddnspass dst-path=# (“/Dynu.”.$ddnshost)

:log info “line 16”

:delay 1

:log info “line 17”

:global str [/file find name=“Dynu.$ddnshost”];

:log info “line 18”

/file remove $str

:log info “line 19”

:global ipddns $ipfresh

:log info “line 20”

:log info “DynuDDNS: IP updated to $ipfresh!”

:log info “line 21”

} else={

:log info “line 22”

:log info “DynuDDNS: dont need changes”;

:log info “line 23”

} }"

Any ideas? I fully admit that I know VERY little about scripting…

I just add the mikrotik ddns to “dns records” in dynu. No scripting necessary.

I just add the mikrotik ddns to “dns records” in dynu. No scripting necessary.

can you explain it?

Sorry for necroposting, need in Record DNS add address name NULL(not enter), Type CNAME ,Hostname: DDNS Name for Cloud, and disable name type A.

https://yadi.sk/i/ezOMuefy799YlQ

Hello,
This works when the cloud is behind nat. It does not work for me

I found this thread when googling how to get my system to work with dynu. It helped me to understand what to do.
However, I don’t think it’s necessary for the script to keep track of IP address.

This script works well for me and seems much simpler:
/tool fetch url=“https://api.dynu.com/nic/update?username=myusername&password=mypassword

I just schedule this to run every hour. Seems to work perfectly.

Sure it works, but your DDNS provider may be less excited, because you’re doing useless updates that don’t really change anything, and putting extra load on their server. And obviously, you’re not going to overload it yourself, but if everyone did it this way, and not everyone would see once an hour as enough, … you get the idea.

Latest Code Working

 /tool fetch mode=http url="http://api.ipify.org" src-path="" dst-path=/dyn.html
:local currentIP [/file get dyn.html contents]
:log warning "Public IP Detected $currentIP"
######################################################
 
:global ddnsuser "username"
:global ddnspass "password"
:global ddnshost "host.mywire.org"
:global ipddns [:resolve $ddnshost];
:log warning "Current DNS $ipddns"
/file/remove dyn.html

:if ($ipddns != $currentIP) do={
:log warning ("Updating IP = $currentIP")
:global str "/nic/update?username=$ddnsuser&password=$ddnspass&hostname=$ddnshost&myip=$currentIP"
/tool fetch address=api.dynu.com src-path=$str mode=https dst-path=("/Dynu.".$ddnshost)
:delay 1
:global str [/file find name="Dynu.$ddnshost"];
/file remove $str
:global ipddns $currentIP
:log info "DynuDDNS: IP updated to $currentIP!"
} else={
:log info "No Need to change, IP Already Updated";
} }

Better do some cleaning up on that mess, including removing the useless logging…

:local ddnsuser  "username"
:local ddnspass  "password"
:local ddnshost  "host.mywire.org"

:local currentIP ([/tool fetch url="http://api.ipify.org" as-value output=user]->"data")
:local dnsIP     [:resolve $ddnshost]
:if ($currentIP != $dnsIP) do={
    :local update "https://api.dynu.com/nic/update?username=$ddnsuser&password=$ddnspass&hostname=$ddnshost&myip=$currentIP"
    /tool fetch url=$update keep-result=no
    :log info "DYNU $ddnshost updated: old IP was $dnsIP and new IP is $currentIP"
}

@Shahid

Do use global variable in the script only if you intend to use the variable in other script or for use at a later time. Use local variable instead.

Sure. Thanks for suggestions.

Good
I don’t know much about scripting. I just got that working so i posted that here. Thanks for the improvement.

Why is that no one keeps track of the current dynamic IPv6 from the router? Is that because the obtained IPv6 is a suffixed address and not a complete one as you can get from a peripheral?

Very simple answer. Not using IPv6…

Thank you for this.