Page 1 of 1 1
Topic Options
#196081 - 2009-09-25 07:03 PM Quick way to check if a server exists
munrobasher Offline
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
#196082 - 2009-09-25 07:21 PM Re: Quick way to check if a server exists [Re: munrobasher]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
There are several Ping UDFs available that will do what you want. The Ping that I use allows you to specify several pings, but will return on the first successful ping - useful for "waking up" dormant connections.

 Code:
If Ping($Host, 10)
  ; run backup
EndIF


Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196086 - 2009-09-25 09:14 PM Re: Quick way to check if a server exists [Re: Glenn Barnas]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Problem with a medium-level development server that's sometimes out of action, is that it may respond to ping but be otherwise dead to the world. I've seen unreliable servers act like a LaBrea tarpit.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#196102 - 2009-09-26 06:04 PM Re: Quick way to check if a server exists [Re: Les]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
There is always WMI but that can be slow.

I have used net view and if there is a return.

All methods are going to have a "timeout" feature of some sort you just need to find the one that responds in what you deem a reasonable time.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#196111 - 2009-09-28 09:05 AM Re: Quick way to check if a server exists [Re: Les]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: Les
Problem with a medium-level development server that's sometimes out of action, is that it may respond to ping but be otherwise dead to the world. I've seen unreliable servers act like a LaBrea tarpit.


True, a server can have the network stack functional while the rest of the OS is out to lunch.

However PING is a cheap way to avoid a machine that really is unavailable such as development or VM which are regularly shut down to reduce cost or conserve resources.

So you may still get broken machines which respond to PING and very little else, but at least you've narrowed the field before attempting more expensive activities.

Top
#196126 - 2009-09-28 02:05 PM Re: Quick way to check if a server exists [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I'm in agreement with Richard. I generally avoid Ping as a "server up" test for just the reason that Les pointed out, but given the OP's requirements, it sounds like the server is ON or OFF, and not in a nebulous state. In this particular situation, I'd start with Ping.

Of course, if you are concerned with the server being in a strange state that replies to pings but isn't functional, I'd do something like
 Code:
If Ping($Host, 10)
  If Exist('\\server\share')
    ; copy the files to \\server\share
  Else
    'Server is not functional!' ?
  EndIf
EndIf
Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#196153 - 2009-09-30 01:49 AM Re: Quick way to check if a server exists [Re: Glenn Barnas]
k711 Offline
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 Offline
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
#196156 - 2009-09-30 11:19 AM Re: Quick way to check if a server exists [Re: k711]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Perhaps you have not added the Ping() UDF to your script?

The various ping UDFs are user contributed functions and are not part of the base KiXtart interpteter. You need to include them in your script by copy-and-paste, or by storing them in an additional file and using CALL to read them in.

You can find the UDFs in the link that Witto has posted.

Top
#196205 - 2009-10-01 06:33 PM Re: Quick way to check if a server exists [Re: Richard H.]
k711 Offline
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 Offline
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.

 Code:
; ********* 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 Offline
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
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 595 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.061 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org