Skip to content

Instantly share code, notes, and snippets.

@jonathanio
Created June 8, 2015 18:59
Show Gist options
  • Save jonathanio/30ac396d8bdc8a1f3636 to your computer and use it in GitHub Desktop.
Save jonathanio/30ac396d8bdc8a1f3636 to your computer and use it in GitHub Desktop.
MikroTik DynDNS Update Script
# mt-dyndns-update.rsc
# ~ MikroTik script to update DynDSN hostname using IP from provided interface
# (c) 2014 onwards, Jonathan Wright <jon@than.io>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
# Set the username and password, and the hostname
# we're going to update within DynDNS.
:local username "{username}"
:local password "{password}"
:local dyndnsHost "{fqdn}"
# Which interface are we going to be checking against?
:local interfaceWAN {interface}
# Set runtime variables
:local addressDNS [:resolve $dyndnsHost];
:local addressWAN
# Startup...
:log debug ("mt-dyndns-update: Initiating update check for hostname $dyndnsHost")
# Extract the WAN address from the interface and strip it of everything from "/"
:set addressWAN [/ip address get [/ip address find interface=$interfaceWAN] address]
:set addressWAN [:pick [:tostr $addressWAN] 0 [:find [:tostr $addressWAN] "/"]]
:if ([:len $addressWAN] = 0) do={
:log critical ("mt-dyndns-update: No IP Address on $interfaceWANv4.")
:error ("mt-dyndns-update: No IP Address on $interfaceWANv4.")
}
#:for i from=( [:len $addressWAN] - 1) to=0 do={
# :if ( [:pick $addressWAN $i] = "/") do={
# :set address [:pick $addressWAN 0 $i];
# }
#}
:log debug ("mt-dyndns-update: Found address $addressWAN on ".$interfaceWAN)
:if ($addressDNS != $addressWAN) do={
:log info ("mt-dyndns-update: Setting $dyndnsHost A $addressWAN.")
/tool fetch \
mode=http \
user=$username \
password=$password \
address=members.dyndns.org \
src-path=("/nic/update". \
"?hostname=$dyndnsHost". \
"&myip=$addressWAN". \
"&wildcard=NOCHG". \
"&mx=NOCHG". \
"&backmx=NOCHG") \
dst-path=("/mt-dyndns-update-".$dyndnsHost)
:delay 1
:log debug ("mt-dyndns-update: ".[/file get ("mt-dyndns-update.".$dyndnsHost.".txt") contents])
/file remove ("mt-dyndns-update.".$dyndnsHost.".txt")
:global addressDNS $addressWAN
:log debug ("mt-dyndns-update: Saved new DNS address.")
} else={
:log debug "mt-dyndns-update: No update required.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment