Jump to content
Sign in to follow this  

Logging dropped pings using AutoIT not matching DOS' ping -t

Recommended Posts

We seem to be having some connectivity problems with a hosted application vendor.  Doing a "ping -t" to the vendor's host reveals that we are dropping packets here and there.  I was asked if I could write a script that could run and log these dropped packets.

i put together the code below and initially it was logging a lot of dropped packets - too many really, so I put in Google.com and was still getting a lot of dropped packets.  I figured that maybe I was ping flooding and so I added in a Sleep between pings.  Now if I run my logger with a ping it running at the same time the command prompt is catching more drops than the app. The only requirement is to date/time stamp these dropped pings.  Does anyone have any suggestions on making AutoIT's ping function more like the Windows version?

If $cmdLine[0] <> 2 Then
    MsgBox(0, "Error", "Script must have 2 parameters to run - Suspect Address & Known Good Address")

$Site1 = $CmdLine[1] ;--> This should be your suspect addr
$Site2 = $CmdLine[2] ;--> This should be your known good addr


While 1

$var = Ping($Site1,600)
If @error <> 0 Then
    $var2 = Ping($Site2,600)
    If $var2 Then; also possible:  If @error = 0 Then ...
        $errstr = $Site1 & " failed to ping, " & $Site2 & " pinged in " & $var2 & " ms"
        $errstr = $Site1 & " failed to ping, " & $Site2 & " also failed to ping"


Sleep(1500) ;--> Added sleep function in case I was flooding, but now ping -t catches lots of drops that this app doesn't

Func LogFile($msg)
    $file = FileOpen($Site1 & "_log.txt", 1+8)

    ; Check if file opened for writing OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file. Script will close")

    $time = @MON & "-" & @MDAY & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
    FileWrite($file, $time & " - " & $msg & @CRLF)


Share this post

Link to post
Share on other sites

Thanks for the quick reply PainTain.  I'm not exactly sure how to do that, but wouldn't I be in the same boat with a single "DOS" ping?  I would still either be pinging too much or too little, right? And I really don't know how / if I could parse a ping -t.

Share this post

Link to post
Share on other sites

Thanks computergroove, I will read up on that.  I wouldn't be able to use that with "ping -t" and still get the desired results would I? (logging the date/time of each dropped ping)  I'm still interested on why using the DOS ping is any better than using AutoIT's ping and why I wouldn't be in the same boat using a single DOS' ping.

Share this post

Link to post
Share on other sites

You can do it directly with Autoit:

HotKeySet("{ESC}", "Esc")
MsgBox(0,"","Press ESC to stop pinging")
Global $check = 0

While $check = 0
    $iPing = Ping("www.google.com", 250)
    If $iPing Then
        ConsoleWrite("The roundtrip-time took: " & $iPing & "ms." & @CRLF)
        Switch @error
            Case 1
                ConsoleWrite("ERROR: Host is offline" & @CRLF)
            Case 2
                ConsoleWrite("ERROR: Host is unreachable" & @CRLF)
            Case 3
                ConsoleWrite("ERROR: Bad destination" & @CRLF)
            Case 4
                ConsoleWrite("ERROR: Other errors" & @CRLF)

Func Esc()
    $check = 1

Share this post

Link to post
Share on other sites

Thank you for the example,Wayfarer.  If you run this script for more than about 5 minutes, do you get a few errors ("Host is offline") when using google.com as the host?  I do, running this script both from work and home (on different ISPs).  But I have a ping -t pinging google.com in a command prompt that has been running all morning with no dropped pings whatsoever.

The roundtrip-time took: 6ms.
The roundtrip-time took: 6ms.
ERROR: Host is offline
The roundtrip-time took: 7ms.
ERROR: Host is offline
The roundtrip-time took: 6ms.
The roundtrip-time took: 7ms.

Share this post

Link to post
Share on other sites

j0kky is right, I agree with him

you could try to increment the "timeout" parameter.

Take note that the default timeout is 4000 ms, while you are using just 250 ms

