Skip to content

Instantly share code, notes, and snippets.

@Moudoux
Last active May 1, 2019 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moudoux/ded37689966350f03eedbc081ef306f0 to your computer and use it in GitHub Desktop.
Save Moudoux/ded37689966350f03eedbc081ef306f0 to your computer and use it in GitHub Desktop.
<?php
/*
Update CloudFlare DNS records via PHP
*/
$api_version = 'v4';
if (isset($argv[1]) == false || isset($argv[2]) == false || isset($argv[3]) == false) {
die('Correct ussage: php CF_DNS_Record_Updater.php <domain> <old_ip> <new_ip>'.PHP_EOL);
}
$domain = $argv[1];
$oldip = $argv[2];
$newip = $argv[3];
echo 'Domain: '.$domain;
echo PHP_EOL.'Old IP: '.$oldip;
echo PHP_EOL.'New IP: '.$newip;
echo PHP_EOL.'CF API Version: '.$api_version;
echo PHP_EOL.PHP_EOL.'Fetching data...';
$authemail = '<CF Email>';
$authkey = '<CF Auth Key>';
$api_uri = 'https://api.cloudflare.com/client/'.$api_version.'/';
$ch = curl_init($api_uri.'zones?page=1&per_page=50&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);
$result = $r['result'];
echo ' Done'.PHP_EOL;
foreach ($result as $zone) {
if (isset($zone['id'])) {
$zoneid = $zone['id'];
$zonename = $zone['name'];
if ($zonename != $domain) {
continue;
}
echo 'Domain: '.$zonename . ', Zone ID: ' . $zoneid . PHP_EOL;
echo 'Scanning DNS records...';
$ch = curl_init($api_uri.'zones/'.$zoneid.'/dns_records');
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);
$dnsrecs = $r['result'];
echo ' Done'.PHP_EOL.PHP_EOL;
foreach ($dnsrecs as $dns) {
if (preg_match("/$oldip/", $dns['content'])) {
$newcontent = preg_replace("/$oldip/", $newip, $dns['content']);
echo 'Updating DNS record... Name: '.$dns['name'].', Type: '.$dns['type'].', Content: '.$dns['content'].', New Content: '.$newip.'...';
$dns['content'] = $newcontent;
updateDNSRecord($dns);
}
}
echo PHP_EOL.'Updated '.$zonename.'\'s DNS records'.PHP_EOL;
echo PHP_EOL;
}
}
function updateDNSRecord($dns) {
global $authemail, $authkey, $api_uri;
$ch = curl_init($api_uri.'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);
if ($r['success'] == true) {
echo ' Success';
} else {
echo ' Fail {'.PHP_EOL;
print_r($r);
echo '}';
}
echo PHP_EOL;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment