Jump to content

Ping via WMI


weaponx
 Share

Recommended Posts

$IP = "www.google.com"
$result = WMI_Ping($IP,32,false,0,true)
If NOT @ERROR Then
    MsgBox(0, "SUCCESS", $result & " milliseconds")
Else
    MsgBox(0, "", "FAIL")
EndIf

#cs --------------------------------------------------------------------------------------------------------
Name: WMI_Ping
    
Description: Ping a remote computer via WMI
    
Author: WeaponX
    
Parameters: (6 - Str,Int,Bool,Int,Bool,Str)
    Address (String) -  Hostname, IPv4 address, or IPv6 address (Vista only)
    BufferSize (Integer) -  Buffer size sent with the ping command. The default value is 32.
    NoFragmentation (Boolean) - If TRUE, "Do not Fragment" is marked on the packets sent. The default is FALSE, not fragmented.
    RecordRoute (Integer) - How many hops should be recorded while the packet is in route. The default is 0 (zero).
    ResolveAddressNames (Boolean) - Command resolves address names of output address values. The default is FALSE, which indicates no resolution.
    SourceRoute (String) - Comma-separated list of valid Source Routes. The default is "".
    
Return: (Int)
    Success - Response time in milliseconds
    Failure - Status code and @ERROR = 1
    Failure creating object - 0 and @ERROR = 2
#ce  --------------------------------------------------------------------------------------------------------
Func WMI_Ping($sAddress = "127.0.0.1", $iBufferSize = 32, $bNoFragmentation = False, $iRecordRoute = 0, $bResolveAddressNames = False, $sSourceRoute = "")
    Local $colItems = "", $strComputer = "localhost", $strStatusCode = 0, $strResponseTime = 0, $Output = ""
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    Local $sQuery = "SELECT * FROM Win32_PingStatus WHERE "
    $sQuery &= "address = '" & $sAddress & "' "
    $sQuery &= "AND BufferSize = " & $iBufferSize & " "
    $sQuery &= "AND NoFragmentation = " & $bNoFragmentation & " "
    $sQuery &= "AND RecordRoute = " & $iRecordRoute & " "
    $sQuery &= "AND ResolveAddressNames = " & $bResolveAddressNames & " "
    $sQuery &= "AND SourceRoute = '" & $sSourceRoute & "'"
    
    $colItems = $objWMIService.ExecQuery($sQuery, "WQL", 0x10 + 0x20)

    If IsObj($colItems) Then
        ConsoleWrite("Object Name: " & ObjName($colItems) & @CRLF)
        For $objItem In $colItems
            $Output &= "Address: " & $objItem.Address & @CRLF
            $Output &= "BufferSize: " & $objItem.BufferSize & @CRLF
            $Output &= "NoFragmentation: " & $objItem.NoFragmentation & @CRLF
            $Output &= "PrimaryAddressResolutionStatus: " & $objItem.PrimaryAddressResolutionStatus & @CRLF
            $Output &= "ProtocolAddress: " & $objItem.ProtocolAddress & @CRLF
            $Output &= "ProtocolAddressResolved: " & $objItem.ProtocolAddressResolved & @CRLF
            $Output &= "RecordRoute: " & $objItem.RecordRoute & @CRLF
            $Output &= "ReplyInconsistency: " & $objItem.ReplyInconsistency & @CRLF
            $Output &= "ReplySize: " & $objItem.ReplySize & @CRLF
            $Output &= "ResolveAddressNames: " & $objItem.ResolveAddressNames & @CRLF
            $strResponseTime = $objItem.ResponseTime
            $Output &= "ResponseTime: " & $strResponseTime & @CRLF
            $Output &= "ResponseTimeToLive: " & $objItem.ResponseTimeToLive & @CRLF
            $strRouteRecord = $objItem.RouteRecord(0)
            $Output &= "RouteRecord: " & $strRouteRecord & @CRLF
            $strRouteRecordResolved = $objItem.RouteRecordResolved(0)
            $Output &= "RouteRecordResolved: " & $strRouteRecordResolved & @CRLF
            $Output &= "SourceRoute: " & $objItem.SourceRoute & @CRLF
            $Output &= "SourceRouteType: " & $objItem.SourceRouteType & @CRLF
            $strStatusCode = $objItem.StatusCode
            $Output &= "StatusCode: " & $strStatusCode & @CRLF
            $Output &= "Timeout: " & $objItem.Timeout & @CRLF
            $strTimeStampRecord = $objItem.TimeStampRecord(0)
            $Output &= "TimeStampRecord: " & $strTimeStampRecord & @CRLF
            $strTimeStampRecordAddress = $objItem.TimeStampRecordAddress(0)
            $Output &= "TimeStampRecordAddress: " & $strTimeStampRecordAddress & @CRLF
            $strTimeStampRecordAddressResolved = $objItem.TimeStampRecordAddressResolved(0)
            $Output &= "TimeStampRecordAddressResolved: " & $strTimeStampRecordAddressResolved & @CRLF
            $Output &= "TimestampRoute: " & $objItem.TimestampRoute & @CRLF
            $Output &= "TimeToLive: " & $objItem.TimeToLive & @CRLF
            $Output &= "TypeofService: " & $objItem.TypeofService & @CRLF

        Next
        
        ;Uncomment to see full results
        ;ConsoleWrite($Output & @CRLF)
        
        If $strStatusCode <> 0 Then
            Return SetError(1,0,$strStatusCode)
        Else
            Return $strResponseTime
        EndIf       
    Else
        Return SetError(2,0,0)
    EndIf

EndFunc   ;==>WMI_Ping

Derived from MSDN:

http://msdn.microsoft.com/en-us/library/aa394350.aspx

Link to comment
Share on other sites

why don't you put it all in an array? like this:

$IP = "www.google.com"
$result = WMI_Ping($IP,32,false,0,true)
If NOT @ERROR Then
    MsgBox(0, "SUCCESS", $result[0] & " milliseconds")
Else
    MsgBox(0, "", "FAIL")
EndIf

#cs --------------------------------------------------------------------------------------------------------
Name: WMI_Ping
   
Description: Ping a remote computer via WMI
   
Author: WeaponX
   
Parameters: (6 - Str,Int,Bool,Int,Bool,Str)
    Address (String) -  Hostname, IPv4 address, or IPv6 address (Vista only)
    BufferSize (Integer) -  Buffer size sent with the ping command. The default value is 32.
    NoFragmentation (Boolean) - If TRUE, "Do not Fragment" is marked on the packets sent. The default is FALSE, not fragmented.
    RecordRoute (Integer) - How many hops should be recorded while the packet is in route. The default is 0 (zero).
    ResolveAddressNames (Boolean) - Command resolves address names of output address values. The default is FALSE, which indicates no resolution.
    SourceRoute (String) - Comma-separated list of valid Source Routes. The default is "".
   
Return: (Int)
    Success - Response time in milliseconds
    Failure - Status code and @ERROR = 1
    Failure creating object - 0 and @ERROR = 2
#ce  --------------------------------------------------------------------------------------------------------
Func WMI_Ping($sAddress = "127.0.0.1", $iBufferSize = 32, $bNoFragmentation = False, $iRecordRoute = 0, $bResolveAddressNames = False, $sSourceRoute = "")
    Local $colItems = "", $strComputer = "localhost", $strStatusCode = 0, $strResponseTime = 0
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    Local $sQuery = "SELECT * FROM Win32_PingStatus WHERE "
    $sQuery &= "address = '" & $sAddress & "' "
    $sQuery &= "AND BufferSize = " & $iBufferSize & " "
    $sQuery &= "AND NoFragmentation = " & $bNoFragmentation & " "
    $sQuery &= "AND RecordRoute = " & $iRecordRoute & " "
    $sQuery &= "AND ResolveAddressNames = " & $bResolveAddressNames & " "
    $sQuery &= "AND SourceRoute = '" & $sSourceRoute & "'"
   
    $colItems = $objWMIService.ExecQuery($sQuery, "WQL", 0x10 + 0x20)
    global $Output[24]
    If IsObj($colItems) Then
       $Output[23] = ObjName($colItems)
        For $objItem In $colItems
            $Output[1] = $objItem.Address
            $Output[2] = $objItem.BufferSize
            $Output[3] = $objItem.NoFragmentation
            $Output[4] = $objItem.PrimaryAddressResolutionStatus
            $Output[5] = $objItem.ProtocolAddress
            $Output[6] = $objItem.ProtocolAddressResolved
            $Output[7] = $objItem.RecordRoute
            $Output[8] = $objItem.ReplyInconsistency
            $Output[9] = $objItem.ReplySize
            $Output[10] = $objItem.ResolveAddressNames
            $Output[0] = $objItem.ResponseTime
            $Output[11] = $objItem.RouteRecord(0)
            $Output[12] = $objItem.RouteRecordResolved(0)
            $Output[13] = $objItem.SourceRoute
            $Output[14] = $objItem.SourceRouteType
            $Output[15] = $objItem.StatusCode
            $Output[16] = $objItem.Timeout
            $Output[17] = $objItem.TimeStampRecord(0)
            $Output[18] = $objItem.TimeStampRecordAddress(0)
            $Output[19] = $objItem.TimeStampRecordAddressResolved(0)
            $Output[20]&= $objItem.TimestampRoute
            $Output[21] &= $objItem.TimeToLive
            $Output[22] &= $objItem.TypeofService
        Next
       
        If $strStatusCode <> 0 Then
            Return SetError(1,0,$strStatusCode)
        Else
            Return $Output
        EndIf      
    Else
        Return SetError(2,0,0)
    EndIf

EndFunc   ;==>WMI_Ping
Link to comment
Share on other sites

That is a good idea, although I tend to avoid returning arrays where there are "keys" involved. Perhaps a scripting dictionary object would be more useful as the return type.

Link to comment
Share on other sites

  • 8 months later...

Has anyone found a way around this example and the use of 'Ping' returning a false positive if an IP address doesn't exist? For example, if you unplug the network cable and disable wireless and run the code it always returns a 'Success'. This code is great if the network cable is plugged in, but what if are trying to verify that the cable is plugged in by performing a PING of an address? Is there a better way than this?

Link to comment
Share on other sites

  • 4 weeks later...

this seems very nice.

but i get false "success" reports with ' "" milliseconds' (blank). needs more precise error handling, i guess.

but - very nice !

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...