Sign in to follow this  
Followers 0
Mingre

[Resolved] How to put ping() or similar alternative function a timeout

2 posts in this topic

#1 ·  Posted (edited)

Hello AutoIt community,

Is there a workaround for including timeout for these functions? They work great when the networked computers are actually online, but really slow if otherwise. I did a simple test (see below) and learned that there's a function faster (at least for my purpose) than ping(). Credits to guinness for _WinAPI_PathFileExists() (you're the one who posted this code, right?).

This also talked about the same problem though it ended with martin's suggesting his UDF. I tried to understand it but unfortunately can't.

There's lot of threads relevant to my question but I *sincerely* deemed their discussion too complicated for my level. Please help. Thanks!

_testAPI('\\G11\d')
_testPing('G11')

_testAPI('\\G12\d')
_testPing('G12')

ConsoleWrite('+++++++++++++++++++++++++++++++++++' & @CRLF)
Sleep(2000)

_testPing('G11')
_testAPI('\\G11\d')

_testPing('G12')
_testAPI('\\G12\d')


Func _testAPI($NetworkComputer)
Local $TimeStarts = TimerInit()
Local $TestOnline = _WinAPI_PathFileExists($NetworkComputer)
ConsoleWrite('API - Online? ' & $TestOnline & ' at ' & TimerDiff($TimeStarts) & 'ms' & @CRLF)
EndFunc ;==>_test

Func _testPing($NetworkComputer)
Local $TimeStarts = TimerInit()
Local $TestOnline = Ping($NetworkComputer, 250)
ConsoleWrite('Ping - Online? ' & $TestOnline & ' at ' & TimerDiff($TimeStarts) & 'ms' & @CRLF)
EndFunc ;==>_test

Func _WinAPI_PathFileExists($sFilePath)
Local $aReturn = DllCall('shlwapi.dll', 'int', 'PathFileExistsW', 'wstr', $sFilePath)
If @error Then
Return SetError(1, 0, 0)
EndIf
Return $aReturn[0]
EndFunc ;==>_WinAPI_PathFileExists

** G11 is online; G12 is offline (but is existing)

Output:

API - Online? 1 at 3.93117268793799ms

Ping - Online? 1 at 8.20257552028031ms

API - Online? 0 at 6739.93723056912ms

Ping - Online? 0 at 2249.59833463846ms

+++++++++++++++++++++++++++++++++++

Ping - Online? 1 at 0.782496947334891ms

API - Online? 1 at 0.680369040666808ms

Ping - Online? 0 at 2248.8030350791ms

API - Online? 0 at 6711.45924658898ms

Edit: Some grammar lapses

Edit 2: Changed output from 'autoit' to 'quote'

Edited by Mingre

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello again,

I just stumbled into a code I had copied before from the site, which is prolly good enough for my mini-project.

Here's it:

_list()


Func _list()
Local $foo, $sActiveLine, $line

$foo = Run(@ComSpec & " /c net view ", @SystemDir, @SW_HIDE, 2)

While 1
$sActiveLine = StdoutRead($foo)

If @error Then ExitLoop
If StringLeft($sActiveLine, 2) = "\\" Then
$line &= StringReplace($sActiveLine, "\\", "") & @CRLF
EndIf
WEnd

$line = StringReplace($line, " ", "")
$line = StringReplace($line, @LF, "")
$line = StringSplit($line, @CR)

TCPStartup()

For $i = 1 To $line[0] - 1
$result = Ping($line[$i], 1)
If Not @error Then
ConsoleWrite('>ONLINE : ' & $line[$i] & @CRLF)
Else
ConsoleWrite('+OFFLINE : ' & $line[$i] & @CRLF)
EndIf
Next
EndFunc ;==>_list

I apologize to this code's author for not citing you (because I don't know you, sorry!).

Edited by Mingre

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