Last active
February 15, 2024 04:12
-
-
Save cmilanf/25abe7f4fb3632aed427774bbd3e8b9c to your computer and use it in GitHub Desktop.
Mikrotik RouterOS GANDI dynamic DNS update script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----- Gandi.net LiveDNS dynamic DNS multi domain and subdomain write by Massimo Ciani ------ | |
# Put the following script on "system script" as new script with policy "read, write, test" and name "LiveDNS_DDNS". | |
# If you use pppoe connection, go to ppp profile and click the profile that pppoe use. In the field "on up" put this | |
# line "/system script run LiveDNS_DDNS". without quotes. | |
#--------------- Change Values in this section to match your setup ------------------ | |
# Your LiveDNS API KEY | |
:local apikey "your API KEY" | |
# Set the domain and subdomain to be updated. | |
# Replace the value in the quotations below with your domain and subdomain name. | |
# To specify multiple domain and subdomain, separate them with commas. | |
# IMPORTANT: Before to start the script, remember to create manually the records for all domain. | |
:local domains { "domain1.com"="@,sub1,sub2,sub3" ; "domain2.com"="@" } | |
:local fqdn | |
:local resolver "8.8.8.8" | |
:local subarray | |
# Set the name of interface where get the internet public IP | |
:local inetinterface "pppoe-out1" | |
#------------------------------------------------------------------------------------ | |
# No more changes need | |
# get current IP | |
:global currentIP | |
:if ([/interface get $inetinterface value-name=running]) do={ | |
:global currentIPa [/ip address get [find interface="$inetinterface" disabled=no] address] | |
# cancel netmask from interface IP address | |
:for i from=( [:len $currentIPa] - 1) to=0 do={ | |
:if ( [:pick $currentIPa $i] = "/") do={ | |
:set currentIP [:pick $currentIPa 0 $i] | |
} | |
} | |
:log info "LiveDNS: Current IP is $currentIP" | |
} else={ | |
:log info "LiveDNS: $inetinterface is not currently running, so therefore will not update." | |
:error [:log info "bye"] | |
} | |
# Recursively resolve al subdomain and update on IP changes | |
:foreach dom,subdomains in=$domains do={ | |
:log info "LiveDNS ====== $dom | $subdomains" | |
:set subarray [:toarray $subdomains] | |
:foreach sub in=$subarray do={ | |
:if ($sub = "@") do={ | |
:set fqdn "$dom" | |
} else { | |
:set fqdn "$sub.$dom" | |
} | |
:global previousIP [:resolve domain-name="$fqdn" server="$resolver"] | |
:if ($currentIP != $previousIP) do={ | |
:log info "A LiveDNS $fqdn: Current IP $currentIP is not equal to previous IP, update needed" | |
:log info "LiveDNS $fqdn: Sending update" | |
/tool fetch mode=https http-method=put http-header-field="Content-Type:application/json,X-Api-Key:$apikey" http-data="{\"rrset_name\": \"$sub\", \"rrset_type\": \"A\",\"rrset_ttl\": 300,\"rrset_values\": [\"$currentIP\"]}" url="https://dns.api.gandi.net/api/v5/domains/$dom/records/$sub/A" dst-path="" output=none | |
:log info "LiveDNS $fqdn: updated on Gandi with IP $currentIP" | |
} else={ | |
:log info "LiveDNS $fqdn: Previous IP $previousIP is equal to current IP, no update needed" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment