Skip to content

Instantly share code, notes, and snippets.

@wannadrunk
Last active November 1, 2016 08:51
Show Gist options
  • Save wannadrunk/53ba66df9cea2b9a9b9c27d4ce7d15fb to your computer and use it in GitHub Desktop.
Save wannadrunk/53ba66df9cea2b9a9b9c27d4ce7d15fb to your computer and use it in GitHub Desktop.
Mikrotik ทำ LB PCC แล้ว dyndns มีปัญหาเหมือนมันไม่รู้จะใช้ ip ของ wan ไหนเป็นหลัก ผมใช้สคริปตามด้านล่างนี้ไม่ทราบต้องแก้ไขตรงไหนครับ
#---- Change Values in this section to match your setup ----
:local username "username"
:local password "password"
:local hostname "domain.dyndns.org"
:local ddnsinterface "pppoe-out1"
#-----------------------------------------------------------
# get the current ip
:local currentip [ /ip address get [/ip address find interface=$ddnsinterface ] address ]
:local ddnsip [ /resolve domain-name=$hostname ]
#strip netmask/get ddns ip
:if ($currentip != "") do={
:set currentip [:pick $currentip 0 [:find $currentip "/"]]
}
# Determine if dyndns update is needed
:if ($currentip != $ddnsip) do={
/tool fetch user=$username password=$password mode=http host="members.dyndns.org" address="members.dyndns.org" \
src-path="/nic/update?hostname=$hostname&myip=$currentip" dst-path="/dyndns.txt"
:local result [/file get dyndns.txt contents]
:log info ("DYNDNS (".$hostname."): ".$result)
}
@wannadrunk
Copy link
Author

Set needed variables

:local username
:local password
:local hostname
:global dyndnsForce
:global previousIP

print some debug info

:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: previousIP = $previousIP")

get the current IP address from the internet (in case of double-nat)

/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"
:delay 1
:local result [/file get dyndns.checkip.html contents]

parse the current IP result

:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "" -1]
:local currentIP [:pick $result $startLoc $endLoc]
:log info "UpdateDynDNS: currentIP = $currentIP"

Remove the # on next line to force an update every single time - useful for debugging,

but you could end up getting blacklisted by DynDNS!

:set dyndnsForce true

Determine if dyndns update is needed

more dyndns updater request details http://www.dyndns.com/developers/specs/syntax.html

:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
:set dyndnsForce false
:set previousIP $currentIP
:log info "$currentIP or $previousIP"
/tool fetch user=$username password=$password mode=http address="members.dyndns.org"
src-path="nic/update?system=dyndns&hostname=$hostname&myip=$currentIP&wildcard=no"
dst-path="/dyndns.txt"
:delay 1
:local result [/file get dyndns.txt contents]
:log info ("UpdateDynDNS: Dyndns update needed")
:log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
:put ("Dyndns Update Result: ".$result)
} else={
:log info ("UpdateDynDNS: No dyndns update needed")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment