Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Script to ping two servers and execute if any one of the server is not reachable.

Posted on 2009-05-19
14
Medium Priority
?
486 Views
Last Modified: 2012-08-14
Hi experts,

Can someone provide me a script that checks the ping response of 2 servers at the same time and run the batch file if any one fails.

If both servers are pingable the it should run another batch file...
 
0
Comment
Question by:anuroopkoka2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
14 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 24426262
@echo off
set answer=
ping -n 1 -w 100 server1 | find "TTL" >nul && set answer=1
ping -n 1 -w 100 server2 | find "TTL" >nul && set answer=2
if "%answer%" == "12" (
   echo Do your "all ok" stuff here
) else (
   echo "Not all servers responding" stuff here
)

0
 
LVL 4

Author Comment

by:anuroopkoka2005
ID: 24426768
Sorry about that and thanks for the script..\\
I wrote a new script Refer to the below one This will call the batch files accordingly... as per my needs


I
@ECHO OFF
 
ping 192.168.1.11 | findstr "Reply"
if "%errorlevel%"=="0" goto T2
goto ADD
 
:T2
ping 192.168.1.12 | findstr "Reply"
if "%errorlevel%"=="0" goto REMOVE
goto ADD
 
:ADD
call ADD.bat
GOTO END
 
 
:REMOVE
call REMOVE.bat
GOTO END
 
:END
ENDLOCAL

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24427271
That's no good practice (anymore), to jump that much. What is wrong with my script?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:t0t0
ID: 24427819
Good evening gentlemen.... How about doing something along these lines:

@echo off
ping -n 1 -w 100 server1 >nul
if %errorlevel% equ 0 (
   ping -n 1 -w 100 server2 >nul
   if %errorlevel% equ 0 (
      exit /b
   )
)

rem process rest of batch file commands
0
 
LVL 4

Author Comment

by:anuroopkoka2005
ID: 24428618
Sorry for all these misunderstanding...

Actually Qlemo your script is not calling some of the batch file which is supposed to call.. The main purpose behind this script is calling batch files when the server is not reachable. It is still going to else when both the servers are reachable.

and in the mean time i also started developing a small batch file and it did that trick...

0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 24429452
Your are right, I had a typo, of course it has to be (see below).

It's ok to try to solve it yourself while you wait for answers. But you should honour posts then, if they show acceptable alternative ways, by giving a share. And one correction should be allowed before dismissing ;-)

@echo off
set answer=
ping -n 1 -w 100 server1 | find "TTL" >nul && set answer=1
ping -n 1 -w 100 server2 | find "TTL" >nul && set answer=%answer%2
if "%answer%" == "12" (
  echo Do your "all ok" stuff here
) else (
  echo "Not all servers responding" stuff here
)

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24429535
t0t0,
that was rude behaviour, to object in a question you did not participate, just to present your own solution. A objection should only be posted by an contributing expert, which is me. I had my reasons to do not.

Next: exit /b in a if - brrrr. This could be part of batch file of many commands, and it is not working that way then.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24432749
Qlemo
Firstly, let me clear up the objection thing... When I first looked at this question, I saw only three replies - yours and anuroopkoka2005's.

Admittedly, I only glanced at your code - enough to acknowledge the use of the neatly laid out parallel PINGs rounded off nicely with a clear and simple IF...ELSE structure. I did however, notice the IF condition and I recall thinkng to myself how creative it was.

The only other thing that immediately caught my eye was the familiar Qlemo-brand use of the '&&' logical test. It looked good and at that moment in time I had no reason to suspect, nor did I realise, your code contained a 'typo'.

I looked at anuroopkoka2005 and immediately noticed the tangled use of GOTOs and CALL statements. I counted the number of lines in his code - which was 16, and counted how many of those were branch instructions - 8!!

I glanced at your next comment. Without actually readin it word-for-word, I picked out the "That's no good practice..." and the "What is wrong with my script?".

It was at that point that I read the asker's reason for wanting to close the question. Naturally, I was led to believe the asker had made up his mind and there was nothing more that could be added which would persuade him to retract his request to close the question.

