NSLookup from ASP.Net


Sunday, 5 August 2012

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.5 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:

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#

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.

Tags

Code, IT
Share with: 

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: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 Functionor in 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.


Support this Site



Popular Articles

What is Kutamo?
Kilimanjaro 2023
Kilimanjaro 2015
Kilimanjaro 2013
Australian Postcodes
New Zealand Postcodes
Worldwide City Database

Recent Articles

Favourite Links

Kutamo Studios
ErrLog.IO
Kutamo
AfterTheCloud
Kilimanjar 2023