Skip to content

Instantly share code, notes, and snippets.

@ryanrhanson
Created June 18, 2013 03:49
Show Gist options
  • Save ryanrhanson/5802559 to your computer and use it in GitHub Desktop.
Save ryanrhanson/5802559 to your computer and use it in GitHub Desktop.
Add A records in bulk to a dns zone.
<?php
/*
This relies on the Softlayer PHP API Client, available at https://github.com/softlayer/softlayer-api-php-client.
Please make sure you have set this up before running the script. To run this, just drop it in the same directory as the "SoftLayer" folder and run 'php dns_bulkAddARecords.php' from the terminal. It will prompt for three things:
API User - This is your SL Username
API Key - Available at https://manage.softlayer.com/Administrative/apiKeychain or https://control.softlayer.com/account/user/profile/
Domain name - Domain to modify
File name - File to read from, with following format:
$host $target
Ex: www 127.0.01
This script will read all lines in from the specified file and add them as A records to the domain you provide.
*/
// Set up our username/key if $single_use is true. Otherwise, read from an apiconfig file that will need to provide this and 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');
}
// Grab domain name to modify and filename to import.
$domName = readline('Domain name: ');
$filename = readline('File name: ');
// Connect to the SoftLayer_Dns_Domain service so we can grab the domain's ID from the name.
$domClient = SoftLayer_SoapClient::getClient('SoftLayer_Dns_Domain', null, $apiUsername, $apiKey);
try {
$dom = $domClient->getByDomainName($domName);
} catch (Exception $e){
echo $e->getMessage(). "\n\r";
}
// Connect to the SoftLayer_Dns_Domain_ResourceRecord service, as we need to add records here.
$recClient = SoftLayer_SoapClient::getClient('SoftLayer_Dns_Domain_ResourceRecord', null, $apiUsername, $apiKey);
// Create an array to store all of our new records.
$records = array();
// Check to see if the specified file exists, and if it does, open it to import. We loop through to grab each line and add each to an array that contains our ResourceRecord data. When done, close out the file.
if (file_exists($filename) && is_readable($filename)) {
$fh = fopen($filename, "r");
while (!feof($fh)) {
list ($recordHost, $recordData) = fscanf($fh, "%s %s");
$singlerecord = array (
'id' => NULL,
'data' => $recordData,
'domainId' => $dom[0]->id,
'host' => $recordHost,
'ttl' => 900,
'type' => 'a'
);
$records[] = (object)$singlerecord;
}
fclose ($fh);
}
// Dump the records array into the dns zone, and we're done.
try {
$recClient->createObjects($records);
} catch (Exception $e){
echo $e->getMessage(). "\n\r";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment