ur

not able to capture returncode for ping

4 posts in this topic

#1 ·  Posted (edited)

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..

Edited by ur

Share this post


Link to post
Share on other sites



Thanks @jguinch,

I am using Ping() function now.

MsgBox(0,waitForUp("win10x64"),"")

Func waitForUp($systemname)
    for $iCount = 1 to 10
        if waitForSystem($systemname) Then
            return True
        Else
            $iCount = $iCount + 1
            Sleep(5000)
        EndIf
    Next
    MsgBox(0,"",$iCount)
    return False
EndFunc

Func waitForSystem($systemname)
    Local $iPing = Ping($systemname)
    if $iPing>0 then
        return True
    Else
        Return False
    EndIf
EndFunc

 

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

  • Similar Content

    • AlexFing17
      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

       
    • tk1
      By tk1
      Hello,
      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.
       
      Thanks,
      tk1
    • jguinch
      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  
    • MrBeatnik
      By MrBeatnik
      I have a rather strange issue on some machines.
      If I attempt to use the inbuilt PING function, no ICMP packet is sent out.
       
      Ping("internal.fqdn.com") is returning "Host Unreachable"
      If I ping using the command prompt, it's all fine.
       
      Using wireshark I can see that it hasn't even attempted an ICMP request. I can see it has looked up DNS to check for the correct IP address (I can see reply packet returns 10 different IP address as round robin - which is expected), but then it simply doesn't send the request.
       
      Any ideas?
    • MuffinMan
      By MuffinMan
      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") Exit EndIf $Site1 = $CmdLine[1] ;--> This should be your suspect addr $Site2 = $CmdLine[2] ;--> This should be your known good addr Logfile("STARTING PING SCAN") 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" Else $errstr = $Site1 & " failed to ping, " & $Site2 & " also failed to ping" EndIf Logfile($errstr) EndIf Sleep(1500) ;--> Added sleep function in case I was flooding, but now ping -t catches lots of drops that this app doesn't Wend 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") Exit EndIf $time = @MON & "-" & @MDAY & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC FileWrite($file, $time & " - " & $msg & @CRLF) FileClose($file) EndFunc