Updated script to fix issues in put command

This commit is contained in:
2025-10-05 20:21:48 +01:00
parent b8d7db00b1
commit 090c205946
6 changed files with 71 additions and 98 deletions
@@ -0,0 +1,134 @@
# 🔁 Dynamic DNS Updater for Cloudflare with Mullvad IP
This script updates A records in Cloudflare with your current public IP as reported by [Mullvad](https://mullvad.net)'s IP check service. It supports:
- Multiple domains/zones via `.env` files
- Secure API access using **Cloudflare API Tokens**
- Smart updates: only changes DNS if your IP has changed
- Daily logging to `./logs/`
---
## 🚀 Features
- ✅ Fetches current public IP from Mullvad API (`https://ipv4.am.i.mullvad.net/json`)
- ✅ Uses `.env` files to manage multiple Cloudflare DNS records
- ✅ Supports **API Tokens** (safer than global keys)
- ✅ Skips unnecessary updates if IP hasn't changed
- ✅ Logs every run to `logs/update_YYYY-MM-DD.log`
---
## 📁 Project Structure
```
.
├── update_cloudflare_ip.sh # Main script
├── zones/ # Folder for per-domain config files
│ ├── example_com.env
│ └── another_domain.env
└── logs/ # Auto-created daily log files
└── update_2025-10-05.log
```
---
## ⚙️ Configuration
1. Create a `zones/` folder (already included in repo).
2. Add one `.env` file per domain/zone, named descriptively. Example:
### `zones/subdomain.domain.com.env`
```
ZONE_ID="your_cloudflare_zone_id"
DNS_RECORD_ID="your_dns_record_id"
CLOUDFLARE_API_TOKEN="your_cloudflare_api_token"
DNS_NAME="subdomain.domain.com"
```
---
## ✅ Use Cloudflare API Tokens with permission:
Zone → DNS → Edit for the required zone.
🧪 Usage
Make the script executable:
```
chmod +x update_cloudflare_ip.sh
```
Run it manually:
```
./update_cloudflare_ip.sh
```
Check logs:
```
cat logs/update_$(date +%F).log
```
---
## 🕒 Automation (Cron Example)
To run the script every 10 minutes, edit your crontab:
```
crontab -e
```
Add this line:
```
*/10 * * * * /path/to/update_cloudflare_ip.sh >> /dev/null 2>&1
```
Or, if you want to keep logs automatically:
```
*/10 * * * * /path/to/update_cloudflare_ip.sh >> /path/to/logs/cron.log 2>&1
```
---
## ✅ Requirements
* bash (scripted for Linux/macOS)
* curl
* jq (lightweight and flexible command-line JSON processor): https://github.com/jqlang/jq
Install jq if missing:
```
# Debian/Ubuntu
sudo apt install jq
# macOS (Homebrew)
brew install jq
```
---
## 🔐 Security Tip
Never share your .env files or commit them to public repositories. They contain sensitive credentials.
Use .gitignore to ignore your secrets:
.gitignore
```
zones/*.env
logs/
```
---
## 🙋 FAQ
Q: What happens if the IP hasn't changed?
A: The script detects it and skips the update.
Q: What if one domain fails?
A: The script continues to process the remaining .env files.
Q: Can I use IPv6?
A: Not currently. This script uses Mullvad's IPv4 API. IPv6 support can be added if needed.
@@ -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
@@ -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"