RouterOS Italia

Versione completa: [Mikrotik] Dynamic DNS Update Script for dynDNS
Al momento stai visualizzando i contenuti in una versione ridotta. Visualizza la versione completa e formattata.
In order to support dyndns i took some parts from the changeip script and made it work for.

1) add a script called dynDNS:

Codice:
# Define User Variables
:global ddnsuser "DYNDNSUSER"
:global ddnspass "DYNDNSPASS"
:global ddnshost "DYNDNSHOST"

# Define Global Variables
:global ddnsip
:global ddnslastip
:if ([ :typeof $ddnslastip ] = nil ) do={ :global ddnslastip "0" }

:global ddnsinterface
:global ddnssystem ("mt-" . [/system package get system version] )

# Define Local Variables
:local int

# Loop thru interfaces and look for ones containing
# default gateways without routing-marks
:foreach int in=[/ip route find dst-address=0.0.0.0/0 active=yes ] do={
 :if ([:typeof [/ip route get $int routing-mark ]] != str ) do={
    :global ddnsinterface [/ip route get $int interface]
 }
}

# Grab the current IP address on that interface.
:global ddnsip [ /ip address get [/ip address find interface=$ddnsinterface ] address ]

# Did we get an IP address to compare?
:if ([ :typeof $ddnsip ] = nil ) do={
  :log info ("DynDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={
 :if ($ddnsip != $ddnslastip) do={
   :log info "DynDNS: Sending UPDATE!"
   :local str "/nic/update?hostname=$ddnshost&myip=$ddnsip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
   /tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \
       password=$ddnspass dst-path=("/DynDNS.".$ddnshost)
   :delay 1
   :local str [/file find name="DynDNS.$ddnshost"];
   /file remove $str
   :global ddnslastip $ddnsip
 }
}
2) Add a scheduler entry to run every minute

Codice:
/system scheduler add name=dynDNS interval=00:01 on-event="/system script run dynDns\r\n"


There is a threadfor further questions.

Getting it to work with 4.XX
Codice:
# Set needed variables

Codice:
:local username "YourUsername"

Codice:
:local password "YourPassword"

Codice:
:local hostname "nohostset"

Codice:
:global systemname [/system identity get name]


Codice:
:if ($systemname  = "Site1" ) do= {

Codice:
:set hostname "yourdomain1.dyndns.org"

Codice:
}

Codice:
:if ($systemname  = "Site2" ) do= {

Codice:
:set hostname "yourdomain2.dyndns.org"

Codice:
}

Codice:
:if ($systemname  = "Site3" ) do= {

Codice:
:set hostname "yourdomain3.dyndns.org"

Codice:
}


Codice:
:global dyndnsForce

Codice:
:global previousIP


Codice:
# print some debug info

Codice:
:log info ("UpdateDynDNS: username = $username")

Codice:
:log info ("UpdateDynDNS: password = $password")

Codice:
:log info ("UpdateDynDNS: hostname = $hostname")

Codice:
:log info ("UpdateDynDNS: previousIP = $previousIP")


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

Codice:
/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"

Codice:
:local result [/file get dyndns.checkip.html contents]


Codice:
# parse the current IP result

Codice:
:local resultLen [:len $result]

Codice:
:local startLoc [:find $result ": " -1]

Codice:
:set startLoc ($startLoc + 2)

Codice:
:local endLoc [:find $result "</body>" -1]

Codice:
:local currentIP [:pick $result $startLoc $endLoc]

Codice:
:log info "UpdateDynDNS: currentIP = $currentIP"


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

Codice:
#  but you could end up getting blacklisted by DynDNS!


Codice:
#:set dyndnsForce true


Codice:
# Determine if dyndns update is needed

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

Codice:
:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={

Codice:
   :set dyndnsForce false

Codice:
   :set previousIP $currentIP

Codice:
   /tool fetch user=$username password=$password mode=http address="members.dyndns.org" \

Codice:
      src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/dyndns.txt"

Codice:
   :local result [/file get dyndns.txt contents]

Codice:
   :log info ("UpdateDynDNS: Dyndns update needed")

Codice:
   :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)

Codice:
   :put ("Dyndns Update Result: ".$result)

Codice:
} else={

Codice:
   :log info ("UpdateDynDNS: No dyndns update needed")

Codice:
}



If have problems to run these scripts, try it:

Codice:
:local ddnsuser "your user name"
another version of this script working in 5.x Debugged version for RoS 6.x:Scheduler:

Codice:
:local ddnspass "your password"

Codice:
:local theinterface "device to renove IP"

Codice:
:local ddnshost "yourhost.dyndns.org"

Codice:
:local ipddns [:resolve $ddnshost];

Codice:
:local ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]

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

Codice:
  :log info ("DynDNS: No ip address on $theinterface .")

Codice:
} else={

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

Codice:
     :if ( [:pick $ipfresh $i] = "/") do={

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

Codice:
     }

Codice:
}


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

Codice:
   :log info ("DynDNS: IP-DynDNS = $ipddns")

Codice:
   :log info ("DynDNS: IP-Fresh = $ipfresh")

Codice:
  :log info "DynDNS: Update IP needed, Sending UPDATE...!"

Codice:
  :local str "/nic/update?hostname=$ddnshost&myip=$ipfresh&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"

Codice:
  /tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \

Codice:
        password=$ddnspass dst-path=("/DynDNS.".$ddnshost)

Codice:
   :delay 1

Codice:
   :local str [/file find name="DynDNS.$ddnshost"];

Codice:
   /file remove $str

Codice:
   :global ipddns $ipfresh

Codice:
 :log info "DynDNS: IP updated to $ipfresh!"

Codice:
   } else={

Codice:
    :log info "DynDNS: dont need changes";

Codice:
   }

Codice:
}




Codice:
# Set needed variables

Codice:
:local username "YOURUSER"

Codice:
:local password "YOURPASWORD"

Codice:
:local hostname "YOURHOSTNAME.dyndns.org"


Codice:
:global dyndnsForce

Codice:
:global previousIP


Codice:
# print some debug info

Codice:
:log info ("UpdateDynDNS: username = $username")

Codice:
:log info ("UpdateDynDNS: password = $password")

Codice:
:log info ("UpdateDynDNS: hostname = $hostname")

Codice:
:log info ("UpdateDynDNS: previousIP = $previousIP")


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

Codice:
/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"

Codice:
:delay 1

Codice:
:local result [/file get dyndns.checkip.html contents]


Codice:
# parse the current IP result

Codice:
:local resultLen [:len $result]

Codice:
:local startLoc [:find $result ": " -1]

Codice:
:set startLoc ($startLoc + 2)

Codice:
:local endLoc [:find $result "</body>" -1]

Codice:
:local currentIP [:pick $result $startLoc $endLoc]

Codice:
:log info "UpdateDynDNS: currentIP = $currentIP"


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

Codice:
# but you could end up getting blacklisted by DynDNS!


Codice:
#:set dyndnsForce true


Codice:
# Determine if dyndns update is needed

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


Codice:
:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={

Codice:
  :set dyndnsForce false

Codice:
  :set previousIP $currentIP

Codice:
  :log info "$currentIP or $previousIP"

Codice:
  /tool fetch user=$username password=$password mode=http address="members.dyndns.org" \

Codice:
     src-path="nic/update?system=dyndns&hostname=$hostname&myip=$currentIP&wildcard=no" \

Codice:
     dst-path="/dyndns.txt"

Codice:
  :delay 1

Codice:
  :local result [/file get dyndns.txt contents]

Codice:
  :log info ("UpdateDynDNS: Dyndns update needed")

Codice:
  :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)

Codice:
  :put ("Dyndns Update Result: ".$result)

Codice:
} else={

Codice:
  :log info ("UpdateDynDNS: No dyndns update needed")

Codice:
}




Codice:
:global ddnsuser "theddnsusername"

Codice:
:global ddnspass "theddnspassword"

Codice:
:global theinterface "interfacename"

Codice:
:global ddnshost blabla.dyndns.org

Codice:
:global ipddns [:resolve $ddnshost];

Codice:
:global ipfresh [ /ip address get [/ip address find interface=$theinterface ] address ]

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

Codice:
  :log info ("DynDNS: No ip address on $theinterface .")

Codice:
} else={

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

Codice:
     :if ( [:pick $ipfresh $i] = "/") do={

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

Codice:
     }

Codice:
}


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

Codice:
   :log info ("DynDNS: IP-DynDNS = $ipddns")

Codice:
   :log info ("DynDNS: IP-Fresh = $ipfresh")

Codice:
  :log info "DynDNS: Update IP needed, Sending UPDATE...!"

Codice:
  :global str "/nic/update\?hostname=$ddnshost&myip=$ipfresh&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"

Codice:
  /tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \

Codice:
        password=$ddnspass dst-path=("/DynDNS.".$ddnshost)

Codice:
   :delay 1

Codice:
   :global str [/file find name="DynDNS.$ddnshost"];

Codice:
   /file remove $str

Codice:
   :global ipddns $ipfresh

Codice:
 :log info "DynDNS: IP updated to $ipfresh!"

Codice:
   } else={

Codice:
    :log info "DynDNS: dont need changes";

Codice:
   }

Codice:
}



Codice:
/system scheduler

Codice:
add interval=1m name=DynDns on-event=DynDns policy=ftp,reboot,read,write,policy,test,winbox