Skip to content

Instantly share code, notes, and snippets.

@jkcgs
Last active June 8, 2021 15:57
Show Gist options
  • Save jkcgs/2b9b943cbbeb937e8b027f611ef4d14d to your computer and use it in GitHub Desktop.
Save jkcgs/2b9b943cbbeb937e8b027f611ef4d14d to your computer and use it in GitHub Desktop.
Updates a Cloudflare DNS record with an interface address
#!/bin/bash
# this script updates a cloudflare domain record with the address from an interface
# you need to create the A record first, then get the record ID from the API
# (you can capture the ID by accessing the DNS record then lookup for the /dns_records call at the network tab in inspector)
# the use case for this is when you're connecting via PPPoE and it has a dynamic IP
iface="ppp0" # the interface you want to get the address from
zone_id="" # displayed at the sidebar on domain overview
record_id="" # this is hard to get lol
token="" # create a token for the zone with zone:dns:edit
domain="" # the domain you want to update
ip_file="/tmp/cf_ip_$iface.txt"
ip=`ip a s dev $iface | grep inet | cut -d" " -f6`
echo "Interface $iface address: $ip"
if [ ! -f $ip_file ] || [ `cat $ip_file` != "$ip" ]; then
data='{"type":"A","name":"'"$domain"'","content":"'"$ip"'","ttl":1}'
#echo "Data: $data"
echo "Updating address..."
curl -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 "$data"
echo
echo "$ip" > $ip_file
else
echo "IP not updated"
fi
echo "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment