PING server2, if responses, reboot then verify PING with server2 again

Hello Experts,
@echo
ping 192.168.7.8
if %errorlevel% == 1 "shutdown \\192.168.7.8"
ping 192.0.7.8
@pause

I have a 'Batch file" on Server1 and want to ping Server2 continuously.
a. Provide back to Server1 with the output of the time & date when Server2 is still alive.
b. If server2 is still alive, reboot Server2 via that Batch file's command.
c. Continuing PING, when Server2 is back online, recording the time & date.

I want to verify how long the Servers2 was Online then offline {Down} then it is up {Online} again.

Thank you,
Lonelygirl_2012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You didn't say what's supposed to happen if the target machine is offline when the script is started; should it just exit, or should it wait for the computer to come online? The following script just exits.
Warning: this script is live, it will reboot the target machine; use a test computer for testing.
@echo off
setlocal enabledelayedexpansion
set Computer=192.168.7.8
set Wait=15
echo [%Date%][%Time%] Testing if %Computer% is online.
ping.exe -4 -n 4 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	echo %Computer% is offline, can't send reboot signal.
	goto :eof
)
echo [%Date%][%Time%] Rebooting %Computer% in %Wait% seconds.
echo To cancel, stop the script NOW using ^<Ctrl-C^>.
set /a PingCount = Wait + 1
ping.exe -4 -n %PingCount% localhost >NUL
echo [%Date%][%Time%] Sending reboot signal to %Computer% ...
shutdown.exe /m \\%Computer% /r /f /t 0 /d p:0:0 /c "Shutdown initiated by '%UserDomain%\%UserName%' from '%ComputerName%'."
if errorlevel 1 goto :eof

<NUL set /p Dummy=[%Date%][%Time%] Waiting for %Computer% to go offline .
:LoopWaitForOffline
	<NUL set /p Dummy=.
	ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if not errorlevel 1 goto LoopWaitForOffline
echo.
echo [%Date%][%Time%] %Computer% is now offline.

<NUL set /p Dummy=[%Date%][%Time%] Waiting for %Computer% to come online .
:LoopWaitForOnline
	set /p Dummy=.<NUL
	ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 goto LoopWaitForOffline
echo.
echo [%Date%][%Time%] %Computer% is now online again.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris MillardCommented:
Based on your question, the initial problem I see is that as soon as Server1 sees that Server2 is online, Server1 will try to shutdown Server2.

What you have is a script that will CONSTANTLY be trying to restart Server2.

The other thing is that upon rebooting, Server2 will respond to pings before it accepts remote commands, so your batch will then fail on trying to reboot the server.

Bear in mind also that network services will shutdown well before the server reboots too, so you aren't going to get any accurate data.
0
Lonelygirl_2012Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.