When an asker states he wants to close a question because he has 'now' figured it out for himself, I often wonder how much of their solution is actually based on contributiuons submitted up to that point. And, to be fair, I have absolutely no objection to anyone closing a question providing a solution has not already been submitted. However, in this case, because you HAD submitted a solution, I thought it was unfair not to give you that recognition even if it was to say "I'll accept your solution as the answer even though I have since developed my own solution having figured it out for myself".... or words to that effect. But this didn't happen.

And your resounding question "What is wrong with my script?" came back at me. At that time, I firmly believed this remark was aimed at anuroopkoka2005 however, in the back of my mind, I thought it's the ASKER who should be answering THAT question - not anuroopkoka2005.

So, if the asker (as it appeared) didn't want to accept YOUR solution, I thought I'd throw something at him to see how he responds while at the same time (on the grounds of unfairness) objected to the question being closed (based on the facvt that this decision was made AFTER a possible solution (s) had been submitted.

At this point, I re-read the question I think at least twice and set about coding up a solution from the ground up (I used the same server names for reasons of consistency).

I saw absolutely no harm in objecting to the asker closing his question. I did not under any circumstances intend to step on your toes, and had absolutely no idea my objection would be perceived as being 'rude behaviour'.

My comment was posted twice purely by accident - I still don't know how that could have happened as I actually typed my reply into the browser itself although, I do recall hitting the RETURN key several times becuase my PC was slow to respond.

As far as I'm concerned, if the asker retracts hs request to close this question and awards points then I'm more than happy on this occasion for those points to be awarded to you if you genuinely feel I have intentionally behaved with rudeness however, I am not aware of any guidance which states a question cannot be closed by a non-contributing expert.

Secondly, you say I only objected because I wanted to present my own code. I have given my reasons why I objected. I have been open and honest in my reply - even to the point of discussing my thoughts for my actions. If I truly did not feel there was anything else that could have been added to this question then I would not have submitted code myself however, I would of at least commented on anuroopkoka2005's spaghetti-like code and made some comment about the unfairness of closing the question for the reasons already stated.

Finally, your comment: "Next: exit /b in a if - brrrr". I know it doesn't look nice however, I'm led to believe there are no stack issues in do so as any internal stacks created during the execution of batch files are destroyed on exiting back to DOS. Also, I have used IF as a process of selection rather than iteration.

So, just to appease you, and because I (now) know you hate the use of EXIT from within IF statements, I'd like to submit the following code (which I assume will go unappreciated by the asker - most probably due to it's length alone).


@echo off
set OnLine=1
set OffLine=0
set EitherServer=0

ping -n 1 -w 100 server1 | find "TTL" >nul && set /a EitherServer = OnLine
ping -n 1 -w 100 server2 | find "TTL" >nul && set /a EitherServer = OnLine

if EitherServer == Offline (
   rem Process Batch Commends
)
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 1000 total points
ID: 24432940
Oops!! Please ignore that last code.... It was my crappy attempt to impress you...At this time of the day it's difficult getting my head around the logic as I'm rushing to make tea for my child as well.

This might work though:


@echo off
set OnLine=1
set OffLine=2
set BothServers=0

ping -n 1 -w 100 server1 | find "TTL" >nul && set /a EitherServer += OnLine
ping -n 1 -w 100 server2 | find "TTL" >nul && set /a EitherServer += OnLine

if not BothServers == OnLine (
   rem Process Batch Commends
)
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24433080
Sorry should I have been "rude" myself. I now understand, but you might see my point of view when you objected.

Technically, your last code is not what is requested - I guess you wanted to apply a boolean AND to the second ping. The above code would give Offline if BOTH server are offline, but the request was to check if both are ONLINE.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 24433095
Oopps, am I that slow today?
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24433326
I give up!...... That's not right either. My rushed efforts to fudge this only bothes it even more.

The logic was kind-of there.... but, i'm practically rushing out the the door with my kiddie and I'm typing this quickly to save my neck from the noose!

Qlemo. The points are your!!

(I may re-visit later tonight after my return from girls guides. If the question's still open then I may try to restore some dignity in my coding otherwise, I'll be ahppy to swiftly move on to the next question (and start on a fresh footing and without being rude)).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question