From 89ce1ae672d3189bf4fa4697354a892183ca3df7 Mon Sep 17 00:00:00 2001 From: Rich O Date: Sat, 6 Feb 2021 13:57:48 -0500 Subject: [PATCH] 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. --- dns_updater.sh | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/dns_updater.sh b/dns_updater.sh index 5e5e8a8..81010b1 100644 --- a/dns_updater.sh +++ b/dns_updater.sh @@ -10,19 +10,27 @@ RECORD_NAME="example.com" RECORD_TYPE="A" 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)" 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" \ - -H "X-Auth-Email: $AUTH_EMAIL" \ - -H "X-Auth-Key: $CLOUDFLARE_API_KEY" \ - -H "Content-Type: application/json" \ - --data '{"type":"$RECORD_TYPE","name":"'$RECORD_NAME'","content":"'$IP'","ttl":'$TTL'}') +#If the RECORD_IP and IP do not match, then we need to update the record information. +if [ "$RECORD_IP" != "$IP" ]; then + RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ + -H "X-Auth-Email: $AUTH_EMAIL" \ + -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 - echo "$DATE|IP:$IP|CF Update: Success" + if [ $(echo "$RESPONSE" | grep '"success":true' | wc -l) -eq 1 ]; then + 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 - echo "$DATE|IP:$IP|CF Update: Failure" - echo "Response: $RESPONSE" + echo "$DATE|Record IP: $RECORD_IP|Pub IP: $IP|CF Update: Skipped" fi