Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can resolve host names to an IP address?

Status
Not open for further replies.

Theo2k

Technical User
Dec 19, 2002
143
US
Hello everyone.

I'm working on a project with 22000 worksations and servers, and my goal is first to resolve all host names available to an ip addresses. Is there a script I can use to do that? Anything in excel or VB that I can just cut a paste all these host names and have them ping in order to give me the results needed?

Thanks for any help you may provide.

Theo
 
You can put your computer names in a spreadsheet or text file and read them off. You can find plenty examples of that here and in other forums. Look for FileSystemObject to read from a text file. There are a lot of examples from the scripting guys too.
Once you get the reading part done, then look at WMI and the Win32_PingStatus Which will let you ping and retrieve the IP address resolved from the HostName.

Put something together and post what you come up with if you run into any issues.

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
dm4ever thank you MUCH for your reply. I have a hard time understanding scripting. Is there something easier? I have all the host names in excel format and like to paste as shows on the example below click the ping all button and get their ip address. Sorry for being such a pain.

Here what I would like to do:

___________________________________________________________

(Click here to PING ALL)

Host Names IP address
WXZ1 ??????
WXZ2 ??????
WXZ3 ??????

 
'WMI options, though you need to find out your dns server,,which isnt that hard but..
Set objDNS = GetObject("winMgmts:\\.\root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
set objDNSZone = objDNS.Get("MicrosoftDNS_Zone.ContainerName=""" & _
strZone & """,DnsServerName=""" & _
objDNSServer.Name & """,Name=""" & strZone & """")

'ADO method
Set objCmd = CreateObject("ADODB.Command")


'*** get a recordset from active directory of the dns nodes
'*** setup ADODB Command Object, setup ADODB Connection object
strServer = "172.22.8.76"
Set objCon = CreateObject("ADODB.Connection")
objCon.Provider = "ADsDSOObject"
objCon.Open = "Active Directory Provider"
objCmd.ActiveConnection = objCon '*** Create connection
objCmd.Properties("Page Size") = 999 '*** set page size to allow for more than 1000 records
strQuery = "SELECT ADsPath, dc, canonicalName, distinguishedName, objectGUID, name FROM 'LDAP://" & strServer & "' WHERE objectClass = 'dnsZone'"
objCmd.commandtext = strQuery

Set objDNSRecords = objCmd.execute
While Not objDNSRecords.EOF
Wscript.Echo LCase(Trim(Cstr(objDNSRecords.Fields("distinguishedName").Value)))
objDNSRecords.MoveNext
Wend

'WMI ping or dreaded shell
Function LookUp(ByVal strPassed)
Dim objWMIService, colItems, objItem, strTemp, i
Dim WshShell, objExec, strNsLookupStdOut, strLine
strTemp = ""
If strPassed <> "" Then
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_PingStatus " & _
"Where Address = '" & strPassed & "'")
For Each objItem in colItems
strTemp = objItem.ProtocolAddress
Next
Else
strTemp = ""
End If
strTemp = ""
If strTemp <> "" Then
Lookup = strTemp
Else
'i hate myself for doing this, i am a sell out, i have no pride
Set WshShell = CreateObject("WScript.Shell")
Set objExec = WshShell.Exec("nslookup " & strPassed)
'strNsLookupStdOut = objScriptExec.StdOut.ReadAll
Do Until objExec.StdOut.AtEndOfStream
strLine = objExec.StdOut.ReadLine
'Wscript.Echo strLine
If LCase(Left(strLine, Len("name:"))) = "name:" Then
If InStr(strLine, strPassed) Then
Wscript.Echo strLine
strLine = Trim(objExec.StdOut.ReadLine)
Wscript.Echo " & the next one is " & strLine
If LCase(Left(strLine, Len("address:"))) = "address:" Then
For i = 1 To Len(strLine)
If IsNumeric(Mid(strLine, i, 1)) Then
Lookup = Right(strLine, Len(strLine) - i + 1)
Exit For
End If
Next
End If
Exit Do
End If
End If
Loop
'WScript.Echo strNsLookupStdOut
Set objExec = Nothing
Set WshShell = Nothing
End If

End Function
 
mrmovie's comment "i hate myself for doing this, i am a sell out, i have no pride" - i thought it was funny putting that in there. However, it is a good example of reading the output from the command line and getting what you want from it; at least for those willing to put forth some time and effort to see what's being done.

Along the lines of what Theo2k is asking of reading an excel file and getting the IP address, here's an example.

Option Explicit
'On Error Resume Next

Dim objExcel, objWorkbook, i

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Temp\HostName.xls") 'excel file location
objExcel.Visible = True

i = 1 'row to begin with that contains computer name

Do Until objExcel.Cells(i, 1).Value = ""
objExcel.Cells(i, 2).Value = ResolveIP(objExcel.Cells(i, 1).Value) 'inserts the resolved ip address in the 2nd column
i = i + 1
Loop

objExcel.Quit

Function ResolveIP(strHostName)
'On Error Resume Next

Dim wmiQuery, objWMIService, objPing, objStatus

wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strHostName & "'"

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)

For Each objStatus In objPing
If objStatus.StatusCode = 0 Then
ResolveIP = objStatus.ProtocolAddress
Else
ResolveIP = "Unreachable"
End If
Next
End Function

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
You guys are great! It works! I cant thank you enough!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top