this could cause false timeouts with consequence of "false" responses of offline status.

I think that if the network where you are working has not very good performances or if the pinged device is not within your LAN (maybe also because your packet must flow through some routers or proxies that may downgrade overall performances) then you should calibrate the timeout parameter by increasing it's value till you will get plausible results from the ping.

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post

Link to post
Share on other sites

Thanks j0kky & Chimp!  I think j0kky is right too - I made some adjustments and already I'm seeing results that are much more in line with what I know to be true.  Thanks to everyone for their suggestions.  I will mark this issue solved.

Share this post

Link to post
Share on other sites

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
Sign in to follow this  

  • Similar Content

    • By hcI
      Hello everyone !
      While writing a sample of code to test ping to my internet box, I got stuck..
      While the code is Pinging with let say.. 5000ms of timeout, I need to update a progress bar from 0 to 5000 ms at the same time but I don't know how could I do this..
      Is there a way or something that could get around the pause of the ping like in InetGet -> InetGetInfo?
    • By AlexFing17
      i want a script that
      "on the beginning of the program it pings to check if there is internet connectivity.
      if there is internet connectivity
      then it executes my code
      else if there is no internet it waits at background to detected internet connectivity then executes my code."
      Is this possible

    • By tk1
      Is there a way, preferably natively within AutoIt, to replicate:
      ping -i 2 <nearby_IP>
      The ping() function only has an option to set the timeout, but I want to set the TTL to 2 instead.
    • By ur
      I want to check whether machine is up or not after we poweron a VM.
      So to check that, I am using ping.
      With the below command, when we run it, it will capture the value success or not with the errorlevel.
      ping -n 1 sinra09-lod11 >nul: 2>nul: so we can track the return code back using below.
      echo %errorlevel% So it is able to return the 1 when it is failure and 0 when it is success.
      But when I convert the above commands to AutoIT, the return code is always giving 0 even on failure.
      MsgBox(0,pingCheck("sinra09-lod11"),"sinra09-lod11") Func pingCheck($system_name) ;$system_name = "sinra09-lod1" $command = @SystemDir&"\ping.exe -n 1 "& $system_name & " >nul: 2>nul:" ConsoleWrite($command) Local $iReturn = RunWait($command) ;MsgBox(0,$system_name,$iReturn) ConsoleWrite($iReturn) if $iReturn = 0 then Return True Else Return False EndIf EndFunc Please suggest..
    • By jguinch
      Here are 4 small function that I use for a tool in my job to launch several ping simultaneously. It also offers the possibility to run in background
       - _Multiping
       - _MultiPingCancel
       - _MultipingGetInfo
       - _MultiPingGetResult

      _Multiping requires the use of the #pragma compile(AutoItExecuteAllowed, True) directive.
      #pragma compile(AutoItExecuteAllowed, True) #include <ProcessConstants.au3> #include <WinAPI.au3> #include <WinAPIProc.au3> ; #EXAMPLE# ===================================================================================================================== #include <Array.au3> ConsoleWrite("Downloading hosts list, please wait...") Local $aDomains = StringRegExp(BinaryToString(InetRead("https://github.com/opendns/public-domain-lists/blob/master/opendns-random-domains.txt")), 'js-file-line">([^<]+)</td>', 3) Redim $aDomains[25] Local $iProgress = 0, $iCount = UBound($aDomains), $aMPInfos, $iPercent Local $aPings = _Multiping($aDomains, 1000, 5, 1) ProgressOn("PING HOSTS", "Please wait while _Multiping is running") While 1 $aMPInfos = _MultipingGetInfo(1) If Not $aMPInfos[0] Then ExitLoop $iPercent = Floor($aMPInfos[1]* 100 / $iCount) ProgressSet($iPercent, $aMPInfos[1] & " / " & $iCount & " done") Sleep(100) WEnd ProgressOff() MsgBox(0, "_Multiping example", "Result :" & @CRLF & $aMPInfos[2] & " hosts are online") Local $aRes = _MultiPingGetResult() _ArrayDisplay($aRes) ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Multiping ; Description ...: Pings a list of hosts using simultaneous pings to increase the speed. ; Syntax ........: _Multiping($aHosts[, $iTimeout = 4000[, $iMaxPing = 5[, $iBackground = 0]]]) ; Parameters ....: $aHosts - 1D array containing the list of the hosts to ping. ; $iTimeout - [optional] Time to wait for an answer in milliseconds (default is 4000). ; $iMaxPing - [optional] Maximum number of simultaneous ping to launch (defaut is 5). ; $iBackground - [optional] 0 - Waits until the ping process is conplete before continuing (defaut) ; 1 - Returns immediately and launch the ping process in the background (see remarks) ; Return values .: 1 on success, 0 on error and sets @error to a non-zero value. See remarks. ; Rermarks ......: Use _MultipingGetInfo to determine if the _Multiping process is finish. ; Use _MultiPingGetResult to get the result of the previous _Multiping call. ; Use _MultiPingCancel to cancel a _Multiping process running in background. ; The function requires the use of the #pragma compile(AutoItExecuteAllowed, True) directive. ; =============================================================================================================================== Func _Multiping($aHosts, $iTimeout = 4000, $iMaxPing = 5, $iBackground = 0) If Not IsDeclared("_iMultipingState") Then Global $g__aMPHosts, $g__iMPMaxPing, $g__iMPTimeout, $g__iMPState, $g__iMPFinished, $g__aMPRet, $g__iMPCount, $g__iMPCancel, $g__iMPCountOnline EndIf If $g__iMPState Then Return SetError(2, 0, 0) $g__aMPHosts = $aHosts $g__iMPMaxPing = $iMaxPing $g__iMPTimeout = $iTimeout $g__iMPCount = 0 $g__iMPState = 1 $g__iMPFinished = 0 $g__iMPCountOnline = 0 Local $aRet[UBound($aHosts)][2] $g__aMPRet = $aRet AdlibRegister("__MultipingAdlib", 100) If Not $iBackground Then While $g__iMPState Sleep(10) WEnd EndIf Return 1 EndFunc ; ==> _Multiping ; #FUNCTION# ==================================================================================================================== ; Name ..........: _MultiPingCancel ; Description ...: Cancels a _Multiping porocess running in backgorund. ; Syntax ........: _MultiPingCancel() ; Parameters ....: None ; Return values .: 1 on success, 0 on error. ; =============================================================================================================================== Func _MultiPingCancel() If Not IsDeclared("g__iMPCancel") Then Return 0 $g__iMPCancel = 1 Return 1 EndFunc ; ==> _MultiPingCancel ; #FUNCTION# ==================================================================================================================== ; Name ..........: _MultipingGetInfo ; Description ...: Returns information for the previous _Multiping call ; Syntax ........: _MultipingGetInfo([$iFlag = 0]) ; Parameters ....: $iFlag - [optional] argument that determines what the return value will be. See Return Value. ; Return values .: The return value depends of the $iFlag value : ; $iFlag = 0 (default) : 1 if the _Multiping process is running, or 0 if finish ; $iFlag = 1 : a 1D array containing the following informations : ; array[0] : 1 if the _Multiping process is running, or 0 if finish ; array[1] : number of hosts processed by _Multiping ; array[1] : number of online hosts processed by _Multiping ; Remarks .......: _MultipingGetInfo is useful to retrieve the progress of a _Multiping running in background ; =============================================================================================================================== Func _MultipingGetInfo($iFlag = 0) Local $iState = Execute("$g__iMPState") If Not $iFlag Then Return $iState Local $aRet = [$iState, Execute("$g__iMPFinished"), Execute("$g__iMPCountOnline") ] Return $aRet EndFunc ; ==> _MultipingGetInfo ; #FUNCTION# ==================================================================================================================== ; Name ..........: _MultiPingGetResult ; Description ...: Returns the result of the previous _Multiping call ; Syntax ........: _MultiPingGetResult([$iFlag = 0]) ; Parameters ....: $iFlag - [optional] Specifies whether to return pingable, not pinbable or all hosts. Default is 0. ; - 0 : returns all results ; - 1 : returns only pingable hosts ; - 2 : returns only not pingable hosts ; Return values .: ; On success, a 2D array containing results : ; array[0][0] : first hosts ; array[0][1] : result for the first host (see remarks) ; array[1][0] : second hosts ; array[1][1] : result for the second host ; On error, 0 and sets @error to a nonzero value. ; Remarks .......: A result can be a one of the following : ; - a positive value for the roundtrip-time in milliseconds. ; - one of theses negative values if the host is not pingable : ; -1 : Hosts is offline ; -2 : Host is unreachable ; -3 : Bad destination ; -4 : Other error ; - 0 if the ping process was cancelled before the host was pinged (only when using _MultiPing in background ; =============================================================================================================================== Func _MultiPingGetResult($iFlag = 0) Local $aRes = Execute("$g__aMPRet") If Not IsArray($aRes) Then Return SetError(1, 0, 0) If $iFlag <> 0 And $iFlag <> 1 And $iFlag <> 2 Then Return SetError(2, 0, 0) Local $aRet[UBound($aRes)][2], $n = 0 For $i = 0 To UBound($aRes) - 1 If $aRes[$i][1] = "" Or StringInStr($aRes[$i][1], "PING:") Then $aRes[$i][1] = 0 If $iFlag = 0 Or ($iFlag = 1 And $aRes[$i][1] > 0) Or ($iFlag = 2 And $aRes[$i][1] < 0) Then $aRet[$n][0] = $aRes[$i][0] $aRet[$n][1] = $aRes[$i][1] $n += 1 EndIf Next Redim $aRet[$n][2] Return $aRet EndFunc ; ==> _MultiPingGetResult ; #INTERNAL USE ONLY#============================================================================================================ Func __MultipingAdlib() Local $iPid, $iRet, $aResult Local Const $STILL_ACTIVE = 259 For $i = 0 To UBound($g__aMPRet) - 1 $g__aMPRet[$i][0] = $g__aMPHosts[$i] Next For $i = 0 To UBound($g__aMPRet) - 1 If $g__aMPRet[$i][1] = "" Then If $g__iMPCount < $g__iMPMaxPing Then If $g__aMPHosts[$i] = "" Then Return SetError(1, 0, 0) $iPid = Run(@AutoItExe & ' /AutoIt3ExecuteLine "Exit Ping(""' & $g__aMPHosts[$i] & '"", ' & $g__iMPTimeout & ') & @error"') $g__aMPRet[$i][1] = "PING:" & _WinAPI_OpenProcess($PROCESS_QUERY_LIMITED_INFORMATION, 0, $iPID) $g__iMPCount +=1 Else ExitLoop EndIf Else If StringInStr($g__aMPRet[$i][1], "PING:") Then $hProcess = StringReplace($g__aMPRet[$i][1], "PING:", "") $iRet = _WinAPI_GetExitCodeProcess($hProcess) If $iRet <> $STILL_ACTIVE Then $aResult = StringRegExp($iRet, "(\d*)(\d)$", 1) $g__aMPRet[$i][1] = ($aResult[0] ? $aResult[0] : (-1 * $aResult[1])) $g__iMPFinished += 1 If $g__aMPRet[$i][1] > 0 Then $g__iMPCountOnline += 1 $g__iMPCount -= 1 EndIf EndIf If $g__iMPCancel Then ExitLoop EndIf Next If $g__iMPFinished = UBound($g__aMPHosts) Or $g__iMPCancel Then AdlibUnRegister("__MultipingAdlib") $g__aMPHosts = 0 $g__iMPMaxPing = 0 $g__iMPTimeout = 0 $g__iMPState = 0 $g__iMPCancel = 0 EndIf EndFunc ; ==> __MultipingAdlib  
  • Create New...