Skip to content

Instantly share code, notes, and snippets.

@SmileyMan
Last active July 8, 2023 00:05
Show Gist options
  • Save SmileyMan/dfa8ed0d96edbeefadc4592b0f158ece to your computer and use it in GitHub Desktop.
Save SmileyMan/dfa8ed0d96edbeefadc4592b0f158ece to your computer and use it in GitHub Desktop.
Update Cloudflare DNS for both IPv4 and IPv6 Bash Script
#!/bin/bash
## Account details
auth_email='fred@blogs.com'
auth_key='00000000000000000000000000000000'
## Record to update
zone_name='domain.com'
record_name='domain.com'
## Binary locations (dig and jq need to be installed)
curl='/usr/bin/curl -s'
date='/bin/date'
dig='/usr/bin/dig'
jq='/usr/bin/jq'
## Log file
log_file='cloudflare_dns_update.log'
## Get current date and time
date_time=$($date)
## Get current IP's
wanip4=$($dig @resolver1.opendns.com A myip.opendns.com +short -4)
wanip6=$($dig @resolver1.opendns.com AAAA myip.opendns.com +short -6)
## Get zone_identifier
zone_identifier=$(
$curl -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
| $jq '.result[0].id' --raw-output
)
## Get current IPv4 (A) record
record_ip4=$(
$curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=A" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
| $jq '.result[0].content' --raw-output
)
## Get current IPv6 (AAAA) record
record_ip6=$(
$curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=AAAA" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
| $jq '.result[0].content' --raw-output
)
# Start logging
echo ---------------------------------------- >> $log_file
echo Date Time: $date_time >> $log_file
echo ---------------------------------------- >> $log_file
echo Current IP4 Address: $wanip4 >> $log_file
echo Current IP4 Record: $record_ip4 >> $log_file
echo Current IP6 Address: $wanip6 >> $log_file
echo Current IP6 Record: $record_ip6 >> $log_file
echo ---------------------------------------- >> $log_file
## Check IPv4
if [ $wanip4 = $record_ip4 ]; then
echo IPv4 Record matches. Update not needed. >> $log_file
else
echo IPv4 Record does not match. Update needed. >> $log_file
## Get record_identifier for DNS record to update
record_identifier=$(
$curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=A" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
| $jq '.result[0].id' --raw-output
)
## Build valid JSON for update
data_json="{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$wanip4\"}"
## Update record
record_update=$(
$curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data $data_json \
| $jq '.success' --raw-output
)
## Log if success
echo IPv4 Record updated: $record_update >> $log_file
fi
## Check IPv6
if [ $wanip6 = $record_ip6 ]; then
echo IPv6 Record matches. Update not needed. >> $log_file
else
echo IPv6 Record does not match. Update needed. >> $log_file
## Get record_identifier for DNS record to update
record_identifier=$(
$curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=AAAA" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
| $jq '.result[0].id' --raw-output
)
## Build valid JSON for update
data_json="{\"type\":\"AAAA\",\"name\":\"$record_name\",\"content\":\"$wanip6\"}"
## Update record
record_update=$(
$curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data $data_json \
| $jq '.success' --raw-output
)
## Log if success
echo IPv6 Record updated: $record_update >> $log_file
fi
echo ---------------------------------------- >> $log_file && echo >> $log_file
exit 0
@mfedatto
Copy link

mfedatto commented Aug 8, 2022

line 101: [: too many arguments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment