Skip to content

Instantly share code, notes, and snippets.

@calee0219
Last active August 9, 2021 12:28
Show Gist options
  • Save calee0219/2b675f90ac67c91575c435a1a7e4c86d to your computer and use it in GitHub Desktop.
Save calee0219/2b675f90ac67c91575c435a1a7e4c86d to your computer and use it in GitHub Desktop.
#!/bin/sh
API_TOKEN=
DNS_DUAL=
DNS_V4=
DNS_V6=
IP_MEM_FILE=/tmp/ddns_ip.txt
if [ -f ${IP_MEM_FILE} ]; then source ${IP_MEM_FILE}; fi
if test -z ${previous_ipv4}; then previous_ipv4="0.0.0.0"; fi
if test -z ${previous_ipv6}; then previous_ipv6="::0"; fi
ipv4=$(curl -s ipinfo.io | sed -n 2p | awk -F"\"" '{print $4}')
ipv6=$(ip -6 a show dev eth1 scope global noprefixroute | grep -v "deprecated" | grep "inet6" | sed -n 1p | awk '{print $2}')
ipv6=${ipv6::-3}
#echo ${ipv4}
#echo ${ipv6}
UPDATED=false
# Update A record
if [ "${ipv4}" != "${previous_ipv4}" ]; then
v4payload=\{\"type\":\"A\",\"name\":\"${DNS_DUAL}\",\"content\":\"${ipv4}\",\"ttl\":120\}
curl -X PUT "https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records/:identifier" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${API_TOKEN}" \
--data ${v4payload}
echo ""
v4payload=\{\"type\":\"A\",\"name\":\"${DNS_V4}\",\"content\":\"${ipv4}\",\"ttl\":120\}
curl -X PUT "https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records/:identifier" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${API_TOKEN}" \
--data ${v4payload}
echo ""
UPDATED=true
fi
# Update AAAA record
if [ "${ipv6}" != "${previous_ipv6}" ]; then
v6payload=\{\"type\":\"AAAA\",\"name\":\"${DNS_DUAL}\",\"content\":\"${ipv6}\",\"ttl\":120\}
curl -X PUT "https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records/:identifier" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${API_TOKEN}" \
--data ${v6payload}
echo ""
v6payload=\{\"type\":\"AAAA\",\"name\":\"${DNS_V6}\",\"content\":\"${ipv6}\",\"ttl\":120\}
curl -X PUT "https://api.cloudflare.com/client/v4/zones/:zone_identifier/dns_records/:identifier" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${API_TOKEN}" \
--data ${v6payload}
echo ""
UPDATED=true
fi
if ${UPDATED}; then printf "previous_ipv4=$ipv4\nprevious_ipv6=$ipv6\n" > ${IP_MEM_FILE}; fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment