Click here to Skip to main content
15,894,106 members
Articles / Web Development / ASP.NET

NSLookup from ASP.NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Jan 2015CPOL1 min read 9.2K   1  
NSLookup from ASP.NET

Today, I found myself writing code to review the validity of the many, err, dozens of domain names that I oversee. I wanted to incorporate this into my personal intranet site, written in ASP.NET using the 4.5RC framework.

The task was to validate whether a domain name was correctly delegated, with valid A records pointing at one or more of my web servers, or externally hosted servers.

Using the system.net assembly to query a domain record is relatively trivial, but using it to query external name server delegation proved to be somewhat cumbersome, so I decided to simply use Window’s built in nslookup tool, available since Windows 2000 (and maybe earlier!).

Calling an executable from within an IIS thread is always risky, but since it’s only myself using it, a quick hack is more than adequate.

So I wrote a little function (well, expanded on a fairly popular extract), that simply calls nslookup and returns the full text of the results. This text can then be parsed to retrieve name server and other details, depending on what you are after.

In VB:

VB.NET
Function RunCmd(ParamArray commands As String()) As String
        Dim returnvalue As String = String.Empty

        Dim info As New ProcessStartInfo("cmd")
        info.UseShellExecute = False
        info.RedirectStandardInput = True
        info.RedirectStandardOutput = True
        info.CreateNoWindow = True

        Using process__1 As Process = Process.Start(info)
            Dim sw As StreamWriter = process__1.StandardInput
            Dim sr As StreamReader = process__1.StandardOutput

            For Each command As String In commands
                sw.WriteLine(command)
            Next

            sw.Close()
            returnvalue = sr.ReadToEnd()
        End Using

        Return returnvalue
    End Function

or in C#:

C#
public string RunCmd(params string[] commands) {
  string returnvalue = string.Empty;

  ProcessStartInfo info = new ProcessStartInfo("cmd");
  info.UseShellExecute = false;
  info.RedirectStandardInput = true;
  info.RedirectStandardOutput = true;
  info.CreateNoWindow = true;

  using (Process process__1 = Process.Start(info)) {
    StreamWriter sw = process__1.StandardInput;
    StreamReader sr = process__1.StandardOutput;

    foreach (string command in commands) {
      sw.WriteLine(command);
    }

    sw.Close();
    returnvalue = sr.ReadToEnd();
  }
  
return returnvalue;
}

Calling the function is as simple as this:

  • VB - Dim results As String = RunCmd(“google.com.au 8.8.8.8″)
  • C# - string results = RunCmd(“google.com.au 8.8.8.8″);

The first component is the domain name, the second is the name server you wish to query.

Of course, these functions are just generic shell execution wrappers, so can be used for any query or command you want to run, server-side.

Use this as you will – there is no validation, not at all elegant, but works fine for me. :-) If it burns down your house and turns your code into mush, then don’t blame me.

This article was originally posted at http://www.matthewproctor.com/nslookup-from-asp-net

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Kutamo and ErrLog.IO
Australia Australia
Pluralsight Author, .Net Developer, Writer & Blogger

http://www.kutamo.com/
http://www.errlog.io/
http://www.matthewproctor.com/
http://www.pluralsight.com/author/matthew-proctor

I am an Australian IT professional based in Melbourne, Victoria, Australia.

I'm a polyglot programmer, being proficient in a number of languages and frameworks including C#, VB.Net, F#, Javascript, Perl, Powershell and Z80 & 6502 assembly. Smile | :)

Comments and Discussions

 
-- There are no messages in this forum --