VB2008 - DNS lookup from IP

Posted on 2009-12-17
Last Modified: 2012-05-08
Hi Guys,

I initially did this in VBS but then realised I can't run a VBS in the WinPE environment so need to make this an executable.

All of our machines IP's and Hostnames are prestaged based on their Mac address, so mac address 00:00:00:00:00:01 would be given IP and Machine name Computer_1 by default in DNS.

What I need to do is create a small app using VB that will look for the machines name in DNS, this will then allow me to change the answer file on the fly inserting the Machine name in the answer file making sure that all of my machines are named correctly bsed on their DNS entry.

Any help would be really cool thanks guys.

Question by:pnorris99
    LVL 1

    Author Comment

    Here is the VBS script I created and was going to use, but 2 problems 1 being no NSlookup in the PE env, and 2 you cant run VBS in the PE env.

    strcomputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery _
        ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
    For Each objitem In colitems
       strIPAddress = Join(objitem.IPAddress, ",")
       Exit For
    set objShell = createobject("")
    strParams = "%comspec% /c NSlookup " & strIPAddress
    Set objExecObj = objShell.exec(strParams)
    Do While Not objExecObj.StdOut.AtEndOfStream
    	strText = objExecObj.StdOut.Readline()
    	If instr(strText, "Server") then 
    		strServer = trim(replace(strText,"Server:",""))
    	Elseif instr (strText, "Name") Then
    		strhost = trim(replace(strText,"Name:",""))
    	End if
    NewString = Right(strhost,6)  
    If instr(strhost,".TEST.COM.AU") Then
    Artsstring = Replace(strhost, ".TEST.COM.AU", "")
    Const ForReading = 1
    Const ForWriting = 2
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("X:\sources\wdsunattend\wdsimageunattend.xml", ForReading)
    strText = objFile.ReadAll
    strNewText = Replace(strText, "$REPLACE$", Artsstring)
    Set objFile = objFSO.OpenTextFile("X:\sources\wdsunattend\wdsimageunattend.xml", ForWriting)
    objFile.WriteLine strNewText
    End If
    If instr(strhost,".TEST.COM.AU") Then
    Albanystring = Replace(strhost, ".TEST.COM.AU", "")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("X:\Sources\wdsunattend\WdsImageUnattend.xml", ForReading)
    strText = objFile.ReadAll
    strNewText = Replace(strText, "$REPLACE$", Albanystring)
    Set objFile = objFSO.OpenTextFile("X:\Sources\wdsunattend\WdsImageUnattend.xml", ForWriting)
    objFile.WriteLine strNewText
    End If

    Open in new window

    LVL 2

    Accepted Solution

    Hello Phil,

    add this API  for VB6
    Private Declare Function GetComputerName Lib "kernel32" _
    Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As _
    Long) As Long


    use th function for getting the machine name
    Public Function ComputerName() As String
      Dim sBuffer As String
      Dim lAns As Long
      sBuffer = Space$(255)
      lAns = GetComputerName(sBuffer, 255)
      If lAns <> 0 Then
            'read from beginning of string to null-terminator
            ComputerName = Left$(sBuffer, InStr(sBuffer, Chr(0)) - 1)
            Err.Raise Err.LastDllError, , _
              "A system call returned an error code of " _
               & Err.LastDllError
       End If

    End Function
    similarly u can use the GetHostByName  API to get the information  for DNS
     try it

    still confusion   TRY this site

    you will read example also for that
    LVL 1

    Author Closing Comment

    Thats perfect thanks for your time mate,


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now