Resolve Host to IP Address

Posted: 03/02/2005 12:48:31
Author: Andy Pilgrim
Is ASP.Net 1.x Compatible? Yes! 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 a Host (Domain Name) to an IP Address

    This article looks at how to use ASP.Net to find the IP address of a given host. A host could be a machine on a local network, or a domain name such as google.com. But why would we want to do this? I find it of particular use when I change hosting providers. By using DNS lookup tools located in different geographical areas you can check the progress of the propagation of alterations to nameservers. For instance, when changing hosts you have to change your domains IP address, making it point to you new hosting provider. By performing DNS lookups of different networks you can see where users are being directed, to the old host or the new one. When all of the DNS lookups point to the new host, you can shut off your old one.

    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 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

    First off we need a basic input page, basically consisting of a text box, submit button and somewhere to display the output. I used the following:

    <html>
    <head>
    <title>DNS LookUp</title>
    </head>
    <body>
     <form runat="server">
      Host: <asp:textbox runat="server" id="Host"/>
      <asp:button runat="server" onclick="doLookUp"
        text="look up"/>
     </form>
     <asp:label runat="server" id="output"/>
    </body>
    </html>

    Next up the code:

    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.GetHostByName(Host.Text)
      Output.Text = HostInfo.AddressList(0).ToString()
    End Sub
    </script>

    So when a host name (microsoft.com for example) is entered and the button clicked, the function is called and performs a DNS Lookup. It then returns to screen using an output label the IP address of the host - job done!

    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/forwarddns.aspx

    Thats it! Next time we will look at a reverse DNS lookup tool