Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

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

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
anuroopkoka2005
Asked:
anuroopkoka2005
  • 6
  • 4
  • 2
2 Solutions
 
QlemoC++ DeveloperCommented:
@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
 
anuroopkoka2005Author Commented:
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
 
QlemoC++ DeveloperCommented:
That's no good practice (anymore), to jump that much. What is wrong with my script?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
t0t0Commented:
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
 
anuroopkoka2005Author Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
t0t0Commented:
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
 
t0t0Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
Oopps, am I that slow today?
0
 
t0t0Commented:
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

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.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now