OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • Tutorials and FAQs »
  • update my public IP address in the A Record of Google Cloud DNS
« previous next »
  • Print
Pages: [1]

Author Topic: update my public IP address in the A Record of Google Cloud DNS  (Read 2772 times)

siga75

  • Full Member
  • ***
  • Posts: 187
  • Karma: 11
    • View Profile
    • www.signorini.ch
update my public IP address in the A Record of Google Cloud DNS
« 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
« Last Edit: May 23, 2020, 12:03:17 pm by siga75 »
Logged
https://www.signorini.ch
Protectli Pfsense Mi7500L6 Intel 7Th Gen Core I7 7500U 16Gb Ddr4 Ram
512Gb Msata Ssd
6 X Intel Gigabit Ethernet

nikkon

  • Full Member
  • ***
  • Posts: 124
  • Karma: 3
    • View Profile
Re: update my public IP address in the A Record of Google Cloud DNS
« Reply #1 on: May 24, 2020, 03:06:39 pm »
interesting
thanks for sharing it
Logged
DEC750 Deciso

astromeier

  • Jr. Member
  • **
  • Posts: 86
  • Karma: 9
    • View Profile
Re: update my public IP address in the A Record of Google Cloud DNS
« Reply #2 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
Logged
Stay secure!
Thomas

OPNsense 22.x / Qotom Q370G4 ram8G ssd256G

siga75

  • Full Member
  • ***
  • Posts: 187
  • Karma: 11
    • View Profile
    • www.signorini.ch
Re: update my public IP address in the A Record of Google Cloud DNS
« Reply #3 on: June 05, 2020, 04:25:15 pm »
Hi,

I put it in crontab to run every 15 minutes

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

astromeier

  • Jr. Member
  • **
  • Posts: 86
  • Karma: 9
    • View Profile
Re: update my public IP address in the A Record of Google Cloud DNS
« Reply #4 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
Logged
Stay secure!
Thomas

OPNsense 22.x / Qotom Q370G4 ram8G ssd256G

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • Tutorials and FAQs »
  • update my public IP address in the A Record of Google Cloud DNS
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2024 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2