Last active
December 11, 2018 18:33
-
-
Save willvincent/9aae8c9a8d45d49e4dce5d61a2b9e668 to your computer and use it in GitHub Desktop.
DDNS with a Linode domain
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 | |
# Linode DDNS Updater script | |
# By Will Vincent <[email protected]> | |
# | |
# Requirements: | |
# - A Linode account (obviously) | |
# - A domain record already created to update with the script | |
# - curl & jq | |
# | |
# Usage: | |
# Follow api instructions to setup a personal access token | |
# and then make queries to find the domain and record ids | |
# | |
# Run with a cron job.. Known to work on openwrt, should | |
# also run anywhere linux-ish that has curl and jq commands | |
# a basic shell (sh, ash, bash, etc), with an internet | |
# connection to the outsie world. | |
# ------------------------------------------------------ | |
# Configuration | |
# ------------------------------------------------------ | |
TOKEN=PutYourTokenHere | |
DOMAIN_ID=PutYourIdHere | |
RECORD_ID=PutYourRecordIdHere | |
# Set full path if you want it elsewhere than alongside | |
# this script file. | |
LOGFILE=ddns.log | |
# ------------------------------------------------------ | |
PUBLIC_IP=`curl -s https://ipinfo.io/ip` | |
CURRENT_IP=`curl -s -H "Authorization: Bearer $TOKEN" https://api.linode.com/v4/domains/$DOMAIN_ID/records/$RECORD_ID | jq -r .target` | |
payload() { | |
cat <<EOF | |
{ | |
"target": "$PUBLIC_IP" | |
} | |
EOF | |
} | |
# Only update if our IP differs from that already in the domain record | |
if [ "$CURRENT_IP" != "$PUBLIC_IP" ] | |
then | |
if LC_ALL=C awk 'BEGIN{exit(!(ARGV[1] ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/))}' "$PUBLIC_IP" | |
then | |
curl -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -X PUT -d "$(payload)" https://api.linode.com/v4/domains/$DOMAIN_ID/records/$RECORD_ID > /dev/null | |
echo "$(date) :: IP Updated from $CURRENT_IP to $PUBLIC_IP" >> $LOGFILE | |
else | |
echo "$(date) :: Invalid IP! $PUBLIC_IP" >> $LOGFILE | |
# Might also want to fire off an email here... | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment