Last active
July 6, 2025 04:43
-
-
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...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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