http://wiki.mikrotik.com/wiki/Dynamic_DNS_Update_Script_for_Namecheap
This is a ROS 7 script (IIRC, the fetch command is bit different in ROS 6) which checks if the IP for an interface has changed, updates namecheap.com's dynamic DNS and also sends you an email about it.
As it turns out, it's also good for letting me know when electricity has been restored after a blackout or when the ISP comes back from an outage.
ROS now has /ip cloud with Mikrotik's own ddns. With this script and Namecheap, you can have a much cooler and shorter ddns domain. And also a backup in case /ip cloud is down.
My first script here so please let me know if things can be improved. I'm quite the newbie to Mikrotik. The script was originally taken from the Mikrotik wiki for another ddns provider and then modified for Namecheap.
Email tool needs to be setup beforehand. Just change the variables to yours. There's no error checking if the ddns update fails or sending email encounters an error.
Schedule this script to run every few minutes as you please. For pppoe, you can also run it from ppp profile up and down script.
Code: Select all
:local "NC_DDNS_SERVER" "dynamicdns.park-your-domain.com"
:local "NC_DDNS_HOSTNAMES_ARRAY" {"host1";"host2"}
:local "NC_DDNS_HOSTNAMES" [:tostr $"NC_DDNS_HOSTNAMES_ARRAY"]
:local "NC_DDNS_DOMAIN" "example.com"
:local "NC_TOKEN" "0123456789abcdef"
:local "TELEGRAM_SERVER" "api.telegram.org"
:local "TELEGRAM_KEY" "bot0123456789abcdef"
:local "TELEGRAM_CHAT_ID" "012345678"
:local "ISP_NAME" "My_ISP"
:local "WAN_INTERFACE" "pppoe-out1"
:local "LOG_FILE_PREFIX" "/disk1/logs/DDNS_NC."
:global "ddns_previous_ip"
:local "ddns_current_ip" [ /ip address get [/ip address find interface=$"WAN_INTERFACE" ] address ]
:local "current_date" [/system clock get date]
:local "current_time" [/system clock get time]
:local "system_name" [/system identity get name]
:local "system_uptime" [/system resource get uptime]
:local "system_free_memory" [/system resource get free-memory]
:local "system_cpu_load" [/system resource get cpu-load]
:local "system_version" ("ROS " . [/system/package/get [find name=routeros] version] )
# Strip the net mask off the IP address
:set "ddns_current_ip" [:pick $"ddns_current_ip" 0 [:find $"ddns_current_ip" "/"]]
:if ([ :typeof $"ddns_previous_ip" ] = nil ) do={ :global "ddns_previous_ip" "0" }
:if ([ :typeof $"ddns_current_ip" ] = nil ) do={
:log info ("DDNS: No ip address present on $"WAN_INTERFACE" interface, please check.")
} else={
:if ($"ddns_current_ip" != $"ddns_previous_ip") do={
:foreach hostname in=$"NC_DDNS_HOSTNAMES_ARRAY" do={
:log info ("DDNS: Updating $hostname.$"NC_DDNS_DOMAIN" $"ddns_previous_ip" -> $"ddns_current_ip"")
:local str "https://$"NC_DDNS_SERVER"/update?host=$"hostname"&domain=$"NC_DDNS_DOMAIN"&password=$"NC_TOKEN"&ip=$"ddns_current_ip""
#:log info $str
/tool fetch url=$str mode=https dst-path=($"LOG_FILE_PREFIX".$hostname)
}
#:log info $"NC_DDNS_HOSTNAMES"
:log info "DDNS: Sending Email"
/tool e-mail send to=myself@example.com subject="$"ISP_NAME" IP $"current_date" $"current_time" $"system_name"" body="$"system_name" $"current_date" $"current_time" \r$"system_version"\rNamecheap: $"NC_DDNS_HOSTNAMES" \rDomain: $"NC_DDNS_DOMAIN" \r$"ISP_NAME" IP: $"ddns_current_ip" \rPrevious IP: $"ddns_previous_ip" \rUptime: $"system_uptime" \rFree memory: $"system_free_memory" kb \rCPU Load: $"system_cpu_load" % "
:log info "DDNS: Sending Telegram"
:local str "https://$"TELEGRAM_SERVER"/$"TELEGRAM_KEY"/sendMessage?chat_id=$"TELEGRAM_CHAT_ID"&parse_mode=Markdown&text=$"ISP_NAME" $"system_name" $"system_version"%0ANamecheap: $"NC_DDNS_HOSTNAMES"%0ADomain: $"NC_DDNS_DOMAIN"%0A$"current_date" $"current_time"%0A$"ISP_NAME" IP: $"ddns_current_ip"%0APrevious IP: $"ddns_previous_ip"%0AUptime: $"system_uptime"%0AFree memory: $"system_free_memory" kb%0ACPU Load: $"system_cpu_load" %"
#:log info $str
/tool fetch url=$str mode=https keep-result=no
:global "ddns_previous_ip" $"ddns_current_ip"
} else={
:log info "DDNS: IP has not changed. DDNS will not be updated."
}
}
Below is a snippet of code for updating Digitalocean's DNS using its API. You can use this instead of Namecheap in the script above or both simultaneously. Fingers crossed DO's DNS recordids are persistent.
Code: Select all
# Only recordid(s) in DO_DDNS_RECORDID_ARRAY are used in Digitalocean's DNS API.
:local "DO_DDNS_RECORDID_ARRAY" {"host1"=123456789;"host2"=987654321}
:local "DO_DDNS_DOMAIN" "example.com"
:local "DO_TTL" 30
:local "DO_TOKEN" "dop_v1_abcdefghijklmn1234567890opqrstuvwxyz"
:local "DO_HEADER" "Content-Type: application/json,Authorization: Bearer $"DO_TOKEN""
:local "DO_LOG_FILE_PREFIX" "/disk1/logs/DDNS_DO."
:local "do_data" "{\"ttl\":$"DO_TTL", \"data\":\"$"ddns_current_ip"\"}"
:foreach hostname,recordid in=$"DO_DDNS_RECORDID_ARRAY" do={
:local "do_url" "https://api.digitalocean.com/v2/domains/$"DO_DDNS_DOMAIN"/records/$"recordid""
:log info $"do_url"
/tool fetch mode=https http-method=put http-header-field=$"DO_HEADER" http-data=$"do_data" url=$"do_url" dst-path=($"DO_LOG_FILE_PREFIX".$hostname)
}