Skip to content

Instantly share code, notes, and snippets.

@killbus
Forked from domaingood/cloudflare_batch.php
Last active September 24, 2023 07:54
Show Gist options
  • Save killbus/459bdc8d4ebec3bceeeba399b20a0763 to your computer and use it in GitHub Desktop.
Save killbus/459bdc8d4ebec3bceeeba399b20a0763 to your computer and use it in GitHub Desktop.
Cloudflare Batch Edit
<?php
// Form a list of all CF IP zones
// For each zone, grab all A records and TXT records matching $oldip
// For each matching record, update it to the new IP address
$authemail = "YOU@YOUREMAIL.COM";
$authkey = "YOURCLOUDFLAREAPIKEY";
// Old IP address to find
$oldip = "ooo.ooo.ooo.ooo";
// New IP address to replace the old one with
$newip = "nnn.nnn.nnn.nnn";
// Function to fetch DNS records for a zone using pagination
function fetchDNSRecords($zoneid, $page, $per_page) {
global $authemail, $authkey;
$ch = curl_init("https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?page=$page&per_page=$per_page");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: ' . $authemail,
'X-Auth-Key: ' . $authkey,
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$r = json_decode($response, true);
return $r['result'];
}
// Function to update a DNS record
function updateDNSRecord($dns) {
global $authemail, $authkey;
$ch = curl_init("https://api.cloudflare.com/client/v4/zones/" . $dns['zone_id'] . "/dns_records/" . $dns['id']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: ' . $authemail,
'X-Auth-Key: ' . $authkey,
'Content-Type: application/json'
));
$data_string = json_encode($dns);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$r = json_decode($response, true);
return $r;
}
// Function to fetch zone data using pagination
function fetchZones($page, $per_page) {
global $authemail, $authkey;
$ch = curl_init("https://api.cloudflare.com/client/v4/zones?page=$page&per_page=$per_page&match=all");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Auth-Email: ' . $authemail,
'X-Auth-Key: ' . $authkey,
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$r = json_decode($response, true);
return $r['result'];
}
$page = 1; // Starting page for zones
$per_page = 50; // Number of zones per page
do {
$zones = fetchZones($page, $per_page);
foreach ($zones as $zone) {
$zoneid = $zone['id'];
$zonename = $zone['name'];
echo "Zone: $zonename, Zone ID: $zoneid\n";
$dns_page = 1; // Starting page for DNS records
do {
$dns_records = fetchDNSRecords($zoneid, $dns_page, $per_page);
foreach ($dns_records as $dns) {
if (preg_match("/$oldip/", $dns['content'])) {
// OK! Change this DNS record.
$newcontent = preg_replace("/$oldip/", $newip, $dns['content']);
echo " - DNS Record ID: {$dns['id']}, Type: {$dns['type']}, Name: {$dns['name']}\n";
echo " Old Content: {$dns['content']}\n";
echo " New Content: $newcontent\n";
// Swap the content then
$dns['content'] = $newcontent;
$result = updateDNSRecord($dns);
echo " Update Result: ";
print_r($result);
echo "\n";
}
}
$dns_page++; // Move to the next page of DNS records
} while (count($dns_records) > 0); // Continue until no more DNS records found on the current page
echo "\n";
}
$page++; // Move to the next page of zones
} while (count($zones) > 0); // Continue until no more zones found on the current page
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment