Jump to content

Ping Test


Recommended Posts

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
Link to comment
Share on other sites

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

qq

Link to comment
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}>

Link to comment
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
 Share

  • Recently Browsing   0 members

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