1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Help making an automated ping batch file

Discussion in 'Windows XP' started by TechUserCJ, Apr 7, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. TechUserCJ

    TechUserCJ Thread Starter

    Joined:
    Apr 20, 2009
    Messages:
    7
    Hey all,

    I am in charge of searching for files at our clients' firms using a batch file. One issue I run into is that some computers at a firm may be left off, and so the search for the files is unsuccessful. It'd be nice to have a way to check if the relevant computers at a firm are on. As such, I am working on an automated ping batch file that reads IPs from a text file and logs the results to a batch file.

    I am not so experienced with writing batch files, but I was able to cobble something together based on some code I saw online. However, it's not working--it logs IP addresses as responding whether or not they actually are. I have reproduced the batch code below. I assume that the problem has something to do with the error level. If someone could take a look and give me some pointers on getting the code to work (and perhaps some insight into why what I have doesn't work), I would greatly appreciate it. Thanks.

    Code:
    @echo off
    
    Set CompList=complist.txt
    Set PingListLog=pinglistlog.txt
    
    If Not Exist "%CompList%" (
    
       Echo Cannot find list of computers called %CompList%
       Pause
       GoTo :EndOfScript
    
       )
    
       
       echo Scanning began on %date% at %time% > %PingListLog%
       
       for /f %%i in (%CompList%) do call :ScanLbl %%i
       
       echo Scanning completed on %date% at %time% >> %PingListLog%
       
       GoTo :EndOfScript
    
       :ScanLbl
       
       echo Scanning %1
       set attrib=responding
       ping -n 1 %1
       if errorlevel 1 set attrib = nonresponsive
       echo %1 is %attrib% >> %PingListLog%
       
       :EndOfScript
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Code:
    C:\WINDOWS\system32>echo hello
    hello
    
    C:\WINDOWS\system32>echo errorlevel
    errorlevel
    
    C:\WINDOWS\system32>echo %errorlevel%
    0
    
    C:\WINDOWS\system32>
    Code:
    if %errorlevel%==1 set attrib=nonresponsive
     
  3. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    You are defining two variables. attrib and attrib{space}. They are not the same.

    Remove the space after attrib in the if errorlevel line.
    The space after the equals sign isn't needed either:
    Code:
    if errorlevel 1 set attrib=nonresponsive
    If errorlevel 1 is the same as If %errorlevel% GEQ 1, and is true if the errorlevel is 1 or greater
    using if %errorlevel%==1 checks for the specific error number of 1.
    Either should work in this case.

    One case where you might get a responding result is if a router replies with a message that the destination route is unreachable. In that case ping does get a response, so the errorlevel is 0:
    Code:
    C:\Temp Dir\test>ping 192.168.9.30
    
    Pinging 192.168.9.30 with 32 bytes of data:
    Reply from 192.168.9.205: Destination host unreachable.
    Reply from 192.168.9.205: Destination host unreachable.
    Reply from 192.168.9.205: Destination host unreachable.
    Reply from 192.168.9.205: Destination host unreachable.
    
    Ping statistics for 192.168.9.30:
        Packets: Sent = 4, [COLOR=Red][B]Received = 4[/B][/COLOR], Lost = 0 (0% loss),
    
    C:\Temp Dir\test>echo %errorlevel%
    [COLOR=Red][B]0[/B][/COLOR]
    You can use this method to check for that result:
    Code:
    echo Scanning %1
    set attrib=responding
    ping -n 1 %1|Findstr /I /C:"timed out" /C:"host unreachable"
    if %errorlevel%==0 set attrib=nonresponsive
    echo %1 is %attrib%>> %PingListLog%
    
     
  4. TechUserCJ

    TechUserCJ Thread Starter

    Joined:
    Apr 20, 2009
    Messages:
    7
    Thanks for the help, folks. One thing that I'm still getting used to is that white space matters in batch files. :)

    TheOutcaste: Could you explain what the part that you added to the ping statement does? I.e.:

    ping -n 1 %1|Findstr /I /C:"timed out" /C:"host unreachable"

    What does the /C switch do? Is that something to do with the C drive, or what?

    Thanks.
     
  5. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Click Start | Run (or press WinKey+R), type cmd, press Enter
    Type findstr /? for an explanation of all the switches.
    Not sure why they picked the letter C for this one. My best guess is it's for Combined. If you specify a search string as just "timed out", it will search for timed or out. Out to lunch would match.
    Using /C:"timed out" combines the two words into one phrase, so it has to be an exact phrase match.
    From the help:
    /C:string Uses specified string as a literal search string.
    What that line does is Pipe (the | symbol is Pipe) the output of the Ping command to Findstr, which checks for either of the two strings indicated with the /C: switches. The /I means it's not case sensitive.
    The result is checked in the If statement; if the errorlevel is 0, one of the strings was found, meaning an error occured.
    Easy way to remember the errorlevel:
    F0und
    M1ssing
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/915378

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice