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

VB2008 - DNS lookup from IP

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

Phil,
0
pnorris99
Asked:
pnorris99
  • 2
1 Solution
 
pnorris99Author Commented:
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
Next

set objShell = createobject("wscript.shell")

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
Loop

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
objFile.Close
strNewText = Replace(strText, "$REPLACE$", Artsstring)

Set objFile = objFSO.OpenTextFile("X:\sources\wdsunattend\wdsimageunattend.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close


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
objFile.Close
strNewText = Replace(strText, "$REPLACE$", Albanystring)

Set objFile = objFSO.OpenTextFile("X:\Sources\wdsunattend\WdsImageUnattend.xml", ForWriting)
objFile.WriteLine strNewText
objFile.Close

End If

Open in new window

0
 
kambleamarCommented:
Hello Phil,

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


and


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

http://allapi.mentalis.org/apilist/gethostname.shtml

you will read example also for that
0
 
pnorris99Author Commented:
Thats perfect thanks for your time mate,

P.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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