Forked from benkulbertis/cloudflare-update-record.sh
Last active
June 11, 2022 23:29
-
-
Save nexaknight/507248c89a9fb249e354443fce2c88a8 to your computer and use it in GitHub Desktop.
Cloudflare API - DDNS IPv4-IPv6 - single Record
This file contains hidden or 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
| #!/bin/bash | |
| # This script is forked from https://gist.github.com/benkulbertis/fff10759c2391b6618dd | |
| # DEFAULT VALUES (CHANGE THESE) | |
| token="BEAERTOKEN" | |
| zone_name="example.com" | |
| host="www" | |
| type="AAAA" # change type to "A" if you want to use ipv4 instead | |
| proxied=true # change to false if you want to disable cf-proxy (grey cloud) | |
| ttl="120" | |
| # Check if A-Record (ipv4) or else use AAAA-Record (ipv6) | |
| if [ "$type" == "A" ]; then | |
| ip=$(curl -s https://api4.ipify.org/) | |
| else | |
| ip=$(curl -s https://api6.ipify.org/) | |
| fi | |
| # MAYBE CHANGE THESE | |
| log_file="cloudflare.log" | |
| # LOGGER | |
| log() { | |
| if [ "$1" ]; then | |
| echo -e "[$(date)] - $1" >> $log_file | |
| fi | |
| } | |
| # SCRIPT START | |
| log "Check Initiated" | |
| # get zone_id | |
| zone_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" \ | |
| -H "Authorization: Bearer $token" \ | |
| -H "Content-Type: application/json"| jq -r '.result | .[0].id') | |
| # check if zone exists | |
| if [ $zone_id == 0 ]; then | |
| # if not: exit with error_msg | |
| error_msg = "Zone not found, check if token or zone_name is correct!" | |
| log "$error_msg" | |
| echo $error_msg | |
| exit 0 | |
| else | |
| # get record information (for example: www.example.com) | |
| record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$host.$zone_name" \ | |
| -H "Authorization: Bearer $token" \ | |
| -H "Content-Type: application/json") | |
| record_id=$(echo $record | jq -r '.result | .[0].id') | |
| # get old ip from record | |
| record_ip=$(echo $record | jq -r '.result | .[0].content') | |
| #check if ip has changed | |
| if [ $ip == $record_ip ]; then | |
| echo "IP has not changed. Exit without updating DNS-Record." | |
| exit 0 | |
| else | |
| update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" \ | |
| -H "Authorization: Bearer $token" \ | |
| -H "Content-Type: application/json" \ | |
| --data '{"id":"'"$zone_id"'","type":"'"$type"'","name":"'"$host.$zone_name"'","content":"'"$ip"'","proxied":'$proxied',"ttl":"'"$ttl"'"}') | |
| update_success=$( echo $update | jq -r '.success' ) | |
| # check if update was successful | |
| if [ $update_success == false ]; then | |
| message="API update failed. dumping results:\n$update" | |
| # log and print error-message | |
| log "$message" && echo -e "$message" | |
| exit 1 | |
| else | |
| message="IP changed from: $record_ip to: $ip" | |
| log "$message" | |
| echo "$message" | |
| fi | |
| fi | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment