;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       ping() 
;; 
;;ACTION         ping - Pings a host 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION	 2.0  - 2007/10/20 
;;HISTORY        1.0  - 2003/11/29 - Initial Release 
;;                based on KORG Ping UDF by Jochen Polster, enhanced to  
;;                return values and IP's. 
;;               2.0  - 2007/10/20 -  WHS version 
;; 
;;SYNTAX         ping(host, [FLAG], [Wait]) 
;; 
;;PARAMETERS     host - Required - String 
;;               - name or IP address of host to ping 
;; 
;;               FLAG - Optional - Integer 
;;               - If negative, returns IP Address that resolved to the hostname 
;;               If >0, specifies number of tries (default is 1) 
;; 
;;               Wait - Optional - Integer 
;;               Ping timeout value 
;;                
;; 
;;REMARKS        ERROR is set to 0 if success, 1 otherwise. 
;; 
;;RETURNS        Int (1=responds) or string containing IP address. 
;;               FLAG >= 0: returns 1 if host is reachable, 0 if not 
;;               FLAG <  0: Returns IP address if resolvable, 0.0.0.0 if not 
;; 
;;DEPENDENCIES   OS Commands Ping & Find 
;; 
;;TESTED WITH    NT4, W2K, WXP 
;; 
;;EXAMPLES       Ping('hostname')       ; returns Success/Failure 
;;               Ping('hostname', -1)   ; returns IP Address 
; 
Function Ping($_Host, OPTIONAL $_Flag, OPTIONAL $_Wait)
 
  Dim $_oExec				; WSH Object 
  Dim $_Tries				; # of times to ping 
  Dim $_Timeout				; Ping timeout value 
  Dim $_Response			; Response Flag 
  Dim $_Line				; Line returned from command string 
  Dim $_Cmd				; first part of command string 
  Dim $_Count				; current ping count 
 
  $_Flag    = Val($_Flag)		; determine what to do 
  $_Wait    = Val($_Wait)		;  
  $_Tries   = 1				; one ping 
  $_Timeout = 1000			; 1 second timeout 
 
  ; set timeout if Wait is non-zero 
  If $_Wait > 0
    $_Timeout = $_Wait
  EndIf
 
  If $_FLAG > 0        ; Multiple pings - return on first reply 
    $_Tries = $_FLAG
  EndIf
 
  ; Ping the host $_Tries times, but only until a response is received 
  $_Count = 0
 
  ; search for reply from host during PING 
  $_Cmd = '%COMSPEC% /c ping.exe -4 -n 1 -w ' + $_Timeout + ' ' + $_Host 
  If $_Flag < 0
    $_Cmd = $_Cmd + ' | %SystemRoot%\System32\Find "Pinging"'
  Else
    $_Cmd = $_Cmd + ' | %SystemRoot%\System32\Find "Reply" | %SystemRoot%\System32\Find "TTL="'
  EndIf
  Do
    $_oExec = CreateObject("WScript.Shell").Exec($_Cmd)
    If Not VarType($_oExec)=9 $Ping = 'WScript.Shell Exec Unsupported' Exit 10 EndIf
    $_Line = Split(Join(Split($_oExec.StdOut.ReadAll + $_oExec.StdErr.ReadAll,CHR(13)),''),CHR(10))[0]
    $_Response = IIf($_Line, 1, 0)
    If $_Response
      $_Count = $_Tries
    EndIf
    $_Count = $_Count + 1
    If $_Count <  $_Tries Sleep 0.25 EndIf
  Until $_Count >= $_Tries
 
  ; If FLAG >= 0, return success/failure - otherwise return IP address 
  If $_FLAG >= 0
    $Ping = $_Response
  Else
    If Not $_Response
      $Ping = '0.0.0.0'
    Else
      ; In this mode we return the IP address - we should have the HOSTNAME 
      ; handle the 'duh' factor for times when we get the IP address instead! 
      If InStr($_Line,'[') > 0
        $Ping= Split(Join(Split($_Line,']',-1),'['), '[')[1]
      Else
        $Ping = Split(Split($_Line,' ',-1)[1], ':')[0]
      EndIf
    EndIf
  EndIf
 
  Exit Not $_Response       ; set the error code 
 
EndFunction