Skip to content

Instantly share code, notes, and snippets.

@drewchapin
Last active April 13, 2024 04:06
Show Gist options
  • Save drewchapin/57d7039e30e8cc49e30bdc56a194f5bf to your computer and use it in GitHub Desktop.
Save drewchapin/57d7039e30e8cc49e30bdc56a194f5bf to your computer and use it in GitHub Desktop.
A shell script to update the public IP address of a Google DynDNS hostname. Intended to be used on DD-WRT routers.
#!/bin/sh
HOSTNAME="host.yourdomain.com"
USERNAME="username"
PASSWORD="password"
LOG_FILE="/tmp/ddns/ddns.log"
while true; do
PUBLIC_IP=$(curl -s -k https://domains.google.com/checkip)
DDNS_IP=$(nvram get ddns_ipaddr)
if [ "$PUBLIC_IP" != "$DDNS_IP" ]; then
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}")
nvram set ddns_ipaddr=${PUBLIC_IP}
nvram commit
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}
sleep 3600
;;
"badauth")
echo "`date`: The username / password combination is not valid for the host ${HOSTNAME}." >> ${LOG_FILE}
sleep 3600
;;
"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}
sleep 300
;;
*)
echo "`date`: $RESP" >> ${LOG_FILE}
sleep 3600
esac
fi
sleep 60
done
@rafex
Copy link

rafex commented Oct 23, 2019

I did something similar with python, avoiding calls from google domains. Today I have the script running every 5 minutes.

https://github.com/rafex/updateGoogleDomains

@DarrienG
Copy link

DarrienG commented Aug 8, 2021

What the heck is nvram

@samip5
Copy link

samip5 commented Sep 10, 2021

What the heck is nvram

Writing to nvram, "NVRAM (nonvolatile random-access memory) ".

@tinncdev
Copy link

tinncdev commented Jan 5, 2022

Those ones having trouble with nvram can just use another temp file for storing current ip address.

LOG_IP_FILE="/tmp/ddns/ip.log"

# replace get last ip from nvram by reading from file
# DDNS_IP=$(nvram get ddns_ipaddr)
DDNS_IP=$(echo ${LOG_IP_FILE})

# replace set nvram by writing to file
# nvram set ddns_ipaddr=${PUBLIC_IP}
# nvram commit
echo $PUBLIC_IP > ${LOG_IP_FILE}

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