Last active
September 6, 2019 16:59
-
-
Save MartinRogalla/98923d9472dabafc11fd0db809b1b082 to your computer and use it in GitHub Desktop.
Google Cloud Dynamic DNS - Update if current IP mismatches DNS entry.
This file contains hidden or 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 | |
| # Google Cloud Dynamic DNS | |
| # Run this under some kind of cron/timer job. | |
| IP="$(dig +short myip.opendns.com @resolver1.opendns.com)" | |
| ZONE_NAME="com-yourdomain" | |
| DNS_NAME="dynamic-dns-entry.yourdomain.com" | |
| gcloud dns record-sets list -z=$ZONE_NAME --name=$DNS_NAME --type=A --format='value(name,type,ttl,rrdatas)' | while read name type ttl data ; do | |
| LAST_IP=$data | |
| echo Last IP was \"$LAST_IP\", current IP is \"$IP\". | |
| if [ "$LAST_IP" != "$IP" ] | |
| then | |
| echo Updating $DNS_NAME dynamic dns entry to \"$IP\". | |
| gcloud dns record-sets transaction start -z=$ZONE_NAME | |
| gcloud dns record-sets list -z=$ZONE_NAME --name=$DNS_NAME --type=A --format='value(name,type,ttl,rrdatas)' | while read name type ttl data ; do | |
| gcloud dns record-sets transaction remove -z=$ZONE_NAME --name=$name --type=$type --ttl=$ttl "$data"; | |
| done | |
| gcloud dns record-sets transaction add -z=$ZONE_NAME --name=$DNS_NAME --ttl=300 --type=A $IP | |
| gcloud dns record-sets transaction execute -z=$ZONE_NAME | |
| rm -f transaction.yaml | |
| else | |
| echo No update of the dynamic dns entry is necessary. | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment