retrieve Hostname from IP address using VBA

StuartKStout

New Member
Joined
Mar 28, 2017
Messages
10
Hi

a Colleague wrote the below and has now left the company, I now need to expand the code so in cell.offset (0, 4) it will display the FQDN, is this possible?

The IPs start at Cell 'A2' and generally go down to row 254

Thanks in advance
Stuart
:confused:
Code:
Sub PingTest()


  Dim Cell As Range
  Dim colPings As Object, objPing As Object, strQuery As String
  Dim Rng As Range
  Dim RngEnd As Range
  Dim Wks As Worksheet
  
    Set Wks = ActiveSheet
    
    Set Rng = Wks.Range("A2")
    Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
    If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
    
    For Each Cell In Rng
    
      'Define the WMI query
       strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & Cell & "'"


      'Run the WMI query
       Set colPings = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)


      'Translate the query results to either True or False
       For Each objPing In colPings
         If Not objPing Is Nothing Then
            Cell.Offset(0, 1) = objPing.ProtocolAddress
            Cell.Offset(0, 2) = objPing.ResponseTime & " ms"
            Cell.Offset(0, 3) = GetPingStatus(objPing.StatusCode)
            'Cell.Offset(0, 4) = objPing.ResolveAddressName.Value
            
 Exit For
            
         End If
       Next objPing
     
     Next Cell
    
End Sub
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This is incorporating a routine found on Geekality:
Code:
Sub PingTest()    Dim Cell As Range
    Dim colPings As Object, objPing As Object, strQuery As String
    Dim Rng As Range
    Dim RngEnd As Range
    Dim Wks As Worksheet
    vUserProf = Environ("USERPROFILE")
    vDir = vUserProf & "\Downloads\"
    TmpFile = vDir & "TmpFile.txt"
    Dim oFSO As Object
    Dim oShell As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oShell = CreateObject("Wscript.Shell")
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1
    Set Wks = ActiveSheet
    Set Rng = Wks.Range("A2")
    Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
    If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
        For Each Cell In Rng
        'Define the WMI query
        strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & Cell & "'"
        'Run the WMI query
        Set colPings = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
        'Run CMD to obtain Domain Name
        vx = "cmd.exe /c nslookup " & Cell & " > " & TmpFile
        oShell.Run "cmd.exe /c nslookup " & Cell & " > " & TmpFile, windowStyle, waitOnReturn
        TmpTxt = ""
        Set oTempWk = oFSO.OpenTextFile(TmpFile, 1)
        Do While oTempWk.AtEndOfStream <> True
            vx = Trim(oTempWk.Readline)
            If "Name:" = Left(vx, 5) Then TmpTxt = ""
            TmpTxt = TmpTxt & vx
        Loop
        oTempWk.Close
        oFSO.DeleteFile (TmpFile)
        If TmpTxt = "" Or InStr(1, TmpTxt, "Name:", vbTextCompare) = 0 Then
            DomainName = "None found"
        Else
            ix1 = InStr(1, TmpTxt, "Name:", vbTextCompare) + 5
            ix2 = InStr(1, TmpTxt, "Address", vbTextCompare)
            DomainName = Trim(Mid(TmpTxt, ix1, ix2 - ix1))
        End If
        'Translate the query results to either True or False
        For Each objPing In colPings
            If Not objPing Is Nothing Then
            Cell.Offset(0, 1) = objPing.ProtocolAddress
            Cell.Offset(0, 2) = objPing.ResponseTime & " ms"
            If objPing.PrimaryAddressResolutionStatus > 0 Then
                Cell.Offset(0, 3) = GetPingStatus(objPing.PrimaryAddressResolutionStatus)
            Else
                Cell.Offset(0, 3) = GetPingStatus(objPing.StatusCode)
            End If
            Cell.Offset(0, 4) = DomainName
            'Cell.Offset(0, 4) = objPing.ResolveAddressName.Value
            Exit For
            End If
        Next objPing
    Next Cell
End Sub
Function GetPingStatus(StatCd As String)
    Select Case StatCd
    Case 0: strResult = "Connected"
    Case 11001: strResult = "Buffer too small"
    Case 11002: strResult = "Destination net unreachable"
    Case 11003: strResult = "Destination host unreachable"
    Case 11004: strResult = "Destination protocol unreachable"
    Case 11005: strResult = "Destination port unreachable"
    Case 11006: strResult = "No resources"
    Case 11007: strResult = "Bad option"
    Case 11008: strResult = "Hardware error"
    Case 11009: strResult = "Packet too big"
    Case 11010: strResult = "Request timed out"
    Case 11011: strResult = "Bad request"
    Case 11012: strResult = "Bad route"
    Case 11013: strResult = "Time-To-Live (TTL) expired transit"
    Case 11014: strResult = "Time-To-Live (TTL) expired reassembly"
    Case 11015: strResult = "Parameter problem"
    Case 11016: strResult = "Source quench"
    Case 11017: strResult = "Option too big"
    Case 11018: strResult = "Bad destination"
    Case 11032: strResult = "Negotiating IPSEC"
    Case 11050: strResult = "General failure"
    Case Else: strResult = "Unknown host"
    End Select
    GetPingStatus = strResult
End Function
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top