GetIPInfo.vbs

'=========================================================================
' GetIPInfo.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: [email protected]
' WEB: https://sigkillit.com
' DATE: 1/1/2011
' COMMENTS: Input a Computer DNS/IP, which then Clears the Current Arp Cache,
' Gets the IP of the DNS/IP, and then gets the MAC address of the DNS/IP.
' EXAMPLE: Wscript.Echo GetIPInfo("Computer")
'=========================================================================
Option Explicit
Wscript.Echo GetIPInfo("Computer")

Private Function GetIPInfo(strComputerOrIP)

    On Error Resume Next

    Dim strResult: strResult = ""
    Call ClearARP()
    ' Get IP of Host Name
    Dim strIP: strIP = GetIP(strComputerOrIP)
    ' Get MAC of Host Name
    Dim strArp: strArp = GetMAC(strIP)

    GetIPInfo = strComputerOrIP & vbTab & strIP & vbTab & strArp

    On Error Goto 0

End Function

' **************************************************************************************************
' Sub ClearARP - Clears the ARP Cache
' **************************************************************************************************
Private Sub ClearARP()

        On Error Resume Next

        ' Clear ARP Cache
       Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
        objShell.Run("netsh interface ip delete arpcache")

        ' Kill Objects
        Set objShell = Nothing

        On Error Goto 0

End Sub

' **************************************************************************************************
' Function GetIP - Returns an IP From Passing an Host Name or IP Into the Function
' ********************************************************************
Private Function GetIP(strHost)

    On Error Resume Next

    ' Ping Host and Extract IP Address
    Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
    Dim objPing: Set objPing = objShell.Exec("%comspec% /c For /f " & chr(34) & "tokens=2 delims=[]" & chr(34) & " %A In ('Ping -a -n 1 " & strHost & "') Do %A")
    Dim strPingResults: strPingResults = objPing.StdOut.ReadAll

    ' Search For an IP Address from Results
    Dim RegEx: Set RegEx = New RegExp
    RegEx.IgnoreCase = True
    RegEx.Global = True
    RegEx.Pattern = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
    If Regex.Test(strPingResults) = True then
        Dim Matches: Set Matches = RegEx.Execute(strPingResults)
        Dim strMatch
        For Each strMatch in Matches
            ' Return Result
            GetIP = strMatch
        Next
    Else
        GetIP = "!~ERROR~!"
    End If

    ' Kill Objects
    Set objShell = Nothing

    On Error Goto 0

End Function

' **************************************************************************************************
' Function GetMAC - Returns a MAC Address From Passing an IP Into the Function
' ********************************************************************
Private Function GetMAC(strIP)

        On Error Resume Next

        ' Check ARP Table and Results
        Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
        Dim objArp: Set objArp = objShell.Exec("arp -a " & strIP)
        Dim strArpResult: strArpResult = objArp.StdOut.ReadAll

        ' Search For a MAC Address from Results
       Dim RegEx: Set RegEx = New RegExp
        RegEx.IgnoreCase = True
        RegEx.Global = True
        RegEx.Pattern = "[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}"
        If Regex.Test(strArpResult) = True then
                Dim Matches: Set Matches = RegEx.Execute(strArpResult)
                Dim strMatch
                For Each strMatch in Matches
                    GetMAC = strMatch
                Next
        Else
            GetMAC = "!~ERROR~!"
        End if

        ' Kill Objects
        Set objShell = Nothing

        On Error Goto 0

End Function