Skip to content

Instantly share code, notes, and snippets.

@gbarre
Created May 12, 2020 14:09
Show Gist options
  • Save gbarre/92d53e2be4ae0497670533ebf96ea0ca to your computer and use it in GitHub Desktop.
Save gbarre/92d53e2be4ae0497670533ebf96ea0ca to your computer and use it in GitHub Desktop.
Update DNS A record with Gandi's API
#!/bin/sh
# Customize with your datas
APIKEY="secret_token"
DOMAIN="my_domain"
RECORD="my_record"
# DO NOT EDIT AFTER THIS LINE
CURRENTIP=$(curl -s ifconfig.co/ip)
IPLENGTH=$(echo -n ${CURRENTIP} | wc -m)
# Check that ifconfig.io give me an IP
if [ ${IPLENGTH} -gt 30 ]
then
exit 1
fi
# Get gandi's NS for my domain
NS=$(curl -s --header "Authorization: Apikey ${APIKEY}" https://api.gandi.net/v5/livedns/domains/${DOMAIN}/nameservers | jq --raw-output '.[0] | sub("^.*?_"; "")')
# Get the last IP recorded
LASTREGISTEREDIP=$(dig +short ${RECORD}.${DOMAIN} @${NS})
# Update if needed
if [ "${CURRENTIP}" != "${LASTREGISTEREDIP}" ]
then
curl -s -X PUT --header "Authorization: Apikey ${APIKEY}" \
--header "Content-Type: application/json" \
-d "{\"rrset_values\": [\"${CURRENTIP}\"], \"rrset_ttl\": "300"}" \
https://api.gandi.net/v5/livedns/domains/${DOMAIN}/records/${RECORD}/A
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment