1

I am trying to create an automated ping test using a .cmd file that pulls the ip addresses from a file called computers. It looks like it works for the most part but all the ping test after the first come back negative or dead.
A picture of the results

@echo off
set fnm=C:\Users\jelliott\Desktop\computers.txt
set lnm=C:\Users\jelliott\Desktop\results.txt

if exist %fnm% goto Label1

echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof

:Label1
echo PingTest STARTED on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo PingTest ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

:Sub
echo Testing %1
set state=alive
ping -n 1 %1 
if errorlevel 1 set state=dead
echo %1 is %state% >> %lnm%
aschipfl
  • 33,626
  • 12
  • 54
  • 99
JacobElliott
  • 127
  • 1
  • 1
  • 8
  • Take a look at this script : [Run command based on IP address from text file](http://stackoverflow.com/questions/41102657/run-command-based-on-ip-address-from-text-file?answertab=active#tab-top) – Hackoo Jan 10 '17 at 16:58
  • Silly question, but ... from your output you are reading machine names not ip addresses. Have you checked the name resolution is working? – MC ND Jan 10 '17 at 17:12
  • First step to batch debugging: **Remove `@echo off` from the top of your file and read the errors.** That line is suppressing the debugging information, and you are asking people on SO to guess what's going wrong from the whole batch file. – Ryan Bemrose Jan 10 '17 at 19:24
  • Looks like it was how I had the computers.txt file setup :( – JacobElliott Jan 10 '17 at 19:42

1 Answers1

0

I don't know if this is the source of the problem but the errorlevel after a ping request is not reliable: it is not always set on 0 after a successful ping. The most reliable way to check a successful ping is to search for "TTL=" in the output of the ping command. The output of a successful ping always contains the TTL of the ping answer. In other cases, the output won't conain "TTL=".
Check the errorlevel of this command instead:

ping -n 1 %1 | find "TTL="

find allows to search for a literal string in some text. It will set the errorlevel on 0 if it is found and 1 otherwise. You can also use findstr instead of find.

EDIT: This is only in case the addresses you're pinging to are IPv4. For IPv6 addresses, the errorlevel is set correctly. For IPv6 no TTL is printed so the errorlevel must be used to check if the ping succeeded or not. [Here] you can find a general way to check if the ping was successful or not (either it is IPv4 or IPv6). Thank you MC ND for the remark and the link. If you ever want to force IPv4 or IPv6, you can use respectively the -4 or the -6 flags of ping.

Community
  • 1
  • 1
J.Baoby
  • 2,167
  • 2
  • 11
  • 17
  • 1
    It is better to check for `TTL=` (there are errors that include the `TTL` string), and **only** if you are testing ipv4 addresses. (more [here](http://stackoverflow.com/a/27748080/2861476)) – MC ND Jan 10 '17 at 17:26
  • It still didn't work. I noticed that after the first ping, the one it says is alive, it gives the reply with the bytes time and so on but with the rest it simple says checking "whatever IP" – JacobElliott Jan 10 '17 at 19:37
  • Can you add that output in your post?? – J.Baoby Jan 10 '17 at 19:51
  • Sorry looks like it was my formatting in the computers.txt file – JacobElliott Jan 10 '17 at 20:05