• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Nslookup in visual basic 2005

Hello experts,
I have to make nslookup command and get the ip address, I have the following code
elencoip = System.Net.Dns.GetHostAddresses(domainname): this works fine for the pc that executes the program, the System.Net.Dns.GetHostAddresses returns the result of nslookup command for the local pc, I have also to have the result of the nslookup command that comes from another pc that does not excecute the program for example remote pc, how can I do this?
For example if the pc that executes the program has ip address I have the result of nslookup command of pc 10.10.10,1, I have also to get the result of nslookup command from pc and
Thanks in advance to all.
  • 3
  • 2
1 Solution
Bob LearnedCommented:
I don't quite understand what you are hoping to get back from the "NSLOOKUP" operation.

aldog1Author Commented:
I would like if in visual basic 2005 I can change the ip address of the pc that executes nslookup command like in dos nslookup command where you can specify the ip address of the pc that will execute the nslookup command: for example if I want that nslookup is executed from pc I write nslookup www.pippo.com
I would like if I can this also in visual basic 2005 for example with System.Net.Dns.GetHostAddresses command, or with another command
Bob LearnedCommented:
I don't know of anything in the System.Net.Dns namespace that will let you indicate the server in order to look up a host entry.  You might want to just run the 'nslookup.exe' process, and get the output from the command, and parse the results.

aldog1Author Commented:
Is it possible call nslookup.exe in supensive mode from visual basic 2005?
If yes how?

Bob LearnedCommented:
>>supensive mode
What do you mean by that?  Are you talking about when you are at a break point in the IDE, and the code is paused?

Attached is a class that I started for NSLOOKUP parsing for MX lookups.

Imports System
Imports System.Diagnostics
Imports System.Text
Imports System.Text.RegularExpressions
Public Enum NsLookupType
End Enum
Public Class NsLookup
  ''' <summary> 
  ''' Set up NSLOOKUP to perform an MX lookup for the specified mail server. 
  ''' </summary> 
  ''' <param name="domainName">The mail server domain name.</param> 
  ''' <returns>The IP address for the server.</returns> 
  Public Shared Function PerformMxLookup(ByVal domainName As String) As String
    Return PerformLookup(NsLookupType.MX, domainName, "", "", -1, -1, _
    False, False, False)
  End Function
  ''' <summary> 
  ''' Set up NSLOOKUP to perform any of the allowable search types, with the various 
  ''' arguments that are pertinent to the lookup type desired. If any option is required, 
  ''' add a parameter, and determine how to add to the argument builder. 
  ''' </summary> 
  ''' <param name="type">Set query type (for example, A, ANY, CNAME, MX, 
  ''' NS, PTR, SOA, SRV)</param> 
  ''' <param name="domainName">Set default domain name to NAME.</param> 
  ''' <param name="rootServer">Set root server to NAME.</param> 
  ''' <param name="searchList">Set domain to N1 and search list to N1, N2, ... N6 (N1[/N2/.../N6].</param> 
  ''' <param name="timeoutSeconds">Set initial time-out interval to X seconds.</param> 
  ''' <param name="retryCount">Set number of retries to X.</param> 
  ''' <param name="recurse">Ask for recursive answer to query.</param> 
  ''' <param name="useDomainSearch">Use domain search list.</param> 
  ''' <param name="useFastZone">use MS fast zone transfer</param> 
  ''' <returns></returns> 
  Public Shared Function PerformLookup(ByVal type As NsLookupType, ByVal domainName As String, ByVal rootServer As String, ByVal searchList As String, ByVal timeoutSeconds As Integer, ByVal retryCount As Integer, _
  ByVal recurse As Boolean, ByVal useDomainSearch As Boolean, ByVal useFastZone As Boolean) As String
    Dim lookup As New Process()
    Dim argumentBuilder As New StringBuilder()
    argumentBuilder.AppendFormat(" -type={0}", type.ToString())
    If domainName.Trim().Length > 0 Then
      argumentBuilder.AppendFormat(" -domain={0}", domainName)
    End If
    If rootServer.Trim().Length > 0 Then
      argumentBuilder.AppendFormat(" -root={0}", rootServer)
    End If
    If searchList.Trim().Length > 0 Then
      argumentBuilder.AppendFormat(" -srchlist={0}", searchList)
    End If
    If timeoutSeconds <> -1 Then
      argumentBuilder.AppendFormat(" -timeout={0}", timeoutSeconds)
    End If
    If retryCount <> -1 Then
      argumentBuilder.AppendFormat(" -retry={0}", retryCount)
    End If
    If recurse Then
      argumentBuilder.Append(" recurse")
    End If
    If useDomainSearch Then
      argumentBuilder.Append(" search")
    End If
    If useFastZone Then
      argumentBuilder.Append(" msxfr")
    End If
    lookup.StartInfo.UseShellExecute = False
    lookup.StartInfo.RedirectStandardInput = True
    lookup.StartInfo.RedirectStandardOutput = True
    lookup.StartInfo.FileName = "nslookup"
    lookup.StartInfo.Arguments = argumentBuilder.ToString()
    lookup.StartInfo.CreateNoWindow = True
    lookup.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    Return ParseCommandOutput(type, lookup.StandardOutput.ReadToEnd())
  End Function
  ''' <summary> 
  ''' Determine which parser routine to call based on lookup type. 
  ''' </summary> 
  ''' <returns>The IP address found in the command output text.</returns> 
  Private Shared Function ParseCommandOutput(ByVal type As NsLookupType, ByVal input As String) As String
    Select Case type
      Case NsLookupType.MX
        Return ParseMxCommand(input)
      Case NsLookupType.A, NsLookupType.ANY, NsLookupType.CNAME, NsLookupType.NS, _
        NsLookupType.PTR, NsLookupType.SOA, NsLookupType.SRV
      Case Else
        Debug.WriteLine("Unknown type: " + type.ToString())
    End Select
    Return ""
  End Function
  ''' <summary> 
  ''' Parse the IP address for the mail server, using a regular expression. 
  ''' </summary> 
  ''' <param name="input">Command output text.</param> 
  ''' <returns>Mail server IP address.</returns> 
  Private Shared Function ParseMxCommand(ByVal input As String) As String
    Dim pattern As String = "mail exchanger = (?<server>[^\s]+)"
    Dim match As Match = Regex.Match(input, pattern)
    If match.Success Then
      Return match.Groups("server").Value
    End If
    Return ""
  End Function
End Class
'Commands: (identifiers are shown in uppercase, [] means optional) 
' NAME - print info about the host/domain NAME using default 
' server 
' NAME1 NAME2 - as above, but use NAME2 as server 
' help or ? - print info on common commands 
' set OPTION - set an option 
' all - print options, current server and host 
' [no]debug - print debugging information 
' [no]d2 - print exhaustive debugging information 
' [no]defname - append domain name to each query 
' [no]recurse - ask for recursive answer to query 
' [no]search - use domain search list 
' [no]vc - always use a virtual circuit 
' domain=NAME - set default domain name to NAME 
' srchlist=N1[/N2/.../N6] - set domain to N1 and search list to N1, N2, 
' and so on 
' root=NAME - set root server to NAME 
' retry=X - set number of retries to X 
' timeout=X - set initial time-out interval to X seconds 
' type=X - set query type (for example, A, ANY, CNAME, MX, 
' querytype=X - same as type 
' class=X - set query class (for example, IN (Internet), ANY) 
' [no]msxfr - use MS fast zone transfer 
' ixfrver=X - current version to use in IXFR transfer request 

Open in new window


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now