From a435fb75c5aba0cc3c64635941b4965f0f14c47c Mon Sep 17 00:00:00 2001 From: Alper Kanat Date: Sat, 23 Jan 2016 18:47:35 +0200 Subject: [PATCH] initial commit --- Dockerfile | 5 +++++ README.md | 1 + dyndns.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 dyndns.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..444331f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine +MAINTAINER Alper Kanat +RUN apk --no-cache add curl jq +COPY dyndns.sh / +ENTRYPOINT exec /dyndns.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..f65ddeb --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Dynamic DNS using DigitalOcean's DNS Services diff --git a/dyndns.sh b/dyndns.sh new file mode 100755 index 0000000..5610bad --- /dev/null +++ b/dyndns.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +api_host="https://api.digitalocean.com/v2" +sleep_interval=${SLEEP_INTERVAL:-300} + +die() { + echo "$1" + exit 1 +} + +test -z $DIGITALOCEAN_TOKEN && die "DIGITALOCEAN_TOKEN not set!" +test -z $DOMAIN && die "DOMAIN not set!" +test -z $NAME && die "NAME not set!" + +dns_list="$api_host/domains/$DOMAIN/records" +domain_records=$(curl -s -X GET \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + $dns_list) +record_id=$(echo $domain_records| jq ".domain_records[] | select(.name == \"$NAME\") | .id") + +test -z $record_id && die "No record found with given domain name!" + +while ( true ); do + ip="$(curl -s ipinfo.io/ip)" + data="{\"type\": \"A\", \"name\": \"$NAME\", \"data\": \"$ip\"}" + url="$dns_list/$record_id" + + if [[ -n $ip ]]; then + echo "Sending data=$data to url=$url" + + curl -s -X PUT \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + -d "$data" \ + "$url" &> /dev/null + else + echo "IP wasn't retrieved within allowed interval. Will try $sleep_interval seconds later.." + fi + + sleep $sleep_interval +done