Create a console app and add a reference to System.Net and System.Net.Sockets. Declare a Main method that looks similar to the one shown below:
Now let us add the GetIpAddress() and GetDomainName() static methods:
// Get Ip Address from DomainName static void GetIPAddress(string hostname) { IPAddress[] ipAddr = Dns.GetHostAddresses(hostname); Console.WriteLine('GetHostAddresses({0}) :', hostname); foreach (IPAddress ip in ipAddr) { Console.WriteLine(ip); } } // Get HostName from IPAddress static void GetDomainName(string ipaddress) { IPHostEntry hostEntry = Dns.GetHostEntry(ipaddress); Console.WriteLine('DNS name : ' + hostEntry.HostName); //Aliases if any String[] aliases = hostEntry.Aliases; for (int i = 0; i < aliases.Length; i++) { Console.WriteLine('Alias: ' + aliases[i]); } }
The Dns.GetHostAddresses method returns the Internet Protocol (IP) addresses for the specified host. Similarly Dns.GetHostEntry method resolves a host name or IP address to an IPHostEntry instance. When an IPHostEntry instance is returned, it has the properties set – like the AddressList, Aliases, and HostName. In the example shown above, we are accessing the HostName property
OUTPUT
Note: Reverse DNS or looking up for a DomainName given an IPAddress may not always fetch the results you expect. The process of reverse resolving an IP address uses the Pointer DNS record type (PTR record). However in cases of a web farm, the hostnames are very generic. Sometimes the PTR record does not even exist. It is also possible that a stale DNS record for an IPv4 address belonging to a different host can be returned. Read more here
In case of IPv6, most IPv6 do not register the PTR record for an IPv6 address.
"
No comments:
Post a Comment