Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Batch file using ping and restart

Posted on 2009-12-23
9
Medium Priority
?
1,521 Views
Last Modified: 2013-12-05
Hello,

I am wondering if anyone knows if it is possible to have a batch file that pings an IP address, and if it can't connect will initiate a reboot of the server.  I am looking to have this as a scheduled task that runs periodically throughout the day.
Thank you.
0
Comment
Question by:bkort1
9 Comments
 
LVL 1

Expert Comment

by:Wizard_Of_Dos
ID: 26115676
I would imagine there might be a better solution to the problem you are facing, but here is what you asked.
You can do it with a Pipe.  Simply replace the server address with your own and run. Obviously the statements can be changed to whatever you wish. This is pretty self explanitory on what it does but if you have any questions let me know. One important this to remember is the Find searches explicitly... proper case is imperative.

 Example: Server Ping and Reboot.bat

@echo off
echo Server Ping and Reboot

Ping 4.2.2.2

echo.
echo.

ping 4.2.2.2 | find "Reply from" > nul
if %ERRORLEVEL% == 0 goto Server_Replied

ping 4.2.2.2 | find "Request timed out" > nul
if %ERRORLEVEL% == 0 goto No_Reply


echo Possible invalid server address.
pause
goto exit

:Server_Replied
echo Echo Ping of Server Was Succesful.
pause
goto exit

:No_Reply
echo No Reply from server. Request timed out.
echo Initiating reboot of server
shutdown -r

:exit

0
 
LVL 11

Expert Comment

by:enriquecadalso
ID: 26115693
Hello. It is not clear to me what do you want to do when cant connect. What server reboot, the local or the remote IP?
0
 
LVL 11

Expert Comment

by:nordtorp
ID: 26115710
bkort1,

 have attached a short code example for you. Just copy the code attached and save the file as <something>.bat

I think there are some flaws about your question, though. First, if you can't reach it by ping, you most certainly won't be able to initiate a remotely reboot.

Just to clarify what you want to reboot. You run the script on SERVER1 to ping SERVER2 and if that doesn't answer what server do you want to reboot? SERVER1 or SERVER2?
ping 127.0.0.1 -n 1 > ping.log
if %errorlevel% == 1 "shutdown -f -r -m \\127.0.0.1 -t 0"

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:bkort1
ID: 26115785
It would reboot the local machine, not the one it is pinging.
0
 
LVL 11

Accepted Solution

by:
nordtorp earned 2000 total points
ID: 26115805
The correct code would then be like this

ping SERVER2 -n 1 > ping.log
if %errorlevel% == 1 "shutdown -r -f -t 0"
0
 
LVL 1

Expert Comment

by:Wizard_Of_Dos
ID: 26115934
Nordtorp's shortend solution will certainly work if you do not wish to add any other provisions or arguments for checking the possibilities of various network conditions, or trying the test from and alternate network adapter, testing an alternate server ext.
0
 
LVL 11

Expert Comment

by:nordtorp
ID: 26115988
I admit my solution is a short solution, and it does not do much of testing to determine anything.

The original question were if it were possible, and we have proven that it is possible.

As for an other side note. The batch scripts given so far will only try to ping once or four times. If it fails on just one ping request, the server will restart. In some situations the network might drop for a second. What would then happen if the server run the task just then? The server would reboot.
0
 
LVL 1

Expert Comment

by:Wizard_Of_Dos
ID: 26116411
I agree, and as stated i still think there is a better solution to his actual problem. Since he wants this to run as a scheduled task your lightweight approach might be perfect for what he wants. Personally IF (and that's a big IF) i was going to run a simple scheduled ping and let it's results potentially cause a server reboot, i would carefully construct the task to include arguments to rule out a single dropped packet or or other possibilities, perhaps on failure attempting al alternate server or even resetting the tcp stack.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 26116597
> As for an other side note. The batch scripts given so far will only try to ping once or four times. If it
> fails on just one ping request, the server will restart. In some situations the network might drop for
> a second. What would then happen if the server run the task just then? The server would reboot.

This is why I typically take the following approach.  I do a first test with a single PING.  If I get a response then the server is up and I'm done.  But if I don't get a response, then I do a second PING, for 5 ot 10 itterations.  If no request is received for any of those pings then I go ahead and assume it is down.

This approach keeps the script running fairly fast when servers are up, but helps work around the problem mentioned above, and making sure a group of pings all fail.

~bp
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

On a regular basis I get questions about slow RDP performance, RDP connection problems, strange errors and even BSOD, remote computers freezing or restarting after initiation of a remote session. In a lot of this cases the quick solutions made b…
Understanding the various editions available is vital when you decide to purchase Windows Server 2012. You need to have a basic understanding of the features and limitations in each edition in order to make a well-informed decision that best suits …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

572 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