Forked from chuanliang/cloudflare-DNS-bulk-update.sh
Created
August 15, 2024 10:37
-
-
Save ghazlewood/0e4ee85f036956f43d349efb03fed10e to your computer and use it in GitHub Desktop.
A bash script to bulk change all IP addresses of A record in all domains in one cloudflare account
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 | |
old_ip="111.111.111.111" | |
new_ip="222.222.222.222" | |
email="my@cloudflare.com" | |
api_token="0123456789abcdefghijklmnopqrstuvwxyz1234" | |
zone_id_list=( $(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/?per_page=500" -H "X-Auth-Email: $email " -H "Authorization:Bearer $api_token " -H "Content-Type: application/json"| jq -r '.result[].id') ) | |
for zone_id in "${zone_id_list[@]}" | |
do | |
echo "zone_id is "$zone_id | |
record_a_list=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?per_page=500&type=A&content=$old_ip " -H "X-Auth-Email: $email " -H "Authorization:Bearer $api_token " -H "Content-Type: application/json") | |
record_a_ip=$(echo $record_a_list | jq -r '{"result"}[] | .[0] | .content') | |
echo "cloudflare dns ip is:" $record_a_ip | |
if [ -n $record_a_ip -a $record_a_ip == $old_ip ] | |
then | |
echo "change the A record" | |
record_list=$(echo $record_a_list | jq -r '.result[].id') | |
for id in ${record_list} | |
do | |
echo "dns_records id is " $id | |
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${id}" -H "X-Auth-Email: $email " -H "Authorization:Bearer $api_token " -H "Content-Type: application/json" --data "{\"content\":\"$new_ip\"}" | |
done | |
fi | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment