2
votes

I need to get every CNAME record out from a single IP address input from our DNS server.
When I lookup:

[System.Net.Dns]::GetHostByAddress("81.95.243.81").Aliases

It only gives me the same 8 aliases in return:

botexshop.dk
bisamba.dk
nordsoenoceanarium.dk
www.brandingcommunity.com
botexhome.dk
botexudstyr.dk
botexjylland.dk
marineacademy.dk

but I know that the IP address has over 69 CNAME records (please look here: Toolbox | DNSstuff | Reverse DNS Lookup Results for 81.95.243.81 )

Why does the GetHostByAddress only return the same 8 aliases all the time? And how do I get all the CNAMEs ?

1
I know, but GetHostEntry doesn't return any aliases at all.. [System.Net.Dns]::GetHostEntry("81.95.243.81").Aliases returns nothing. So how can I benefit from your answer?user1281991
it's not an answer: it's clearly a comment.Mitch Wheat
Since I can't downwote comments, I downwoted the question - I believe it's wrong to make fun of someone trying to help you.Sandman4

1 Answers

0
votes

System.Net.Dns is quite lacking in many ways. I've seen some people go as far as writing full-blown DNS parsers to get what they need.

I know this doesn't fully answer your question, but this function seems to get the job done, however it's quite fragile and relies on nslookup so YMMV:

function get-dnsaliases($ip)
{
    $ip_rev = $ip -split '\.'
    [array]::reverse($ip_rev)
    $ip_rev = $ip_rev -join '.'
    $ptr_regex = "^`t" + [regex]::escape("$ip_rev.in-addr.arpa, type = PTR, class = IN")

    $responses = nslookup -d $ip

    $foundanswer = $null
    $aliases = @()

    foreach ($response in $responses)
    {
        if($foundanswer)
        {
            if($response -match "^`tname = (?<alias>.+)$")
            {
                $aliases += $Matches.alias
            }
        }
        elseif($response -match $ptr_regex)
        {
            $foundanswer = $true
        }
    }
    return $aliases
}