Created
May 14, 2015 14:27
-
-
Save simplyadrian/68c7c54d373fe3b94fea to your computer and use it in GitHub Desktop.
Register private IP with DNS services. Supports DNSMadeEasy, DynDNS, Rackspace and Route53
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
# Powershell 2.0 | |
if($env:RS_REBOOT){exit 0} | |
Start-Sleep -s 60 | |
# Stop and fail script when a command fails. | |
$errorActionPreference = "Stop" | |
$ip = $env:VNS3_IP_ADDRESS | |
if ($ip -eq 'VNS3 IP') | |
{ | |
$ip = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1] | |
} | |
Write-Host "DNS Service is ${env:DNS_SERVICE}, IP is $ip" | |
if ($env:VNS3_IP_ADDRESS.StartsWith('10.')) | |
{ | |
Write-Warning "VNS3 and private IPs are switched: Re-run the DNS Register VNS3 IP right script when the VNS3 adapter starts." | |
} | |
switch ($env:DNS_SERVICE) | |
{ | |
'DNS Made Easy' | |
{ | |
$url = "https://www.dnsmadeeasy.com/servlet/updateip?username=$($env:DNS_USER)&password=$($env:DNS_PASSWORD)&id=$($env:DNS_ID)&ip=$ip" | |
$wc = New-Object net.WebClient | |
$response = $wc.downloadData($url) | |
$strResponse = [text.encoding]::ascii.getString($response) | |
$ok = ($strResponse.IndexOf("success") -ne -1) -or ($strResponse.IndexOf("error-record-ip-same") -ne -1) | |
if (-not $ok) | |
{ | |
Write-Host "ERROR: $strResponse" | |
exit 1 | |
} | |
} | |
'DynDNS' | |
{ | |
$user_pass = "$env:DNS_USER"+":"+"$env:DNS_PASSWORD" | |
$query="hostname=$env:VNS3_DOMAIN_NAME"+"&myip=$ip" | |
#$result = curl -u $user_pass -S -s -f -g "`"http://members.dyndns.org/nic/update?$query`"" | |
$result = curl -u $user_pass -S -g "`"http://members.dyndns.org/nic/update?$query`"" | |
#Both success and same ip are considered successful actions | |
if ($result.IndexOf("good") -ne -1) | |
{ | |
Write-Host ("Domain name $VNS3_DOMAIN_NAME was updated with $ip") | |
} | |
elseif ($result.IndexOf("nochg") -ne -1) | |
{ | |
Write-Host ("Operation completed. No update was done") | |
} | |
elseif ($result.IndexOf("badauth") -ne -1) | |
{ | |
Write-Host ("Operation failed. Incorrect credentials") | |
} | |
else | |
{ | |
Write-Host "ERROR: Operation failed with error: $result" | |
exit 1 | |
} | |
} | |
{ $_.StartsWith('Rackspace Cloud DNS')} | |
{ | |
Import-Module RaxHelpers | |
$region = $(if (($env:DNS_SERVICE).Contains('UK')) { 'uk' } else { 'us' }) | |
# Get Auth Token and Account Id from Rackspace | |
$authResponse = Get-AuthInfo -userName "$env:DNS_USER" -apiKey "$env:DNS_PASSWORD" -accountRegion "$region" | |
$accountId = $authResponse.Headers["X-Server-Management-Url"].Split('/')[-1] | |
$authToken = $authResponse.Headers["X-Auth-Token"] | |
#Using Rackspace API to chajnge ip address | |
$cdnsClient = New-Object RAXHelpers.CdnsClient | |
$cdnsClient.CdnsRegion = $region | |
$cdnsClient.AccountId = $accountId | |
$cdnsClient.AuthToken = $authToken | |
# Find my domain | |
$response = $cdnsClient.Do("GET","domains") | |
$responseXml = [RaxHelpers.Utils]::GetResponseContentXml($response) | |
$myDomainId = ($responseXml.domains.domain | where {$_.name -eq "$env:VNS3_DOMAIN_NAME"}).Id | |
if ($myDomainId -eq $null) | |
{ | |
Write-Host "ERROR: $domainName not found in cdns." | |
exit 1 | |
} | |
# Find records in my domain | |
$response = $cdnsClient.Do("GET","domains/$myDomainId") | |
$domainInfo = [RaxHelpers.Utils]::GetResponseContentXml($response) | |
$recordId = "A-"+$env:DNS_ID | |
$record = ($domainInfo.domain.recordsList.record | where {$_.id -eq $recordId}).id | |
if ($record -eq $null) | |
{ | |
Write-Host "ERROR: $record record was not found" | |
exit 1 | |
} | |
$updateDnsXml = "<?xml version=`"1.0`" encoding=`"UTF-8`" standalone=`"yes`"?><record id=`"$recordId`" data=`"$ip`" ttl=`"$env:DNS_TTL`" xmlns:ns2=`"http://docs.rackspacecloud.com/dns/api/management/v1.0`" xmlns=`"http://docs.rackspacecloud.com/dns/api/v1.0`" xmlns:ns3=`"http://www.w3.org/2005/Atom`"/>" | |
$response = $cdnsClient.Put("domains/$myDomainId/records/$recordId", $updateDnsXml) | |
$responseXml = [RaxHelpers.Utils]::GetResponseContentXml($response) | |
if ($response.StatusCode -eq "BadRequest") | |
{ | |
Write-Host "ERROR: Unable to update record: $($responseXml.badrequest)" | |
exit 1 | |
} | |
else | |
{ | |
$responseXml.asyncResponse.status | |
$responseXml.asyncResponse.request | |
} | |
} | |
'Route53' | |
{ | |
Import-Module Route53Tools | |
[Route53Helper.Authentication]::AwsAccessKeyId = "$env:DNS_USER" | |
[Route53Helper.Authentication]::AwsSecretAccessKey= "$env:DNS_PASSWORD" | |
Update-ARecord -domainName "$env:VNS3_DOMAIN_NAME"` | |
-type "A"` | |
-ttl "$env:DNS_TTL"` | |
-hostedZoneId "$env:DNS_ID"` | |
-comment "Changing ip address"` | |
-newIP "$ip" | |
} | |
default | |
{ | |
Write-Host "ERROR: Unsupported DNS provider: ${env:DNS_SERVICE}." | |
exit 101 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment