Dynamic DNS Update Script for ChangeIP.com

From MikroTik Wiki
Revision as of 20:33, 6 June 2008 by Changeip (talk | contribs)
Jump to: navigation, search

The following script should be created when you wish to update your ChangeIP.com Dynamic DNS account. Once created you should schedule this to run once in a while. The :global variables should be edited to include your unique username and password, interface name, etc.

The script below is RouterOS 3.0 Compatible!

An updated script here (01/20/08) should allow auto-detection of the default gateways interface name. This script below can be used if you have more than 1 WAN connection, but only 1 is active at a time.

# Define User Variables
:global ddnsuser "CHANGEIPUSERID"
:global ddnspass "CHANGEIPPASSWORD"
:global ddnshost "FREEHOSTNAME.TOUPDATE.TLD"

# 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 ("DDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={

  :if ($ddnsip != $ddnslastip) do={

    :log info "DDNS: Sending UPDATE!"
    :log info [ :put [/tool dns-update name=$ddnshost address=[:pick $ddnsip 0 [:find $ddnsip "/"] ] key-name=$ddnsuser key=$ddnspass ] ]
    :global ddnslastip $ddnsip

  } else={ 
    :log info "DDNS: No update required."
  }

}

# End of script

If errors or problems occur with the above scripts please check to see if we are even receiving any updates. https://www.changeip.com/Reports/DDNSUpdates.asp will show you current updates on your account. Feel free to contact Support at ChangeIP.com if you are having problems.

2.9 Series: (Please use the above for the newer 3.0 version - this version is left here for archival reasons.)

:log info "DDNS: Begin"

:global ddns-user "YOURUSERID"
:global ddns-pass "YOURPASSWORD"
:global ddns-host "*1"
:global ddns-interface "EXACTINTERFACENAME"

:global ddns-ip [ /ip address get [/ip address find interface=$ddns-interface] address ]

:if ([ :typeof $ddns-lastip ] = nil ) do={ :global ddns-lastip 0.0.0.0/0 }

:if ([ :typeof $ddns-ip ] = nil ) do={

  :log info ("DDNS: No ip address present on " . $ddns-interface . ", please check.")

} else={

  :if ($ddns-ip != $ddns-lastip) do={

    :log info "DDNS: Sending UPDATE!"
    :log info [ /tool dns-update name=$ddns-host address=[:pick $ddns-ip 0 [:find $ddns-ip "/"] ] key-name=$ddns-user key=$ddns-pass ]
    :global ddns-lastip $ddns-ip

  } else={ 

    :log info "DDNS: No change" 

  }

}

:log info "DDNS: End"

For those of you that like to use the CLI, and want to make sure you get a very clean import with no line breaks, etc, you can run this script to create it for you:

/system script
add name=HomingBeaconDynamicDNSUpdater policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff \
    source="# Define User Variables\r\
    \n:global ddnsuser \"CHANGEIPUSERID\"\r\
    \n:global ddnspass \"CHANGEIPPASSWORD\"\r\
    \n:global ddnshost \"FREEHOSTNAME.TOUPDATE.TLD\"\r\
    \n\r\
    \n# Define Global Variables\r\
    \n:global ddnsip\r\
    \n:global ddnslastip\r\
    \n:if ([ :typeof \$ddnslastip ] = nil ) do={ :global ddnslas\
    tip \"0\" }\r\
    \n\r\
    \n:global ddnsinterface\r\
    \n:global ddnssystem (\"mt-\" . [/system package get system \
    version] )\r\
    \n\r\
    \n# Define Local Variables\r\
    \n:local int\r\
    \n\r\
    \n# Loop thru interfaces and look for ones containing\r\
    \n# default gateways without routing-marks\r\
    \n:foreach int in=[/ip route find dst-address=0.0.0.0/0 acti\
    ve=yes ] do={ \r\
    \n  :if ([:typeof [/ip route get \$int routing-mark ]] != st\
    r ) do={\r\
    \n     :global ddnsinterface [/ip route get \$int interface]\
    \r\
    \n  } \r\
    \n}\r\
    \n\r\
    \n# Grab the current IP address on that interface.\r\
    \n:global ddnsip [ /ip address get [/ip address find interfa\
    ce=\$ddnsinterface ] address ]\r\
    \n\r\
    \n# Did we get an IP address to compare\?\r\
    \n:if ([ :typeof \$ddnsip ] = nil ) do={\r\
    \n   :log info (\"DDNS: No ip address present on \" . \$ddns\
    interface . \", please check.\")\r\
    \n} else={\r\
    \n\r\
    \n  :if (\$ddnsip != \$ddnslastip) do={\r\
    \n\r\
    \n    :log info \"DDNS: Sending UPDATE!\"\r\
    \n    :log info [ :put [/tool dns-update name=\$ddnshost add\
    ress=[:pick \$ddnsip 0 [:find \$ddnsip \"/\"] ] key-name=\$d\
    dnsuser key=\$ddnspass ] ]\r\
    \n    :global ddnslastip \$ddnsip\r\
    \n\r\
    \n  } else={ \r\
    \n    :log info \"DDNS: No update required.\"\r\
    \n  }\r\
    \n\r\
    \n}\r\
    \n\r\
    \n# End of script"