Last active
April 13, 2024 04:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
What the heck is
nvram
Writing to nvram, "NVRAM (nonvolatile random-access memory) ".
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
What the heck is
nvram