Added comparison of current pub IP to the DNS record. DNS Record will only be updated if the record does not match the current public IP.

This commit is contained in:
rich committed 2021-02-06 13:57:48 -05:00
1 parent afc41f75b8
commit 89ce1ae672
1 file changed
+17 -9
+17 -9
View File
@@ -10,19 +10,27 @@ RECORD_NAME="example.com"
RECORD_TYPE="A" RECORD_TYPE="A"
TTL=1 TTL=1
#Get the current IP address for the DNS record
RECORD_IP="$(dig @1.1.1.1 $RECORD_NAME | grep $RECORD_NAME | grep -v ';' | awk '{ print $5} ')"
IP="$(curl -s http://ipinfo.io/ip 2> /dev/null)" IP="$(curl -s http://ipinfo.io/ip 2> /dev/null)"
DATE=$(date +%Y%m%d-%H%M%S) DATE=$(date +%Y%m%d-%H%M%S)
RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ #If the RECORD_IP and IP do not match, then we need to update the record information.
-H "X-Auth-Email: $AUTH_EMAIL" \ if [ "$RECORD_IP" != "$IP" ]; then
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \ RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Content-Type: application/json" \ -H "X-Auth-Email: $AUTH_EMAIL" \
--data '{"type":"$RECORD_TYPE","name":"'$RECORD_NAME'","content":"'$IP'","ttl":'$TTL'}') -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-H "Content-Type: application/json" \
--data '{"type":"$RECORD_TYPE","name":"'$RECORD_NAME'","content":"'$IP'","ttl":'$TTL'}')
if [ $(echo "$RESPONSE" | grep '"success":true' | wc -l) -eq 1 ]; then if [ $(echo "$RESPONSE" | grep '"success":true' | wc -l) -eq 1 ]; then
echo "$DATE|IP:$IP|CF Update: Success" echo "$DATE|Record IP: $RECORD_IP|Pub IP: $IP|CF Update: Success"
else
echo "$DATE|Record IP: $RECORD_IP|Pub IP: $IP|CF Update: Failure"
echo "Response: $RESPONSE"
fi
else else
echo "$DATE|IP:$IP|CF Update: Failure" echo "$DATE|Record IP: $RECORD_IP|Pub IP: $IP|CF Update: Skipped"
echo "Response: $RESPONSE"
fi fi