.bat file to check servers are up

12,928

For a simple check whether the server is up or not you could put all IP addresses in a file and call the following script with script.bat urls.txt:

@echo off
for /f %%a in (%1) do (
    echo Pinging %%a ...
    ping -n 1 %%a | find "Reply" > NUL
    if not errorlevel 1 (echo %%a is up) else (echo %%a is down)
)

urls.txt should look like this (just a list of IP addresses of your servers or urls):

www.google.com
www.amazon.com
10.2.1.12
8.8.8.8
Share:
12,928

Related videos on Youtube

Rauf
Author by

Rauf

:)

Updated on September 18, 2022

Comments

  • Rauf
    Rauf over 1 year

    We are running ERP software which is deployed to number of servers.  If we get a problem, we usually restart the servers and cross check that all the servers are running (just check if we get login page or not).  Alternatively, we ping to IP address 192.XXX.X.XX to check the 'reply'.

    This checking process is time consuming.  How can I create a .bat file to check and give me results like

    192.XXX.X.XX:80 OK
    192.XXX.X.XX:81 OK
    192.XXX.X.YY:80 No Reply
    
  • Rauf
    Rauf over 7 years
    I tried this. But where I can find the result ? I ran 'cmd', then put script.bat urls.txt. The cmd did not give me any result.
  • DavidPostill
    DavidPostill about 7 years
    Have you actually tested this? This batch file contains at least 4 mistakes.
  • cmtjk
    cmtjk about 7 years
    @Rauf, I updated the script
  • cmtjk
    cmtjk about 7 years
    @DavidPostill, no I'm not on a Windows machine but the new script should work. feel free to edit!
  • DavidPostill
    DavidPostill about 7 years
    @r3r57 Revised script works. Please test next time :)
  • Bruno  Vincent
    Bruno Vincent about 5 years
    How do your run this? If my files are in this directory: C:/BatchPing/url.txt C:/BatchPing/script.bat I know what to put in URL, but what do I put into .bat file? Also, what would be the exact code for this situation? Do I need to substitute %%a and %1 with something?
  • Ghulam Akbar
    Ghulam Akbar about 4 years
    The above script works fine. But if ping is prohibited for some URL/server, this batch script doesn't work. In this case, what should be the modified script ...?
  • mivk
    mivk over 3 years
    You should probably use find "TTL=" instead of find "Reply".
  • Maris B.
    Maris B. over 3 years
    @mivk is right. For IPv4 the "TTL=" is the most reliable method. The "Reply" is found even when there is an error "Destination host unreachable". Check this thread for more info: stackoverflow.com/questions/9329749/…