Last active
January 3, 2026 01:45
-
-
Save zsimic/c39dd9686c6d6b0d149a67ff23286b99 to your computer and use it in GitHub Desktop.
Linode dynamic dns on ubiquiti edge router
This file contains hidden or 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/bash | |
| # See https://gist.github.com/zsimic/c39dd9686c6d6b0d149a67ff23286b99 for docs on how to use | |
| # Note: you can invoke with LOGFILE= for troubleshooting | |
| [ -z "$LOGFILE" ] && LOGFILE=/var/log/messages | |
| if [ -z "$1" -a -f $LOGFILE ]; then # Pass any command line arg to avoid the logging redirect | |
| exec $0 run 1>> $LOGFILE 2>&1 | |
| fi | |
| function do_log { | |
| echo "$(date +'%h %e %T') $(hostname) linode-ddns: $@" | |
| } | |
| function usage { | |
| do_log "Usage error: $@" | |
| exit 1 | |
| } | |
| # Note: you can invoke with CFGFILE= for troubleshooting | |
| [ -z "$CFGFILE" ] && CFGFILE=~/.ssh/linode-ddns-cfg.sh | |
| CFGFOLDER=$(dirname $CFGFILE) | |
| IPFILE="$CFGFOLDER/.last-ip" # Allows to do nothing when IP didn't change, remove file to force re-run | |
| [ ! -f $CFGFILE ] && usage "Config file $CFGFILE does not exist" | |
| source $CFGFILE | |
| [ -z "$TOKEN" ] && usage "TOKEN not defined in $CFGFILE" | |
| [ -z "$RECORDS" ] && usage "RECORDS not defined in $CFGFILE" | |
| URL="https://api.linode.com/v4" | |
| H1="Content-type: application/json" | |
| H2="Authorization: Bearer $TOKEN" | |
| CURRENT_IP=$(ip address show eth0 | grep -Eo 'inet [0-9\.]+' | cut -d " " -f2) | |
| LAST_IP=$([ -f $IPFILE ] && head -1 $IPFILE) | |
| [[ $LAST_IP == $CURRENT_IP ]] && exit 0 | |
| DBG="" | |
| [ ! -d /config/scripts ] && DBG="echo" # Useful to debug, allows to perform actual REST call only on router | |
| for record in $RECORDS; do | |
| OUTPUT=$($DBG curl -s -H"$H1" -H"$H2" "$@" -XPUT $URL/domains/$record -d "{\"target\": \"$CURRENT_IP\"}") | |
| if [ $? -ne 0 ]; then | |
| do_log "Updating record $record failed: $OUTPUT" | |
| exit 1 | |
| fi | |
| done | |
| do_log "Home IP updated to $CURRENT_IP" | |
| [ ! -f $IPFILE ] && touch $IPFILE && chmod 0600 $IPFILE # chmod just because domainId is logged there... | |
| echo $CURRENT_IP > $IPFILE | |
| echo "# Updated on $(date) for $RECORDS" >> $IPFILE |
Author
interesting. Do you have any tutorials you can point me towards?
Author
Yes, like https://www.youtube.com/watch?v=nmE28_BA83w or https://www.youtube.com/watch?v=B4pnGyvj2sM or https://www.youtube.com/watch?v=Cs8yOmTJNYQ - all very good channels, Techno Tim (that last one) probably explains things more in depth
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've moved away from ubiquiti edge routers 😊 I'm using their "dream machine" now and don't want to publicize my IP anymore (reverse tunnel is way better😬)