Skip to content

Instantly share code, notes, and snippets.

@trnila
Last active February 17, 2017 10:28
Show Gist options
  • Save trnila/fa2375dfdc8a0140ebc9b58cc552553a to your computer and use it in GitHub Desktop.
Save trnila/fa2375dfdc8a0140ebc9b58cc552553a to your computer and use it in GitHub Desktop.
Create or update AAAA dns record on cloudflare
#!/bin/bash
user=user@example.org
pass=insert_apikey
zone_id=insert_zone_id
domain=$(hostname --fqdn)
addr=$(ip a | grep 'inet6 2001' | awk '{print $2}' | cut -d '/' -f 1)
if [ "$domain" == "arch" ]; then
echo "not updating"
exit
fi
if [ "$(dig "$domain" +short AAAA)" == "$addr" ]; then
echo "Ip address not need to be updated"
exit
fi
id=$(curl -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$domain&type=AAAA" \
-H "X-Auth-Email: $user" \
-H "X-Auth-Key: $pass" \
-H "Content-Type: application/json" | jq .result[0].id -r -e)
if [ $? -eq 1 ]; then
curl -X POST "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records" \
-H "X-Auth-Email: $user" \
-H "X-Auth-Key: $pass" \
-H "Content-Type: application/json" \
--data '{"type":"AAAA","name":"'${domain}'","content":"'${addr}'","ttl":120,"proxied":false}'
else
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$id" \
-H "X-Auth-Email: $user" \
-H "X-Auth-Key: $pass" \
-H "Content-Type: application/json" \
--data '{"type":"AAAA","name":"'${domain}'","content":"'${addr}'","ttl":120,"proxied":false}'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment