#196081 - 2009-09-25 07:03 PM
Quick way to check if a server exists
|
munrobasher
Just in Town
Registered: 2009-06-30
Posts: 3
Loc: Cheshire
|
In our logon script, we backup some local folders as a belt-n-braces backup as we don't officially backup local drives. The server that this backs up to is a medium-level development server that's sometimes out of action. The script uses robocopy but this pauses for about 30 seconds if the target server isn't available.
I've also tried:
if exist("\\server004\share")<>0 ..do backup.. endiff
but this also takes a long time to fail if the server is down.
So looking for another way to quickly check if the server exists... something like ping with error result sort of thing, as ping soon works out of the server is down.
Cheers, Rob.
|
|
Top
|
|
|
|
#196153 - 2009-09-30 01:49 AM
Re: Quick way to check if a server exists
[Re: Glenn Barnas]
|
k711
Just in Town
Registered: 2009-09-29
Posts: 2
Loc: az
|
Uh.. I am a newbie at kix. I just cant get this ping to work! Please let me know what dumb mistake I am making.
If Ping($Host, 10) ; run backup EndIF
When I run something like this the first line above says expected ')'
What am I doing wrong?
Thanks
|
|
Top
|
|
|
|
#196155 - 2009-09-30 08:52 AM
Re: Quick way to check if a server exists
[Re: k711]
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Look at http://www.kixtart.org/udf/ for some "ping" udf's and addd them to your code. I do like WMIPing()
|
|
Top
|
|
|
|
#196205 - 2009-10-01 06:33 PM
Re: Quick way to check if a server exists
[Re: Richard H.]
|
k711
Just in Town
Registered: 2009-09-29
Posts: 2
Loc: az
|
Thanks, I'll check out the UDF's
Here's what I did to get this to work, and is short and simple! -------------------------------------- $strHost = "COMPUTER NAME" $strDownCmd = "c:\down\down /p /f /q \\" + $strHost $strPingCmd = "ping -n 1 " + $strHost
SHELL $strPingCmd
IF @ERROR AT (15,25) "Machine " + $strHost + " not running" ELSE ; COMPUTER IS POWERED UP SHELL $strDownCmd
ENDIF
Thanks!
|
|
Top
|
|
|
|
#196226 - 2009-10-02 10:02 AM
Re: Quick way to check if a server exists
[Re: k711]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Use one of the pre-rolled UDFs.
Your code has got some problems because of how PING and SHELLed commands work. We've already worked through these problems and created some nice fully functional UDFs which hide the working and give you a simple interface to robust code.
Take a look at the code below. It is quite long but you don't need to worry about the functions. These are black boxes and you use them in the same way as you would the KiXtart built-in functions - you don't need to know how they work, just what the inputs and outputs are.
; ********* YOUR CODE STARTS HERE *********
Dim $strHost,$strHostList,$iResponse
$strHostList="COMPUTER1 COMPUTER2.COMPANY.COM 127.0.0.1"
For Each $strHost in Split($strHostList)
$iResponse=wmiPing($strHost)
If @ERROR
"Error pinging "+$strHost+", reason code ["+@ERROR+"] "+udfPingStatus(@ERROR)+@CRLF
Else
"Host "+$strHost+" is up, response time is "+$iResponse+@CRLF
EndIf
Next
; ********* YOUR CODE ENDS HERE *********
; -------------------------------------------------------------------------
; ********* PAY NO ATTENTION TO THE FUNCTIONS BEHIND THE CURTAIN **********
; -------------------------------------------------------------------------
;FUNCTION wmiPing()
;
;ACTION:
; Ping an IP-address/name and return the responsetime in msecs.
;
;AUTHORs:
; KHOLM
; Jooel
; Fixes by Richard H.
;
;VERSION:
; 1.1-rh
;
;DATE CREATED:
; 11 Dec 2006
;
;REVISION HISTORY:
; Original release from 2004 can be found here
;
; 20091002 Richard Howarth <rhowarth@harsco.com>
; Fixed handling of NULL status code.
; Explicitly return success
; Set error is no responses found
; UDF now explicitly exits on first result
; Assume NULL is a hostname lookup failure
;
;KIXTART:
; 4.22
;
;SYNTAX;
; wmiPing($Address[,$Timeout [,$BlockSize]])
;
;PARAMETERS:
; $Address: Name and IP-address of target to ping
; $Timeout: Optional, max waittime for reply in msecs. Default is 1000
; $BlockSize: Optional, size of send/recieve block in bytes. Default is 32
;
;RETURNS:
; Time of response in msecs if successfull
; otherwise @error is set to statuscode.
;
;DEPENDENCIES:
; Windows XP or Server 2003
;
;EXAMPLE:
; $Add = '195.140.240.55' ; Same as $Add = 'kixtart.org'
; $Res = wmiPing($Add,5000)
; If @Error
; ? 'No response from: ' + $Add
; Else
; ? 'Responsetime for: ' + $Add + ' is ' + $Res + ' ms'
; EndIf
;
;SOURCE:
Function wmiPing($Address,Optional $Timeout,Optional $BlockSize)
Dim $Query,$oWMI,$oItem,$cItems
$Query = "Select ResponseTime,StatusCode From Win32_PingStatus Where Address='" + $Address + "'"
If $Timeout $Query = $Query + " And TimeOut=" + $Timeout EndIf
If $BlockSize $Query = $Query + " And BufferSize=" + $BlockSize EndIf
$oWMI = GetObject("winmgmts:root\cimv2")
$cItems = $oWMI.ExecQuery($Query)
For Each $oItem In $cItems
If VarType($oItem.StatusCode) > 1
If $oItem.StatusCode
Exit $oItem.StatusCode
Else
$wmiPing = $oItem.ResponseTime
Exit 0
EndIf
Else
Exit 8524 ; Dummy exit value for NULL status, most descriptive (DNS lookup failure)
EndIf
Next
Exit 11050
EndFunction
Function udfPingStatus($ERROR,Optional $SERROR)
If Not VarName($SERROR) $SERROR="Unrecognised error code" EndIf
$udfPingStatus=$SERROR
Select
Case $Error="8524" $udfPingStatus="NULL result - probable host name lookup failure"
Case $Error="11001" $udfPingStatus="Buffer Too Small"
Case $Error="11002" $udfPingStatus="Destination Net Unreachable"
Case $Error="11003" $udfPingStatus="Destination Host Unreachable"
Case $Error="11004" $udfPingStatus="Destination Protocol Unreachable"
Case $Error="11005" $udfPingStatus="Destination Port Unreachable"
Case $Error="11006" $udfPingStatus="No Resources"
Case $Error="11007" $udfPingStatus="Bad Option"
Case $Error="11008" $udfPingStatus="Hardware Error"
Case $Error="11009" $udfPingStatus="Packet Too Big"
Case $Error="11010" $udfPingStatus="Request Timed Out"
Case $Error="11011" $udfPingStatus="Bad Request"
Case $Error="11012" $udfPingStatus="Bad Route"
Case $Error="11013" $udfPingStatus="TimeToLive Expired Transit"
Case $Error="11014" $udfPingStatus="TimeToLive Expired Reassembly"
Case $Error="11015" $udfPingStatus="Parameter Problem"
Case $Error="11016" $udfPingStatus="Source Quench"
Case $Error="11017" $udfPingStatus="Option Too Big"
Case $Error="11018" $udfPingStatus="Bad Destination"
Case $Error="11032" $udfPingStatus="Negotiating IPSEC"
Case $Error="11050" $udfPingStatus="General Failure"
EndSelect
EndFunction
|
|
Top
|
|
|
|
#196403 - 2009-10-19 06:42 PM
Re: Quick way to check if a server exists
[Re: Richard H.]
|
munrobasher
Just in Town
Registered: 2009-06-30
Posts: 3
Loc: Cheshire
|
Thanks everyone for the replies. For some reason, I didn't get notifications of the replies and I've the job has just esclated itself to remind myself.
I'll try the Ping UDF.
Cheers, Rob.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 595 anonymous users online.
|
|
|