-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Mikrotik RouterOS Cloudflare Dynamic DNS Script and API Comman…
…d for List Cloudflare Record ID.txt
- Loading branch information
1 parent
299caec
commit a623471
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
... RouterOS Cloudflare Dynamic DNS Script and API Command for List Cloudflare Record ID.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Cloudflare Dynamic DNS update script | ||
# Required policy: read, write, test, policy | ||
# Add this script to scheduler | ||
# Install DigiCert root CA or disable check-certificate | ||
# Configuration --------------------------------------------------------------------- | ||
|
||
:local TOKEN "__APITOKEN__" | ||
:local ZONEID "__ZONEIDENTIFIER__" | ||
:local RECORDID "__RECORDIDENTIFIER__" | ||
:local RECORDNAME "__DNSRECORD__" | ||
:local WANIF "__WANINTERFACE__" | ||
|
||
#------------------------------------------------------------------------------------ | ||
|
||
:global IP4NEW | ||
:global IP4CUR | ||
|
||
:local url "https://api.cloudflare.com/client/v4/zones/$ZONEID/dns_records/$RECORDID/" | ||
|
||
:if ([/interface get $WANIF value-name=running]) do={ | ||
# Get the current public IP | ||
:local requestip [tool fetch url="https://ipv4.icanhazip.com" mode=https check-certificate=no output=user as-value] | ||
:set IP4NEW [:pick ($requestip->"data") 0 ([:len ($requestip->"data")]-1)] | ||
# Check if IP has changed | ||
:if ($IP4NEW != $IP4CUR) do={ | ||
:log info "CF-DDNS: Public IP changed to $IP4NEW, updating" | ||
:local cfapi [/tool fetch http-method=put mode=https url=$url check-certificate=no output=user as-value \ | ||
http-header-field="Authorization: Bearer $TOKEN,Content-Type: application/json" \ | ||
http-data="{\"type\":\"A\",\"name\":\"$RECORDNAME\",\"content\":\"$IP4NEW\",\"ttl\":120,\"proxied\":false}"] | ||
:set IP4CUR $IP4NEW | ||
:log info "CF-DDNS: Host $RECORDNAME updated with IP $IP4CUR" | ||
} else={ | ||
:log info "CF-DDNS: Previous IP $IP4NEW not changed, quitting" | ||
} | ||
} else={ | ||
:log info "CF-DDNS: $WANIF is not currently running, quitting" | ||
} | ||
|
||
|
||
* [API Command for List Cloudflare Record ID] * | ||
|
||
curl -X GET "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records?name=SUB_DOMAIN" \ | ||
-H "X-Auth-Email: [email protected]" \ | ||
-H "X-Auth-Key: YOUR_API_KEY" \ | ||
-H "Content-Type: application/json" |