OPNsense Forum

English Forums => Tutorials and FAQs => Topic started by: siga75 on May 23, 2020, 10:10:25 am

Title: update my public IP address in the A Record of Google Cloud DNS
Post by: siga75 on May 23, 2020, 10:10:25 am
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
Title: Re: update my public IP address in the A Record of Google Cloud DNS
Post by: nikkon on May 24, 2020, 03:06:39 pm
interesting
thanks for sharing it
Title: Re: update my public IP address in the A Record of Google Cloud DNS
Post by: astromeier on June 04, 2020, 09:41:11 pm
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
Title: Re: update my public IP address in the A Record of Google Cloud DNS
Post by: siga75 on June 05, 2020, 04:25:15 pm
Hi,

I put it in crontab to run every 15 minutes

Cheers
Title: Re: update my public IP address in the A Record of Google Cloud DNS
Post by: astromeier on June 06, 2020, 10:04:58 pm
Hi siga, thanks!
This is one way - but i search someting like the Services/Monit thing to trigger the script when nesessary.

cheers