Sign in to follow this  
Followers 0
MattX

Ping Test

3 posts in this topic

I'm a bit stuck - I want this little prog to keep looping until I break out of it - I wanted to use a 'continueloop' but cannot work out if this is the best way or shall I use something else instead - some help would be appreicated.

The prog just keeps pinging an address, if it fails then it logs it, if not then I want it to keep going.....and going.....and going....

$var = Ping("10.0.0.4")

Dim $sLogPath = "c:\matt\log.txt"
Dim $sLogMsg = "Failed Ping at  "


If $var Then
    Else
    _FileWriteLog($sLogPath, $sLogMsg)
    Sleep(60000)
        
EndIf
Func _FileWriteLog($sLogPath, $sLogMsg)
    Local $sDateNow
    Local $sTimeNow
    Local $sMsg
    Local $hOpenFile
    Local $hWriteFile
    
    $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
    $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
    $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg
    
    $hOpenFile = FileOpen($sLogPath, 1)
    
    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    $hWriteFile = FileWriteLine($hOpenFile, $sMsg)
    
    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf
    
    FileClose($hOpenFile)
    Return 1
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

While Loop?

Dim $sLogPath = "c:\matt\log.txt"
Dim $sLogMsg = "Failed Ping at  "

While 1
   $var = Ping("10.0.0.4")
   If $var = 0 Then
      _FileWriteLog($sLogPath, $sLogMsg)
      Sleep(60000)
   EndIf
Wend

Func _FileWriteLog($sLogPath, $sLogMsg)
    Local $sDateNow
    Local $sTimeNow
    Local $sMsg
    Local $hOpenFile
    Local $hWriteFile
    
    $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
    $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
    $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg
    
    $hOpenFile = FileOpen($sLogPath, 1)
    
    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    $hWriteFile = FileWriteLine($hOpenFile, $sMsg)
    
    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf
    
    FileClose($hOpenFile)
    Return 1
EndFunc

I also fixed you If $var statement.

Edit: You might also want to include the nature of the error using @error to determine the failure.

When the function fails (returns 0) @error contains extended information:

1 = Host is offline

2 = Host is unreachable

3 = Bad destination

4 = Other errors

Edited by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

Christ that was quick !! Many thanks.

While Loop?

Dim $sLogPath = "c:\matt\log.txt"
Dim $sLogMsg = "Failed Ping at  "

While 1
   $var = Ping("10.0.0.4")
   If $var = 0 Then
      _FileWriteLog($sLogPath, $sLogMsg)
      Sleep(60000)
   EndIf
Wend

Func _FileWriteLog($sLogPath, $sLogMsg)
    Local $sDateNow
    Local $sTimeNow
    Local $sMsg
    Local $hOpenFile
    Local $hWriteFile
    
    $sDateNow = @YEAR & "-" & @MON & "-" & @MDAY
    $sTimeNow = @HOUR & ":" & @MIN & ":" & @SEC
    $sMsg = $sDateNow & " " & $sTimeNow & " : " & $sLogMsg
    
    $hOpenFile = FileOpen($sLogPath, 1)
    
    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    $hWriteFile = FileWriteLine($hOpenFile, $sMsg)
    
    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf
    
    FileClose($hOpenFile)
    Return 1
EndFunc

I also fixed you If $var statement.

<{POST_SNAPBACK}>

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  
Followers 0