Skip to content

Instantly share code, notes, and snippets.

@0x4C4A
Last active April 8, 2025 10:30
Show Gist options
  • Save 0x4C4A/78ebc72479380f3382782be2f7c6a9e3 to your computer and use it in GitHub Desktop.
Save 0x4C4A/78ebc72479380f3382782be2f7c6a9e3 to your computer and use it in GitHub Desktop.
nsupdate.info update script
#!/usr/bin/env sh
# Script to update nsupdate.info IP when it has changed or if a week has passed.
# Will send an update on reboot, even if the IP is current.
# A nice quick snippet to add to your NAS, I run this on my Synology devices
# Slightly modified from https://www.raphael.li/blog/2015/03/custom-ddns-service-provider/
# - Added weekly update even if no IP change detected
DOMAIN="your-full-domain-here.nsupdate.info"
TOKEN="your-token"
# Evaluate the current remote IP and the one that is currently registered
CURRENT=$(curl -s https://ipv4.nsupdate.info/myip)
CURRENTv6=$(curl -s https://ipv6.nsupdate.info/myip)
SAVED=$(python2 -c "import socket; print socket.gethostbyname('$DOMAIN')")
SAVEDv6=$(python2 -c "import socket; print socket.getaddrinfo('$DOMAIN', 80)[0][4][0]")
LOGFILE='/var/log/nsupdate.log'
TEMPFILE='/var/tmp/nsupdate'
TIMESTAMP_FILE='/var/tmp/nsupdate_timestamp'
# Write the current date, IP, and registered IP into the log file
if [ ! -e "$LOGFILE" ]; then
touch "$LOGFILE"
fi
{
echo '----------------------------'
date
echo "The current external IPv4 is: $CURRENT"
echo "The current external IPv6 is: $CURRENTv6"
echo "The following IPv4 is registered: $SAVED"
echo "The following IPv6 is registered: $SAVEDv6"
} >> "$LOGFILE"
# Check if an update is required - if so, update and verify the response
NEEDS_UPDATE=false
if [ "$CURRENT" != "$SAVED" ]; then
NEEDS_UPDATE=true
else
# Check if the last update was more than a week ago
if [ ! -e "$TIMESTAMP_FILE" ]; then
touch -d "1970-01-01" "$TIMESTAMP_FILE"
fi
LAST_UPDATE=$(stat -c %Y "$TIMESTAMP_FILE")
CURRENT_TIME=$(date +%s)
ONE_WEEK=$((7 * 24 * 60 * 60))
if [ $((CURRENT_TIME - LAST_UPDATE)) -ge $ONE_WEEK ]; then
NEEDS_UPDATE=true
fi
fi
if [ "$NEEDS_UPDATE" = true ]; then
echo "updating..." >> "$LOGFILE"
RESPONSE=$(curl --user "$DOMAIN":"$TOKEN" https://ipv4.nsupdate.info/nic/update)
START=$(python2 -c "print '$RESPONSE'[:5]")
if [ "$START" = " good" ]; then
echo "Update IPv4 successful!" >> "$LOGFILE"
touch "$TIMESTAMP_FILE" # Update the timestamp file
elif [ "$START" = "nochg" ]; then
echo "WARNING: IP has not changed - cache" >> "$LOGFILE"
touch "$TIMESTAMP_FILE" # Update the timestamp file
fi
else
echo 'no update required' >> "$LOGFILE"
fi
# Keep the log down to 1000 lines
cp -R "$LOGFILE" "$TEMPFILE"
tail -n 1000 "$TEMPFILE" > "$LOGFILE"
rm "$TEMPFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment