Last active
January 18, 2021 00:19
-
-
Save sleemanj/7406bf02a38963bac375696dbbbd1c36 to your computer and use it in GitHub Desktop.
Metaname (NZ Domain Registrar) API Script to allow something approaching ALIAS (aka ANAME) records to sort of set the Apex of a zone to be a CNAME.
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 | |
/** | |
* Go through all DNS Zones in your Metaname account and for any zone that have | |
* a CNAME record called "_apex_", resolve the cname to an IP address and | |
* update thea actual Apex A record (what you get when you resolve the domain | |
* without a host) with that IP address. | |
* | |
* So you can do like for your example.com zone | |
* | |
* _apex_ CNAME host1.myhostingservers.com. | |
* www CNAME _apex_ | |
* | |
* and (after running this) you can resolve example.com and will get the IP | |
* address of host1.myhostingservers.com (as it was when the script ran). | |
*/ | |
include('lib/my_metaname.php'); | |
$domains = $metaname->domain_names(); | |
foreach($domains as $domain) | |
{ | |
if(!$domain->uses_dns_hosting) continue; | |
if(strlen(@$domain->redirected_to)) continue; | |
echo $domain->name . "\n\n"; | |
$ApexAlias = NULL; | |
$Apex = NULL; | |
$AliasIP = NULL; | |
// Find the apex A record, and the _apex_ CNAME | |
// record if it exists | |
foreach($metaname->dns_zone($domain->name) as $rr) | |
{ | |
if($rr->type === 'CNAME' && $rr->name === '_apex_') | |
{ | |
$ApexAlias = $rr; | |
} | |
if($rr->type === 'A' && $rr->name === "{$domain->name}.") | |
{ | |
$Apex = $rr; | |
} | |
} | |
// If we have both then update the Apex with the Alias | |
if($ApexAlias && $Apex) | |
{ | |
// Allow relative alias, example... | |
// _alias_apex_ 3600 IN CNAME www | |
// www 3600 IN CNAME some.other.host.com. | |
// would then set the apex to be the IP address of some.other.host.com. | |
// (if it resolves, unchanged otherwise). | |
if(!preg_match('/\.$/', $ApexAlias->data)) | |
{ | |
$ApexAlias->data = $ApexAlias->data.'.'.$domain->name . '.'; | |
} | |
// Resolve alias | |
$AliasIP = gethostbyname($ApexAlias->data); | |
if(!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $AliasIP)) | |
{ | |
echo "Unable to resolve Alias {$ApexAlias->data}\n"; | |
continue; | |
} | |
else | |
{ | |
echo " Apex : {$Apex->name} {$Apex->data}\n"; | |
echo " Alias: {$ApexAlias->data} ({$AliasIP})\n"; | |
// Update if it needs to be | |
if($Apex->data !== $AliasIP) | |
{ | |
$NewApex = array( | |
'name' => $Apex->name, | |
'type' => $Apex->type, | |
'aux' => null, | |
'ttl' => $Apex->ttl, | |
'data' => $AliasIP | |
); | |
try | |
{ | |
$metaname->update_dns_record($domain->name, $Apex->reference, $NewApex); | |
echo " Updated\n"; | |
} | |
catch(JsonRpcFault $E) | |
{ | |
echo " Error Updating New Apex\n"; | |
print_r($NewApex); | |
echo "\n"; | |
switch($E->getCode()) | |
{ | |
case -4: echo " Invalid Domain\n"; break; | |
case -3: echo " Domain Not Found\n"; break; | |
case -12: echo " DNS Not Enabled\n"; break; | |
case -16: echo " Invalid Record\n"; break; | |
case -17: echo " Record Not Found\n"; break; | |
default: | |
{ | |
echo " {$E->getMessage()}\n"; | |
echo " Unknown Error {$E->getCode()}\n"; | |
break; | |
} | |
} | |
} | |
} | |
else | |
{ | |
echo " No Change\n"; | |
} | |
} | |
} | |
echo str_repeat('-', 80) ."\n\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment