Created
May 14, 2015 14:25
-
-
Save simplyadrian/79bde7b15cbdd0a7b933 to your computer and use it in GitHub Desktop.
Register public 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 | |
# Stop and fail script when a command fails. | |
$errorActionPreference = "Stop" | |
### Declare inputs | |
#$env:DNS_ID | |
#$env:DNS_IP_ADDRESS | |
#$env:DNS_DOMAIN_NAME | |
#$env:DNS_USER | |
#$env:DNS_PASSWORD | |
#$env:DNS_SERVICE | |
#$env:DNS_TTL | |
# Temporary workaround for SoftLayer switched IPs issue | |
if ($env:RS_CLOUD_PROVIDER -eq 'softlayer') | |
{ | |
if ($env:RS_PUBLIC_IP.StartsWith('10.')) | |
{ | |
Write-Warning "Public and private IPs are switched: RS_PUBLIC_IP=$($env:RS_PUBLIC_IP), RS_PRIVATE_IP=$($env:RS_PRIVATE_IP)" | |
Write-Verbose "Resetting env variables..." | |
$privateIP = $env:RS_PUBLIC_IP | |
$publicIP = $env:RS_PRIVATE_IP | |
[Environment]::SetEnvironmentVariable("RS_PUBLIC_IP", $publicIP, "Process") | |
[Environment]::SetEnvironmentVariable("RS_PRIVATE_IP", $privateIP, "Process") | |
Write-Verbose "Env variables reset: RS_PUBLIC_IP=$($env:RS_PUBLIC_IP), RS_PRIVATE_IP=$($env:RS_PUBLIC_IP)" | |
} | |
} | |
$ip = $env:DNS_IP_ADDRESS | |
if ($ip -eq 'Public IP') | |
{ | |
$ip = $env:RS_PUBLIC_IP | |
} | |
elseif ($ip -eq 'Private IP') | |
{ | |
$ip = $env:RS_PRIVATE_IP | |
} | |
Write-Host "DNS Service is ${env:DNS_SERVICE}, IP is $ip" | |
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:DNS_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 $DNS_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:DNS_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:DNS_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