Created
January 30, 2020 04:39
-
-
Save eightyknots/76ae21ba58d1c32e1081b71bd385d626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CF_DEBUG=0 | |
CF_TEMP_FILE="<OUTPUT LOG FILE>" | |
CF_API_TOKEN="<API KEY SCOPED TO UPDATE RECORDS>" | |
CF_ZONE_ID="<DNS ZONE / DOMAIN ID>" | |
CF_RECORD_ID="<DNS RECORD ID FOR A RECORD FROM CLOUDFLARE>" | |
if [ -e ${CF_TEMP_FILE} ]; then | |
rm ${CF_TEMP_FILE} | |
fi | |
DATE=`date` | |
echo "==============================" | |
echo "Start update at ${DATE}" | |
CUR_EXT_IPV4=`wget -qO - ipv4.icanhazip.com` | |
if [ ${#CUR_EXT_IPV4} -gt 6 ]; then | |
# A valid v4 IP address must be in the form X.X.X.X | |
echo "Valid IP found: ${CUR_EXT_IPV4}" | |
# See if we need to update | |
CUR_IPV4_CHECK="\"content\":\"${CUR_EXT_IPV4}\"" | |
CURL_GET_REQUEST="$(curl -s -H "Authorization: Bearer ${CF_API_TOKEN}" -H "Content-Type: application/json" \ | |
-X GET "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_RECORD_ID}")" | |
if [[ $CURL_GET_REQUEST == *"$CUR_IPV4_CHECK"* ]]; then | |
echo "No update neccessary; IP on Cloudflare already matches!" | |
else | |
echo "Updating Cloudflare..." | |
# Proceed to update using curl | |
CURL_UPDATE_REQUEST="$(curl -s --write-out "%{http_code}\n" --output ${CF_TEMP_FILE} -H "Authorization: Bearer ${CF_API_TOKEN}" -H "Content-Type: application/json" \ | |
-d '{"type":"A","name":"trenzalore.ninearches.net","content":"'"${CUR_EXT_IPV4}"'"}' \ | |
-X PUT "https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/dns_records/${CF_RECORD_ID}")" | |
if [ $CURL_UPDATE_REQUEST -eq 200 ]; then | |
echo "Update completed successfully." | |
else | |
echo "Update could not be completed: ${CURL_UPDATE_REQUEST}" | |
fi | |
if [ $CF_DEBUG -eq 1 ]; then | |
cat ${CF_TEMP_FILE} | |
fi | |
fi | |
else | |
echo "Could not get a valid IP." | |
fi | |
echo "End update at ${DATE}" | |
echo "" | |
echo "" | |
# End |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment