Skip to content

Instantly share code, notes, and snippets.

@sirgt
Forked from ryanrhanson/dns.php
Created August 26, 2014 17:32
Show Gist options
  • Save sirgt/6491b969b85163b95d86 to your computer and use it in GitHub Desktop.
Save sirgt/6491b969b85163b95d86 to your computer and use it in GitHub Desktop.
<?php
/*
This script will take all dns records on a SoftLayer account and create a dns zone file for them in the 'zones' subdirectory. If this subdirectory does not exist, it will be created.
If single_use is set to true, you can supply your API Username and Key as arguments to the script. single_use also assumes that it is in the same directory as the SoftLayer folder from the PHP API Client. If single_use is false, it will draw from a file in the 'configuration' subdirectory named apiconfig.php, which should define apiUsername, apiKey, and the path to the SoapClient.class.php file.
*/
$single_use = true;
if ($single_use) {
require_once('SoftLayer/SoapClient.class.php');
$apiUsername = readline("API User: ");
$apiKey = readline("API Key: ");
}
else {
require_once('configuration/apiconfig.php');
}
//Create connection to the SoftLayer_Account API Service.
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
//Set an Object Mask to return domain information.
$dnsMask = new SoftLayer_ObjectMask();
$dnsMask = 'mask[domains[id,name]]';
$client->setObjectMask($dnsMask);
$dnslist = $client->getObject();
/*
Loop through the array we got back from SoftLayer_Account, and for each domain, create a zonefile in the 'zones' sudir with the following format:
'zones/<domain name>.db' - This should be easy enough to drop into a cpanel server, as it contains zone information formatted as a standard zone file.
*/
if (!is_dir('zones')){
mkdir('zones');
}
foreach ($dnslist->domains as $domainlist) {
$zones = SoftLayer_SoapClient::getClient('SoftLayer_Dns_Domain', $domainlist->id, $apiUsername, $apiKey);
$zonefile = $zones->getZoneFileContents();
echo "Creating zone file for " . $domainlist->name . " in 'zones/" . $domainlist->name . ".db'\n\r";
$f = fopen("zones/" . $domainlist->name . ".db", "w");
fwrite ($f, $zonefile);
fclose($f);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment