Last active
August 30, 2024 11:24
-
-
Save dimitur2204/4364c41bc4f85edc7f7a0701f644ccbe to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Initial data; they need to be filled by the user | |
## API token | |
api_token=<api-token> | |
email=<your-clodflare-email> | |
zone_name=<root-domain-name> | |
dns_record=<submdomain> # requires the full sub+domain ex. subdomain.root.com | |
# Function to log messages with timestamp | |
log() { | |
local type="$1" | |
local message="$2" | |
echo "$(date '+%Y-%m-%d %H:%M:%S') [$type] $message" | |
} | |
log "INFO" "Script started." | |
# Get the basic data | |
log "INFO" "Fetching IPv4 and IPv6 addresses." | |
ipv4=$(curl -s -X GET -4 https://ifconfig.co) | |
ipv6=$(curl -s -X GET -6 https://ifconfig.co) | |
log "INFO" "Verifying API token." | |
user_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \ | |
-H "Authorization: Bearer $api_token" \ | |
-H "Content-Type:application/json" \ | |
| jq -r '{"result"}[] | .id') | |
if [ -z "$user_id" ]; then | |
log "ERROR" "Invalid API token or email." | |
exit 1 | |
fi | |
log "INFO" "User ID verified: $user_id" | |
log "INFO" "Zone name: $zone_name" | |
log "INFO" "DNS record: $dns_record" | |
log "INFO" "IPv4: $ipv4" | |
log "INFO" "IPv6: $ipv6" | |
# Get the zone ID | |
log "INFO" "Fetching Zone ID for $zone_name." | |
zone_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name&status=active" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Auth-Email: $email" \ | |
-H "Authorization: Bearer $api_token" \ | |
| jq -r '{"result"}[] | .[0] | .id') | |
if [ -z "$zone_id" ]; then | |
log "ERROR" "Unable to fetch Zone ID. Check if the Zone Name is correct." | |
exit 1 | |
fi | |
log "INFO" "Zone ID fetched: $zone_id" | |
# Update A record (IPv4) | |
if [ -n "$ipv4" ]; then | |
log "INFO" "Checking current A record for $dns_record." | |
dns_record_a_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?type=A&name=$dns_record" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Auth-Email: $email" \ | |
-H "Authorization: Bearer $api_token") | |
dns_record_a_ip=$(echo $dns_record_a_id | jq -r '{"result"}[] | .[0] | .content') | |
log "INFO" "Current IPv4 set on Cloudflare (A Record) is: $dns_record_a_ip" | |
if [ "$dns_record_a_ip" != "$ipv4" ]; then | |
log "INFO" "Updating A record with new IPv4." | |
response=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$(echo $dns_record_a_id | jq -r '{"result"}[] | .[0] | .id')" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Auth-Email: $email" \ | |
-H "Authorization: Bearer $api_token" \ | |
--data "{\"type\":\"A\",\"name\":\"$dns_record\",\"content\":\"$ipv4\",\"ttl\":1,\"proxied\":false}" \ | |
| jq -r '.errors') | |
if [ -z "$response" ]; then | |
log "INFO" "A record updated successfully." | |
else | |
log "ERROR" "Failed to update A record: $response" | |
fi | |
else | |
log "INFO" "The current IPv4 and DNS record IPv4 are the same. No update needed." | |
fi | |
else | |
log "WARNING" "Could not get your IPv4. Check your internet connection or https://ifconfig.co" | |
fi | |
# Update AAAA record (IPv6) | |
if [ -n "$ipv6" ]; then | |
log "INFO" "Checking current AAAA record for $dns_record." | |
dns_record_aaaa_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?type=AAAA&name=$dns_record" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Auth-Email: $email" \ | |
-H "Authorization: Bearer $api_token") | |
dns_record_aaaa_ip=$(echo $dns_record_aaaa_id | jq -r '{"result"}[] | .[0] | .content') | |
log "INFO" "Current IPv6 set on Cloudflare (AAAA Record) is: $dns_record_aaaa_ip" | |
if [ "$dns_record_aaaa_ip" != "$ipv6" ]; then | |
log "INFO" "Updating AAAA record with new IPv6." | |
response=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$(echo $dns_record_aaaa_id | jq -r '{"result"}[] | .[0] | .id')" \ | |
-H "Content-Type: application/json" \ | |
-H "X-Auth-Email: $email" \ | |
-H "Authorization: Bearer $api_token" \ | |
--data "{\"type\":\"AAAA\",\"name\":\"$dns_record\",\"content\":\"$ipv6\",\"ttl\":1,\"proxied\":false}" \ | |
| jq -r '.errors') | |
if [ -z "$response" ]; then | |
log "INFO" "AAAA record updated successfully." | |
else | |
log "ERROR" "Failed to update AAAA record: $response" | |
fi | |
else | |
log "INFO" "The current IPv6 and DNS record IPv6 are the same. No update needed." | |
fi | |
else | |
log "WARNING" "Could not get your IPv6. Check your internet connection or https://ifconfig.co" | |
fi | |
log "INFO" "Script completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment