Skip to content

Instantly share code, notes, and snippets.

@mbierman
Last active September 4, 2024 04:52
Show Gist options
  • Save mbierman/b9edadfe691a565c2a45a2b34f1902ac to your computer and use it in GitHub Desktop.
Save mbierman/b9edadfe691a565c2a45a2b34f1902ac to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
zoneID="your_zone_ID"
ID="your_ID"
APIToken="your_API_token"
name="your_domain"
DIRECTORY=/data/logs
log="$DIRECTORY/updateIP.log"
echo $log
logMe () {
tail -n 2020 $log > $log.tmp
mv $log.tmp $log
exit
}
IPINFO=$(curl -s https://ipinfo.io/)
provider=$(echo $IPINFO | jq -r ".org")
currentIP=$(echo $IPINFO | jq -r ".ip")
cloudflaresays=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records?type=A&name=private.thebiermans.net&page=1&per_page=5&match=all" \
-H "Authorization: Bearer $APIToken" \
-H "Content-Type: application/json" | grep content | sed -e 's|{.*"content":"||g' | sed -e 's|","proxiable.*||g')
if [ "$currentIP" = "$cloudflaresays" ] ; then
echo -e "======\n$(date) No change required ($currentIP)" | tee -a $log
logMe
else
comment="updated: $(date)"
echo -e "======\n $(date) IP updated ($currentIP)"| tee -a $log
logMe
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records/$ID" \
-H "Authorization: Bearer $APIToken" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$name\",\"content\":\"$currentIP\",\"ttl\":600,\"proxied\":true,\"comment\":\"$comment\"}"
fi
logMe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment