Skip to content

Instantly share code, notes, and snippets.

@gartnera
Last active November 11, 2017 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gartnera/bf7806a2cf2de10b1558bca5546fa6c1 to your computer and use it in GitHub Desktop.
Save gartnera/bf7806a2cf2de10b1558bca5546fa6c1 to your computer and use it in GitHub Desktop.
A script to update a cloudflare DNS record with your current IP
#!/bin/bash
auth_email='YOUR_EMAIL'
auth_key='YOUR_API_KEY'
zone_id='YOUR_ZONE_ID'
#you must create the record before using this script
record_id='YOUR_RECORD_ID'
host_name='YOUR_HOST_NAME'
ip4=$(curl -4 --connect-timeout 2 https://icanhazip.com/ 2>/dev/null)
ip6=$(curl -6 --connect-timeout 2 https://icanhazip.com/ 2>/dev/null)
if [[ -n "$ip4" ]]; then
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$host_name\",\"content\":\"$ip4\",\"ttl\":120,\"proxied\":false}"
fi
if [[ -n "$ip6" ]]; then
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" \
-H "X-Auth-Email: $auth_email" \
-H "X-Auth-Key: $auth_key" \
-H "Content-Type: application/json" \
--data "{\"type\":\"AAAA\",\"name\":\"$host_name\",\"content\":\"$ip6\",\"ttl\":120,\"proxied\":false}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment