Created
February 7, 2011 21:39
-
-
Save softlayer/815278 to your computer and use it in GitHub Desktop.
setReverseDns
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
<?php | |
/** | |
* Add/update a reverse DNS record by providing an IP and FQDN. | |
* | |
* This assumes the SoftLayer API PHP client | |
* <http://github.com/softlayer/softlayer-api-php-client> is installed in the | |
* directory '/SoftLayer' in this script's path and that you wish to use the | |
* SOAP client instead of our XML-RPC client. | |
* | |
* @see http://sldn.softlayer.com/wiki/index.php/SoftLayer_Network_Subnet | |
* @license <http://sldn.softlayer.com/article/License> | |
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com> | |
*/ | |
require_once('SoftLayer/SoapClient.class.php'); | |
/** | |
* Set your SoftLayer API username and key. | |
*/ | |
$apiUser = 'USER HERE'; | |
$apiKey = 'KEY HERE'; | |
/** | |
* IP address to modify, and the domain name to use | |
*/ | |
$ipAddress = 'IP HERE'; | |
$domainName = 'server1.example.com.'; // Fully Qualified Domain Name | |
/** | |
* Connect to the SoftLayer_Dns_Domain_ResourceRecord and | |
* SoftLayer_Network_Subnet services | |
*/ | |
$dnsClient = SoftLayer_SoapClient::getClient('SoftLayer_Dns_Domain_ResourceRecord', NULL, $apiUser, $apiKey); | |
$subnetClient = SoftLayer_SoapClient::getClient('SoftLayer_Network_Subnet', NULL, $apiUser, $apiKey); | |
try { | |
setPtrForIpv4($ipAddress, $domainName); | |
} catch( Exception $e) { | |
die($e->getMessage()); | |
} | |
/** | |
* Setup a dummy SoftLayer Dns Domain ResourceRecord object for the resource record. | |
* The only difference between an update to a record and a new record is population of the id key. | |
*/ | |
function setPtrForIpv4($ip, $FQDN, $ttl = 86400) { | |
global $subnetClient; | |
/// Store id for specific IPs subnet, then discard the subnet object | |
$subnetId = $subnetClient->getSubnetForIpAddress($ip)->id; | |
// Store all of the existing reverse DNS records | |
$existingRecords = getReverseRecords($subnetId); | |
// Store the last octet of the server's IP for use as the host record | |
$host = implode(array_slice(explode('.', $ip), -1, 1)); | |
$newRecord = array ( | |
'id' => NULL, | |
'data' => $FQDN, | |
'domainId' => $existingRecords->id, | |
'host' => $host, | |
'ttl' => (int)$ttl, | |
'type' => 'ptr' | |
); | |
$newRecord = (object)$newRecord; | |
// If we already have a record for this host, set the ID of the existing record, then | |
// set the FQDN | |
foreach ($existingRecords->resourceRecords as $record) { | |
if ($record->host == $host) { | |
$newRecord->id = $record->id; | |
$newRecord->data = $FQDN; | |
} | |
} | |
updateDnsZone($newRecord); | |
} | |
/** | |
* Updates or creates a reverse DNS record utilizing the given SoftLayer Dns Domain ResourceRecord | |
*/ | |
function updateDnsZone($newRecord) { | |
global $dnsClient; | |
// Default to creation | |
$method = "createObject"; | |
// If we have an id set, change the method to editObject, and setup the SoftLayer_Dns_Domain_ResourceRecord | |
// to interact with the existing record | |
if (!is_null($newRecord->id)) { | |
$method = "editObject"; | |
$dnsClient->setInitParameter($newRecord->id); | |
} | |
// Create/edit the record, and return results | |
return $dnsClient->$method($newRecord); | |
} | |
/** | |
* Return an array of reverse DNS resource records when provided a subnet id | |
*/ | |
function getReverseRecords($subnetId) { | |
global $subnetClient; | |
// Setup the client to interact with the specific subnet | |
$subnetClient->setInitParameter($subnetId); | |
return array_shift($subnetClient->getReverseDomainRecords()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment