Resolve IP Address to Host
Posted: 08/02/2005 13:37:06
Author: Andy Pilgrim
 ASP.Net 1.x Compatible (?)
Introduction
Every so often I seem to end up on Google searching for a DNS tools, especially when I change hosts and
want to check everything has changed over smoothly. My search often brings me to a complex page mixing many
DNS tools together. I would prefer one page per tool: one for forward DNS, one for reverse DNS and one for a
whois lookup. In this series of articles we examine how we may use ASP.Net to quickly and easily build these
three tools.
Resolving an IP Address to a Host Name
Also known as a Reverse DNS Lookup. In Part 1 we looked at converting a host name into an
IP address. In this article we look how to reverse the procedure. Every so often one stumbles upon an IP address, maybe in some IIS
log files, is it possible to determine what this address refers to? Yes - thats the topic of this article! Using ASP.Net it is possible to convert
an IP address into a Host Name.
The System.Net.Dns Namespace
The System.Net.Dns namespace provides domain name resolution functionality, and will provide the methods we
need to perform a Reverse DNS look up. First we must import the namespace into our page:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>
Importing the System.Net namespace also gives us the System.Net.Dns namespace, plus some added types which
we will need to handle the output. Now we are all set to write the rest of the page:
Designing A Page
We need a basic input page, basically consisting of a text box, submit button and somewhere to
display the output. I used an adapted version of the one we used in Part 1.
<html>
<head>
<title>Reverse DNS LookUp</title>
</head>
<body>
<form runat="server">
IP Addr: <asp:textbox runat="server" id="IP"/>
<asp:button runat="server" onclick="doLookUp"
text="look up"/>
</form>
<asp:label runat="server" id="output"/>
</body>
</html>
Next up the code:
Reverse DNS LookUp
It really couldn't be simpler. All we need is to call one function and display its output. Adding this
script tag to the page does the job quite nicely.
<script runat="server">
Sub doLookUp(sender As Object, e As System.EventArgs)
Dim HostInfo As IPHostEntry = Dns.GetHostByAddress(IP.Text)
Output.Text = HostInfo.HostName
End Sub
</script>
Now when an IP address is input, the system will return the corresponding host name. For example 127.0.0.1 returns 'localhost'.
Note: looking up your domain's IP address won't necessarily return your domain name, this will depend on your server
configuration. For example using 207.46.130.108 (microsoft.com) curiously returns blueyonderairlines.org, which redirects to the Microsoft
homepage!
Thats really all there is to it. A bit of validation wouldn't go a miss, and perhaps enclosing the lookup
in a try..end try block to catch errors should the host not be found. Of course the interface could use a
polish to! You can see an online working version on the code at http://nettools.semichaos.com/reversedns.aspx
Thats it! Next time we will look at a whois lookup tool.
|