Skip to content

Instantly share code, notes, and snippets.

@raphiz
Last active February 2, 2020 12:53
Show Gist options
  • Save raphiz/837453f189dca966a69c to your computer and use it in GitHub Desktop.
Save raphiz/837453f189dca966a69c to your computer and use it in GitHub Desktop.
Update script for nsupdate.info (For Synology NAS systems)
#!/usr/bin/env sh
DOMAIN="domain.nsupdate.info"
TOKEN="MYTOKEN"
# Evaluate the current remote IP and the one that is currently registerd
CURRENT=$(curl -s https://ipv4.nsupdate.info/myip)
SAVED=$(python2 -c "import socket; print socket.gethostbyname('$DOMAIN')")
LOGFILE=$( cd "$( dirname "${0}" )" && pwd )/log.txt
TEMPFILE=$( cd "$( dirname "${0}" )" && pwd )/tmp
# Write the current date, ip and registered ip into the log file
if [ ! -e "" ]; then
touch "$LOGFILE"
fi
{
echo '----------------------------'
date
echo "The current external IP is: $CURRENT"
echo "The following IP is regitered: $SAVED"
} >> "$LOGFILE"
# Check if an update is required - if so, update and verify the response
# On the first of every month, update anyway
if [ "$CURRENT" != "$SAVED" ] || [ "$(date +%d)" -eq "01" ]; 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 succesful!" >> "$LOGFILE"
elif [ "$START" = "nochg" ]; then
echo "WARNING: ip has not changed - cache" >> "$LOGFILE"
fi
else
echo 'no update required' >> "$LOGFILE"
fi
# Keep the log down to 100 lines
cp -R "$LOGFILE" "$TEMPFILE"
tail -n 100 tmp > "$TEMPFILE"
rm "$TEMPFILE"
@rosch100
Copy link

Thanks for your script! I updated it so that it supports IPv6 as well. Small bugs removed...

#!/usr/bin/env sh

DOMAIN="domain.nsupdate.info"
TOKEN="MYTOKEN"

# Evaluate the current remote IP and the one that is currently registerd
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'

# Write the current date, ip and registered ip into the log file
if [  ! -e "" ]; 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
# On the first of every month, update anyway
if [ "$CURRENT" != "$SAVED" ] || [ "$(date +%d)" -eq "01" ]; 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 succesful!" >> "$LOGFILE"
    elif [ "$START" = "nochg" ]; then
        echo "WARNING: ip has not changed - cache" >> "$LOGFILE"
    fi
elif [ "$CURRENTv6" != "$SAVEDv6" ] || [ "$(date +%d)" -eq "01" ]; then
    echo "updating..." >> "$LOGFILE"
    RESPONSE=$(curl --user "$DOMAIN":"$TOKEN" https://ipv6.nsupdate.info/nic/update)
    START=$(python2 -c "print '$RESPONSE'[:5]")
    if [ "$START" = " good" ]; then
        echo "Update IPv6 succesful!" >> "$LOGFILE"
    elif [ "$START" = "nochg" ]; then
        echo "WARNING: ip has not changed - cache" >> "$LOGFILE"
    fi
else
    echo 'no update required' >> "$LOGFILE"
fi

# Keep the log down to 100 lines
cp -R "$LOGFILE" "$TEMPFILE"
tail -n 100 "$TEMPFILE" > "$LOGFILE"
rm "$TEMPFILE"

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