Hello again everyone,
I was trying to make my own script to update the Cloudflare DNS records for my domain whenever my IP changes using the Cloudflare API (specifically this endpoint: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-patch-dns-record).
The problem I am facing is that it doesn’t work when I run it. It gets to the point where it makes the request, but after that, nothing happens, which lead me to believe that might be the issue.
However, I have:
- Verified that the command I use to make the requests works by testing it in the normal terminal and checking the Cloudflare Dashboard if the IP has indeed been updated (it works!).
- Verified that the arguments (zone id, record id, ip, record name and api key) are correct in the context of the script (by logging them before sending the request).
So…I’m kind of at a loss again and I have to rely on you for support. I hope you can help me understand what is happening. Thank you in advance.
Here’s the code for the script:
#--------------- Change Values in this section to match your setup ------------------
# Script originally for OPENDNS (https://support.opendns.com/hc/en-us/articles/227987847-Mikrotik-WinBox-Dynamic-Update-Script)
# Update-only API key
:local dnskey "my_dnskey"
# DNS zone and record ids
:local dnszoneid "my_zoneid"
:local dnsrecordname "*"
:local dnsrecordid "my_recordid"
# Change to the name of interface that gets the changing IP address
# May not be needed for your model number - commenting out this line may still work for single interface devices or if this is not supplied in the script currently being used
:local inetinterface "pppoe-out"
#------------------------------------------------------------------------------------
# No more changes needed, one optional change
:global previousIP;
:log info "DNS-UPDATE: Fetching current IP"
# Get the current public IP using DNS-O-Matic service into the currentIP variable
# :local currentIP [/tool fetch url="http://myip.dnsomatic.com/" mode=http as-value output=user]
# Get the current public IP using the pppoe-client
:local output [/interface pppoe-client monitor 0 once as-value]
:local currentIP ($output->"local-address")
:log info "DNS-UPDATE: Fetched current IP as $currentIP"
# --------- Optional check to only run if the IP has changed (one line: :if)
# to disable, set line below to: ":if ($currentIP != 1) do={"
:if ($currentIP != $previousIP) do={
:log info "DNS-UPDATE: Update needed"
:set previousIP $currentIP
:log info "DNS-UPDATE: Sending update for zone with ID $dnszoneid"
# Send the request to Cloudflare API
:local dnsReply [/tool fetch http-method=patch url="https://api.cloudflare.com/client/v4/zones/$dnszoneid/dns_records/$dnsrecordid" http-data="{\"content\": \"$currentIP\",\"name\": \"$dnsrecordname\",\"proxied\": true,\"type\": \"A\"}" http-header-field="Authorization: Bearer $dnskey,Content-Type: application/json" as-value output=user]
# Wait for response
:delay 2s
# Log to let the user know about the result
:if ($dnsReply->"status" = "finished") do={
:log info "DNS-UPDATE: Update successful."
} else={
:if ($dnsReply->"status" = "failed") do={
:log error "DNS-UPDATE: Update failed."
}
}
} else={
:log info "DNS-UPDATE: Previous IP ($previousIP) and current IP ($currentIP) equal, no update need"
}