Skip to content

Instantly share code, notes, and snippets.

@pfaffman
Created December 7, 2019 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfaffman/ab7ae38727f5d48509667677ab2aaffa to your computer and use it in GitHub Desktop.
Save pfaffman/ab7ae38727f5d48509667677ab2aaffa to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ ! -z "$1" ]
then
DOMAIN=`echo $1 | cut -d "." -f 2-9`
if [ ! -z "$DOMAIN" ]
then
DO_DYNDNS_DOMAIN=$DOMAIN
fi
HOST=`echo $1 | cut -d "." -f 1`
NOPUBLIC=1
else
HOST=$HOSTNAME
fi
if [ ! -z "$2" ]
then
LOCAL_IP="$2"
else
LOCAL_IP="0"
fi
TTL=300
echo $0 "-------------------- Setting IP for $HOST.$DO_DYNDNS_DOMAIN"
########################################
# GIVE SOME HELP
########################################
help_message () {
echo $basename
cat <<EOF
Create or update DNS record for hostname using a Digital Ocean's API.
Requires '$DO_DYNDNS_DOMAIN' and '$DO_API_KEY' to be defined.
DO_DYNDNS_DOMAIN should be a registered domain managed by Digital Ocean.
DO_API_KEY should be a Digital Ocean API key for the account that manages DO_DYNDNS_DOMAIN.
To install this script with your DO_API_KEY into /etc/$BASENAME/$BASENAME and put a symlink
in /etc/NetworkdManager/dispatch.d so that it is run when a network state changes, use:
$basename --install
EOF
}
########################################
# DO WE HAVE A KEY?
########################################
if [ -z "$DO_API_KEY" ]
then
echo DO_API_KEY is empty or not defined
echo
echo
help_message
exit
fi
########################################
# DO WE HAVE A DOMAIN?
########################################
if [ -z "$DO_DYNDNS_DOMAIN" ]
then
echo DO_DYNDNS_DOMAIN is empty or not defined
echo
echo
help_message
exit
fi
########################################
# DO WE HAVE doctl
########################################
if ! ( which doctl 2>&1 > /dev/null )
then
echo Installing jq . . .
sudo snap install --classic doctl
fi
########################################
# GET THE IP #
########################################
if [ $LOCAL_IP == "0" ]
then
IFACE=`ip r get 8.8.8.8 |head -1 |cut -d " " -f 5`
LOCAL_IP=`ip -4 a show $IFACE|grep inet|cut -d " " -f 6|cut -d '/' -f 1`
FIREWALL=`dig +short myip.opendns.com @resolver1.opendns.com`
else
LOCAL_IP="$2"
FIREWALL=$LOCAL_IP
fi
echo "I $IFACE, IP $LOCAL_IP, FW: $FIREWALL. $0 -- $1"
########################################
# Is old-style if-up installed?
########################################
if [ -e /etc/network/if-up.d/do-dyndns ]
then
INSTALL="YES"
sudo rm /etc/network/if-up.d/do-dyndns
fi
########################################
# SHOULD WE INSTALL?
########################################
if [ "$#" == 1 ] || [ "$INSTALL" == "YES" ]
then
BASENAME=`basename $0`
if [ "$1" == "--install" ] || [ "$INSTALL" == "YES" ]
then
echo Installing $BASENAME to /etc/NetworkManager/dispatcher.d/
sudo mkdir -p /etc/$BASENAME
sudo chmod 700 /etc/$BASENAME
echo "#!/usr/bin/env bash" | sudo tee /etc/$BASENAME/$BASENAME
echo "export DO_API_KEY=$DO_API_KEY" | sudo tee -a /etc/$BASENAME/$BASENAME
echo "export DO_DYNDNS_DOMAIN=$DO_DYNDNS_DOMAIN" | sudo tee -a /etc/$BASENAME/$BASENAME
echo `realpath $0` | sudo tee -a /etc/$BASENAME/$BASENAME
sudo chmod -R 600 /etc/$BASENAME
sudo chmod 700 /etc/$BASENAME/$BASENAME
sudo ln -sf /etc/$BASENAME/$BASENAME /etc/NetworkManager/dispatcher.d
sudo /etc/$BASENAME/$BASENAME
exit
else
echo ----------------------------------------
echo Use \"$0 --install\" to install
help_message
echo ----------------------------------------
fi
fi
########################################
# Get existing hostname
########################################
get_host_record() {
echo DELETING DOMAIN RECORDS FOR $HOST.$DO_DYNDNS_DOMAIN >&2
for x in `doctl -t $DO_API_KEY compute domain records list $DO_DYNDNS_DOMAIN |grep $HOST|col1`
do
doctl -t $DO_API_KEY compute domain records delete $DO_DYNDNS_DOMAIN $x --force
done
echo ""
}
########################################
# Define the function that does the work
########################################
set_hostname() {
HOST=$1
DO_DYNDNS_DOMAIN=$2
IP=$3
echo set_hostname: $HOST $DO_DYNDNS_DOMAIN $IP >&2
# Get hostid if host exists
CURRENT_IP=`dig +short $HOST.$DO_DYNDNS_DOMAIN @ns1.digitalocean.com`
if [ "$IP" == "$CURRENT_IP" ]
then
echo "IP has not changed."
return
fi
HOSTID=$(get_host_record)
echo HOSTID: "$HOSTID" >&2
if [ -n "$HOSTID" ]
then
echo Updating $HOSTID >&2
curl -s -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $DO_API_KEY" -d "{\"data\":\"$IP\",\"ttl\":$TTL}" "https://api.digitalocean.com/v2/domains/$DO_DYNDNS_DOMAIN/records/$HOSTID"
echo
else
echo Creating $HOST >&2
curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $DO_API_KEY" -d "{\"type\":\"A\",\"name\":\"$HOST\",\"data\":\"$IP\",\"priority\":null,\"port\":null,\"ttl\":$TTL,\"weight\":null}" "https://api.digitalocean.com/v2/domains/$DO_DYNDNS_DOMAIN/records"
echo
fi
echo "$HOST.$DO_DYNDNS_DOMAIN ($IP) --> `dig +short $HOST.$DO_DYNDNS_DOMAIN @ns1.digitalocean.com`"
}
########################################
# Get on with the show
########################################
set_hostname $HOST $DO_DYNDNS_DOMAIN $LOCAL_IP
if [ -z $NOPUBLIC ]
then
set_hostname "$HOST-public" $DO_DYNDNS_DOMAIN $FIREWALL
fi
echo $0 "-------------------- Setting IP for $HOST.$DO_DYNDNS_DOMAIN DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment