Skip to content

Instantly share code, notes, and snippets.

@BenMcLean
Forked from drewchapin/update-google-dyndns.sh
Last active March 3, 2024 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenMcLean/7cdb6448f3a277658e07f5007f4d8031 to your computer and use it in GitHub Desktop.
Save BenMcLean/7cdb6448f3a277658e07f5007f4d8031 to your computer and use it in GitHub Desktop.
A shell script to update the public IP address of a Google DynDNS hostname.
#!/bin/bash
# Google Domains provides an API to update a DNS "Synthetic record". This script
# updates a record with the script-runner's public IP, as resolved using a DNS
# lookup.
#
# Google Dynamic DNS: https://support.google.com/domains/answer/6147083
# Synthetic Records: https://support.google.com/domains/answer/6069273
# Original script: https://gist.github.com/cyrusboadway/5a7b715665f33c237996
# Original modified script: https://gist.github.com/drewchapin/57d7039e30e8cc49e30bdc56a194f5bf
# To automate this, run command "crontab -e" to edit crontab, and add the line
# "*/15 * * * * sh pathtoscript" without quotes to run every 15 minutes.
USERNAME=""
PASSWORD=""
HOSTNAME=""
LOG_FILE="/tmp/ddns/log.log"
LOG_IP_FILE="/tmp/ddns/ip.log"
PUBLIC_IP=$(curl -s "https://checkip.amazonaws.com/")
DDNS_IP=$(cat ${LOG_IP_FILE})
if [[ "$PUBLIC_IP" != "$DDNS_IP" ]]; then
echo "`date`: New public IP is \"${PUBLIC_IP}\" while old public IP was \"${DDNS_IP}\"." >> ${LOG_FILE}
URL="https://domains.google.com/nic/update?hostname=${HOSTNAME}&myip=${PUBLIC_IP}"
RESP=$(curl -s -k --user "${USERNAME}:${PASSWORD}" "$URL")
case $RESP in
"good ${PUBLIC_IP}" | "nochg ${PUBLIC_IP}")
echo $PUBLIC_IP > ${LOG_IP_FILE}
echo "`date`: ${HOSTNAME} successfully updated to ${PUBLIC_IP}." >> ${LOG_FILE}
;;
"nohost")
echo "`date`: The host ${HOSTNAME} does not exist, or does not have Dynamic DNS enabled." >> ${LOG_FILE}
;;
"badauth")
echo "`date`: The username / password combination is not valid for the host ${HOSTNAME}." >> ${LOG_FILE}
;;
"notfqdn")
echo "`date`: The supplied hostname ${HOSTNAME} is not a valid fully-qualified domain name." >> ${LOG_FILE}
exit
;;
"badagent")
echo "`date`: Your Dynamic DNS client is making bad requests. Ensure the user agent is set in the request." >> ${LOG_FILE}
exit
;;
"abuse")
echo "`date`: Dynamic DNS access for the hostname ${HOSTNAME} has been blocked." >> ${LOG_FILE}
exit
;;
"911")
echo "`date`: An error happened on Googles end. Wait 5 minutes and retry." >> ${LOG_FILE}
;;
*)
echo "`date`: $RESP" >> ${LOG_FILE}
esac
#else
# echo "`date`: Everything's good." >> ${LOG_FILE}
fi
@jeremie-j
Copy link

l20: DDNS_IP=$(echo ${LOG_IP_FILE})

Change echo by cat, else the following if will always be true cause value of DDNS_IP would be "/tmp/ddns/ip.log" and PUBLIC_IP something like 255.255.255.255

@BenMcLean
Copy link
Author

Not sure I fully understand.
Do you mean "change echo to cat"?
So line 20 would read, DDNS_IP=$(cat ${LOG_IP_FILE})?

@jeremie-j
Copy link

Yep

@BenMcLean
Copy link
Author

OK thanks. I would appreciate any other suggestions or if there's anything better to use since I'd rather not maintain this if I don't have to. I'm not a particularly big fan of Google or anything.

@jeremie-j
Copy link

No problems, thanks for the this script. I don't have anything to add since it look like to work well ;)

@BenMcLean
Copy link
Author

I should have included instructions on how to find out if this script is still running and how to look at the log because I don't remember anymore LOL

@BenMcLean
Copy link
Author

I found out it wasn't logging unless you touch the log files first. Oops.

I am now thinking that I should have used ddclient instead. https://docs.linuxserver.io/images/docker-ddclient

@BenMcLean
Copy link
Author

Google is doing their usual thing of shutting down perfectly good products and services that were making them money for no reason. Google Domains is transferring to Squarespace and Squarespace doesn't support Dynamic DNS, so I don't want them.
Instead, I switched to Cloudflare as my registrar and had this installed and working before I realized it is no longer developed. Please post if you know of a more recent utility for Cloudflare DDNS, especially if it is nice enough to just be a Docker install that just works.

@domingues
Copy link

@BenMcLean
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment