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
  • 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
@BenMcLean
Copy link
Author

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