Skip to content

Instantly share code, notes, and snippets.

@ewollesen
Created December 22, 2015 16:56
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 ewollesen/28d2beb031deacbbd2c7 to your computer and use it in GitHub Desktop.
Save ewollesen/28d2beb031deacbbd2c7 to your computer and use it in GitHub Desktop.
Script for updating IP and IPv6 addresses with dnsimple.com.
#!/bin/bash
# Pass RECORD_ID_IP or RECORD_ID_IP6 on the command line
# EMAIL and TOKEN can come from ~/.auth/dnsimple.sh or the command line
set -e
#set -x
[ -r ~/.auth/dnsimple.sh ] && . ~/.auth/dnsimple.sh
DOMAIN="my-domain.local"
API_HOST="https://api.dnsimple.com"
CURL_RETURN_CODE_EMPTY_RESPONSE=52
OUTPUT="$(mktemp -t dnsimple-update-ip.XXXXXXXX)"
trap cleanup EXIT
function cleanup () {
[ -n "$DEBUG" ] && cat "$OUTPUT"
rm -f "$OUTPUT"
}
function update-record-content () {
RECORD_TYPE="$1"
RECORD_ID="$2"
if [ "$RECORD_TYPE" = "ip" ]; then
CURL_IP_FLAGS="-4"
elif [ "$RECORD_TYPE" = "ip6" ]; then
CURL_IP_FLAGS="-6"
fi
if [ -n "$DEBUG" ]; then
CURL_FLAGS="-v"
if [ "$RECORD_TYPE" = "ip" ]; then
CONTENT="my ipv4 address"
elif [ "$RECORD_TYPE" = "ip6" ]; then
CONTENT="my ipv6 address"
fi
else
CURL_FLAGS="-s"
CONTENT="$(curl $CURL_IP_FLAGS $CURL_FLAGS ifconfig.co)"
fi
if [ -n "$DEBUG" ]; then
nc -l localhost 8080 &
API_HOST="http://localhost:8080"
fi
# EMAIL and TOKEN are read from ~/.auth/dnsimple.sh or the environment
curl $CURL_FLAGS \
-X PUT \
-H "X-DNSimple-Token: ${EMAIL}:${TOKEN}" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d "{\"record\":{\"name\":\"pain\",\"content\":\"${CONTENT}\"}}" \
${API_HOST}/v1/domains/${DOMAIN}/records/${RECORD_ID} \
|| [ -n "$DEBUG" -a $? -eq $CURL_RETURN_CODE_EMPTY_RESPONSE ]
} >> $OUTPUT
[ -z "$RECORD_ID_IP" ] || update-record-content ip "$RECORD_ID_IP"
[ -z "$RECORD_ID_IP6" ] || update-record-content ip6 "$RECORD_ID_IP6"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment