;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       kfPing() 
;; 
;;ACTION         Pings a host, returns status or IP address 
;; 
;;AUTHOR         Glenn Barnas 
;;		 Based on Ping() by Jochen Polster  
;;		 rewritten to use WSH and now Kixforms.Shell 
;; 
;;VERSION	 1.0  - 2007/10/25 
;; 
;;HISTORY        1.0  - 2007/10/25 - Initial Release 
;; 
;;SYNTAX         kfPing(host, [Flag], [Wait]) 
;; 
;;PARAMETERS     host - name of host to ping 
;;               FLAG - if negative, returns IP Address 
;;                      if >0, specifies number of tries (default is 1) 
;;               Wait - optional ping timeout value 
;;                
;; 
;;REMARKS        Replacement for WSH based functions used in KF apps 
;;               ERROR is set to 0 if success, 1 otherwise. 
;; 
;;RETURNS        FLAG >=  0: returns 1 if host is reachable, 0 if not 
;;               FLAG =  -1: Returns IP address if resolvable, 0.0.0.0 if not 
;;               FLAG =  -2: Returns response string as array of lines 
;; 
;;DEPENDENCIES   OS Command Ping.exe, KixForms.DLL (classic) 
;; 
;;TESTED WITH    NT4, W2K, WXP, Vista, X64 
;; 
;;EXAMPLES       kfPing('hostname')       ; returns Success/Failure 
;;               kfPing('hostname',-1)    ; returns IP Address 
; 
Function kfPing($_Host, OPTIONAL $_Flag, OPTIONAL $_Wait)
 
  Dim $_oSys				; System KixForms object 
  Dim $_Tries				; # of times to ping 
  Dim $_Timeout				; Ping timeout value 
  Dim $_Response			; Response Flag 
  Dim $_aData				; 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 = 1				; 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 
 
  $_oSys = CreateObject('Kixtart.System')
  Select
   Case $_Flag = -1
    $_aData = Split($_oSys.Shell($_Cmd,0,3), @CRLF)
    $_Response = AScan($_aData, 'Pinging', 0, , 1)
    If $_Response >= 0
      $_Response = $_aData[$_Response]
      ; 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($_Response,'[') > 0
        $kfPing= Split(Join(Split($_Response,']',-1),'['), '[')[1]
      Else
        $kfPing = Split(Split($_Response,' ',-1)[1], ':')[0]
      EndIf
    Else
      $kfPing = '0.0.0.0'
    EndIf
 
   Case $_Flag = -2
    $kfPing = Split($_oSys.Shell($_Cmd,0,3), @CRLF)
    $_Response = 1
 
   Case 1
    Do
      $_aData = Split($_oSys.Shell($_Cmd,0,3), @CRLF)
      $_Response = IIf(AScan($_aData, 'TTL=', 0, , 1) = -1, 0, 1)
      If $_Response
        $_Count = $_Tries
      EndIf
      $_Count = $_Count + 1
    Until $_Count >= $_Tries
    $kfPing = $_Response
  EndSelect
  $_oSys = 0
 
  Exit Not $_Response       ; set the error code 
 
EndFunction