Skip to content

Instantly share code, notes, and snippets.

@eXtrem0us
Last active July 6, 2025 04:43
Show Gist options
  • Save eXtrem0us/c44b39d57d2b667704528cbd7dc76f28 to your computer and use it in GitHub Desktop.
Save eXtrem0us/c44b39d57d2b667704528cbd7dc76f28 to your computer and use it in GitHub Desktop.
This script uses ArvanCloud DNS/CDN services to implement DDNS for you to access your home computing board or camera...
#!/bin/bash
# === Configuration ===
ZONE_NAME="my-domain.ir"
RECORD_NAME="my-a-record"
API_TOKEN="Apikey xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
API_URL="https://napi.arvancloud.ir/cdn/4.0/domains"
# === Get current public IP ===
CURRENT_IP=$(curl -s ifconfig.io/ip)
# === Get current DNS record IP ===
DNS_IP=$(dig +short ${RECORD_NAME}.${ZONE_NAME})
# === Exit if IPs could not be retrieved ===
if [ -z "$CURRENT_IP" ] || [ -z "$DNS_IP" ]; then
echo "❌ Failed to retrieve IP addresses"
exit 1
fi
echo "βœ… Current IP: $CURRENT_IP"
echo "πŸ“Œ DNS IP: $DNS_IP"
# === If IP has not changed, do nothing ===
if [ "$CURRENT_IP" == "$DNS_IP" ]; then
echo "β„Ή IP has not changed. No update needed."
exit 0
fi
# === Retrieve record ID from ArvanCloud ===
RECORDS=$(curl -s -H "authorization: ${API_TOKEN}" "${API_URL}/${ZONE_NAME}/dns-records")
RECORD_ID=$(echo "${RECORDS}" | jq -r ".data[] | select(.name == \"${RECORD_NAME}\") | .id")
echo The ID for $RECORD_NAME is $RECORD_ID
# === Check if record was found ===
if [ -z "$RECORD_ID" ]; then
echo "❌ DNS record not found."
exit 1
fi
echo "πŸ›  Updating DNS record..."
# === Update DNS record with new IP ===
UPDATE_RESULT=$(curl -s --request PUT --location "$API_URL/$ZONE_NAME/dns-records/$RECORD_ID" \
--header "Content-Type: application/json" \
--header "authorization: $API_TOKEN" \
--data '{
"id": "'"$RECORD_ID"'",
"type": "a",
"name": "'"$RECORD_NAME"'",
"value": [
{
"ip": "'"$CURRENT_IP"'"
}
],
"cloud": false
}')
# === Check if update was successful ===
if echo "$UPDATE_RESULT" | grep -q 'DNS record updated'; then
echo "βœ… DNS record updated successfully to $CURRENT_IP"
else
echo "❌ Failed to update DNS record:"
echo "$UPDATE_RESULT"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment