It could be useful for someone, so I just put it here
[root@myfw ~]# cat gcloud-dns-updatemyip
#!/bin/sh
######################################################################
#
# update my public IP address in the A Record of Google Cloud DNS
#
# 20200523 - siga: creation
#
######################################################################
# set constants
SCRIPTNAME="$( basename $0 )"
LOGDIR="/var/log"
LOGFILE="${LOGDIR}/${SCRIPTNAME}.log"
ARECORD="www.signorini.ch."
######################################################################
# set functions
log()
{
message="$1"
date="$( date +%Y%m%d-%H%M%S )"
echo "${date}: $message" | tee -a "$LOGFILE"
}
######################################################################
# main
oldip=$( /usr/local/bin/gcloud dns record-sets list --name="$ARECORD" --type="A" -z "external-ch" | awk -v NAME="$ARECORD" '$1==NAME {print $4}' )
newip=$( /usr/local/bin/curl -s ifconfig.me )
if [ "$oldip" != "$newip" ]; then
log "my ip is changed: oldip: $oldip - newip: $newip"
/usr/local/bin/gcloud dns record-sets transaction start --zone="external-ch" | tee -a "$LOGFILE"
/usr/local/bin/gcloud dns record-sets transaction remove "$oldip" --zone="external-ch" --name="$ARECORD" --type="A" --ttl="300" | tee -a "$LOGFILE"
/usr/local/bin/gcloud dns record-sets transaction add "$newip" --zone="external-ch" --name="$ARECORD" --type="A" --ttl="300" | tee -a "$LOGFILE"
/usr/local/bin/gcloud dns record-sets transaction execute --zone=external-ch | tee -a "$LOGFILE"
else
log "myip is not changed: $oldip"
fi
exit 0
interesting
thanks for sharing it
Hi Siga,
thanks for sharing!
How do you trigger that script?
If you trigger when your public IP changes I'd be interested in your solution.
I have a problem in finding that trigger to change the A-record of my provider (Lima-City, via API)
TIA
Thomas
Hi,
I put it in crontab to run every 15 minutes
Cheers
Hi siga, thanks!
This is one way - but i search someting like the Services/Monit thing to trigger the script when nesessary.
cheers