IP batch ping

February 22, 2010 at 07:10:33
Specs: Windows XP
I am trying to ping a list of IP addresses using a batch script and get the results back in a text file. But the results i get aren't correct, the code is most likely wrong...These are the steps i took:

1. I created a txt file clled "hosts" which has the listing of over 1000 IP's that have to be pinged

2. I then created a batch file called "pinghosts" and the code is as follows:

if errorlevel==1 goto notgood
if errorlevel==0 goto good
echo %1 is not pingable >> ping.log
goto end
echo %1 is good >> ping.log
goto end
echo ---------------------- >> ping.log

3. on command prompt i changed the directory to where "hosts" and "pinghosts" are located (desktop)

4. I then entered in command prompt the following code to run it:

for /f %z in (hosts.txt) do pinghosts.bat %%z

When i get the results on the text file called "ping" all the IP's comeback pingable:

%[IP ADDRESS] is good

I know these results aren't correct because when i ping them manually some of them are not pingable

Please any suggestions to the code would be helpful, thanx.

See More: IP batch ping

Report •

February 22, 2010 at 08:15:29
i don't think ping "errors" intuitively. (ie: errorlevel is zero whether ping went through or not).
you prob'ly need to gummy it up somewhat:

@echo off
for /f %%a in (hosts) do (
for /f "tokens=10* delims= " %%b in ('ping -n 1 -w 3 %%a ^|find " Lost = "') do (
if %%b equ 1 (set xx=not) else (set xx=)
>> logfile echo %%a %%b %xx% ok

Report •

February 23, 2010 at 03:05:47
The errorlevel check you are doing is wrong, the syntax is different ... and also : DONT PUT ANY COMMAND BETWEEN THE BEHOLDED COMMAND, AND THE ERRORLEVEL CHECK, because errorlevel refers to the LAST command executed, before you do errorlevel-checks. (Example: in your code, you are checking the errorlevel of the ECHO command)

Nbrane: PING does set errorlevel correctly, no problem!

D:\Temp>ping www.idontexistdoi.com
Ping request could not find host www.idontexistdoi.com. Please check the name an
d try again.

D:\Temp>if errorlevel 1 echo NO

D:\Temp>ping www.google.com

Pinging www.l.google.com [] with 32 bytes of data:

Reply from bytes=32 time=19ms TTL=51
Reply from bytes=32 time=19ms TTL=51
Reply from bytes=32 time=19ms TTL=51
Reply from bytes=32 time=17ms TTL=51

Ping statistics for
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 17ms, Maximum = 19ms, Average = 18ms

D:\Temp>if errorlevel 1 echo NO


Report •

February 23, 2010 at 03:23:01

Ping returns non-zero only if it fails to resolve host.

Helping others achieve escape felicity


Report •

Related Solutions

February 25, 2010 at 05:24:34
Thank you guys for your valiant efforts. I will try these new strategies, i appreciate your support.

Report •

Ask Question