update my public IP address in the A Record of Google Cloud DNS

Started by siga75, May 23, 2020, 10:10:25 AM

Previous topic - Next topic
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
https://www.signorini.ch
Protectli Pfsense Mi7500L6 Intel 7Th Gen Core I7 7500U 16Gb Ddr4 Ram
512Gb Msata Ssd
6 X Intel Gigabit Ethernet


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
Stay secure!
Thomas

OPNsense 22.x / Qotom Q370G4 ram8G ssd256G

Hi,

I put it in crontab to run every 15 minutes

Cheers
https://www.signorini.ch
Protectli Pfsense Mi7500L6 Intel 7Th Gen Core I7 7500U 16Gb Ddr4 Ram
512Gb Msata Ssd
6 X Intel Gigabit Ethernet

Hi siga, thanks!
This is one way - but i search someting like the Services/Monit thing to trigger the script when nesessary.

cheers
Stay secure!
Thomas

OPNsense 22.x / Qotom Q370G4 ram8G ssd256G