Skip to content

Instantly share code, notes, and snippets.

@Asenar
Forked from gbarre/gandi.sh
Created July 26, 2024 07:53
Show Gist options
  • Save Asenar/c0ebc8c2067a0f6742e352820ed8ca43 to your computer and use it in GitHub Desktop.
Save Asenar/c0ebc8c2067a0f6742e352820ed8ca43 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