Updated script to fix issues in put command

This commit is contained in:
Phil 2025-10-05 20:21:48 +01:00
parent b8d7db00b1
commit 090c205946
6 changed files with 71 additions and 98 deletions

View File

@ -1,93 +0,0 @@
#!/bin/bash
# Exit on error
set -e
# --- Configuration ---
ENV_DIR="./zones"
LOG_DIR="./logs"
DATE=$(date +%F)
LOG_FILE="$LOG_DIR/update_${DATE}.log"
# --- Ensure log directory exists ---
mkdir -p "$LOG_DIR"
# --- Logging function ---
log() {
local message="$1"
echo "$message" | tee -a "$LOG_FILE"
}
# --- Step 1: Get current IP from Mullvad ---
log "[*] Fetching IP from Mullvad..."
IP_INFO=$(curl -s https://ipv4.am.i.mullvad.net/json)
CURRENT_IP=$(echo "$IP_INFO" | jq -r '.ip')
if [[ -z "$CURRENT_IP" ]]; then
log "[!] Failed to extract IP address."
exit 1
fi
log "[*] Current public IP is: $CURRENT_IP"
log ""
# --- Step 2: Loop through all .env files ---
for ENV_FILE in "$ENV_DIR"/*.env; do
log "[*] Processing config: $ENV_FILE"
# Load environment variables
set -a
source "$ENV_FILE"
set +a
# Check for required variables
if [[ -z "$ZONE_ID" || -z "$DNS_RECORD_ID" || -z "$CLOUDFLARE_API_TOKEN" || -z "$DNS_NAME" ]]; then
log "[!] Missing required variables in $ENV_FILE"
log ""
continue
fi
# --- Step 3: Get existing DNS record from Cloudflare ---
log "[*] Fetching current DNS record for $DNS_NAME..."
DNS_RECORD=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json")
EXISTING_IP=$(echo "$DNS_RECORD" | jq -r '.result.content')
if [[ "$EXISTING_IP" == "$CURRENT_IP" ]]; then
log "[=] No update needed. $DNS_NAME already points to $CURRENT_IP"
log ""
continue
fi
# --- Step 4: Update DNS record ---
log "[*] IP has changed: $EXISTING_IP$CURRENT_IP"
log "[*] Updating Cloudflare DNS record..."
UPDATE_RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"type\": \"A\",
\"name\": \"$DNS_NAME\",
\"content\": \"$CURRENT_IP\",
\"ttl\": 3600,
\"proxied\": true,
\"comment\": \"Auto-updated via script\"
}")
SUCCESS=$(echo "$UPDATE_RESPONSE" | jq -r '.success')
if [[ "$SUCCESS" == "true" ]]; then
log "[+] Successfully updated $DNS_NAME to $CURRENT_IP"
else
log "[!] Failed to update $DNS_NAME"
log "Cloudflare response:"
echo "$UPDATE_RESPONSE" | tee -a "$LOG_FILE"
fi
log ""
done

View File

@ -1,5 +0,0 @@
ZONE_ID="your_zone_id"
DNS_RECORD_ID="your_dns_record_id"
CLOUDFLARE_API_TOKEN="your_cloudflare_api_token"
DNS_NAME="subdomain.example.com"

View File

@ -0,0 +1,66 @@
#!/bin/bash
# Exit if any command fails
set -e
# Directory containing all .env config files
ENV_DIR="./zones"
# Fetch current IP from Mullvad
echo "[*] Fetching IP info from Mullvad..."
IP_INFO=$(curl -s https://ipv4.am.i.mullvad.net/json)
IP=$(echo "$IP_INFO" | jq -r '.ip')
if [[ -z "$IP" ]]; then
echo "[!] Failed to extract IP address."
exit 1
fi
echo "[*] Current public IP is: $IP"
echo
# Loop through all .env files in ENV_DIR
for ENV_FILE in "$ENV_DIR"/*.env; do
echo "[*] Processing config: $ENV_FILE"
# Load environment variables
set -a
source "$ENV_FILE"
set +a
# Check required variables
if [[ -z "$ZONE_ID" || -z "$DNS_RECORD_ID" || -z "$CLOUDFLARE_EMAIL" || -z "$CLOUDFLARE_API_KEY" || -z "$DNS_NAME" ]]; then
echo "[!] Missing required variables in $ENV_FILE"
continue
fi
# Make the API request
echo "[*] Updating Cloudflare record for $DNS_NAME..."
UPDATE_RESPONSE=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_RECORD_ID" \
-H "Content-Type: application/json" \
-H "X-Auth-Email: $CLOUDFLARE_EMAIL" \
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
-d '{
"name": "'$DNS_NAME'",
"ttl": 3600,
"type": "A",
"comment": "Domain verification record",
"content": "'$IP'",
"proxied": true
}')
SUCCESS=$(echo "$UPDATE_RESPONSE" | jq -r '.success')
if [[ "$SUCCESS" == "true" ]]; then
echo "[+] $DNS_NAME updated successfully!"
else
echo "[!] Failed to update $DNS_NAME."
echo "Cloudflare response:"
echo "$UPDATE_RESPONSE"
fi
echo
done

View File

@ -0,0 +1,5 @@
ZONE_ID="abc123zoneid"
DNS_RECORD_ID="def456recordid"
CLOUDFLARE_EMAIL="you@example.com"
CLOUDFLARE_API_KEY="your_api_key_here"
DNS_NAME="subdomain.example.com"