Skip to content

Instantly share code, notes, and snippets.

@1ma
Last active December 9, 2022 23:41
Show Gist options
  • Save 1ma/8c8fd4b8d4eaa8960cc402be8575247b to your computer and use it in GitHub Desktop.
Save 1ma/8c8fd4b8d4eaa8960cc402be8575247b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Requirements: curl, jq (sudo apt install curl jq)
# First, create an A record manually with a TTL of 120 (2 minutes, the minimum)
# The API key is generated from Vultr's user dashboard.
# The Record ID is a UUID, fetch it from here:
# curl -H "Authorization: Bearer ${VULTR_API_KEY}" https://api.vultr.com/v2/domains/${VULTR_DOMAIN}/records
# Save the script under /opt/dynamic-ip-refresh.sh and create a root cron entry to run it every 3rd minute:
# */3 * * * * /opt/dynamic-ip-refresh.sh
VULTR_API_KEY=<your API key>
VULTR_DOMAIN=<example.com>
VULTR_VPN_RECORD_ID=<subdomain id>
CURRENT_IP=$(curl -s https://ip.1mahq.com/)
DNS_IP=$(curl -s -H "Authorization: Bearer ${VULTR_API_KEY}" -X GET https://api.vultr.com/v2/domains/${VULTR_DOMAIN}/records/${VULTR_VPN_RECORD_ID} | jq -r .record.data)
if [ "$CURRENT_IP" = "$DNS_IP" ]; then
echo "IP ${CURRENT_IP} hasn't changed, exiting."
exit 0
fi
echo "IP changed from ${DNS_IP} to ${CURRENT_IP}, updating DNS record."
echo
curl -i -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${VULTR_API_KEY}" \
--data "{\"data\": \"${CURRENT_IP}\"}" \
-X PATCH https://api.vultr.com/v2/domains/${VULTR_DOMAIN}/records/${VULTR_VPN_RECORD_ID}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment