2

I am working on setting up a LAN ping test using a batch file. The code i have works great for websites but it acts strange for local IPs. I am running the ping test on 3 computers that i know the IPs of. No matter which one i unplug, when i run the code below, the %errorlevel% is always 0 on all three computers. It never equals to 1 like it does on a website. How can i resolve this?

@echo off
cls
Set IPaddress=www.google.com
PING %IPaddress% -n 1
 call :PingTest

Set IPaddress=www.yahoo.com
PING %IPaddress% -n 1
 call :PingTest

Set IPaddress=www.unabletoping.com
PING %IPaddress% -n 1
 call :PingTest

pause > null
exit

:PingTest
IF %errorlevel% EQU 1 (echo "Server is Offline") else (GOTO:EOF)
PY_
  • 1,189
  • 8
  • 18
  • 29
  • Does each computer on the LAN get assigned a static IP or is it dynamic? Is DHCP on? If so, I would recommend using the computer name rather than their IP address. `ping ` This way you are looking for that specific computer and not whatever device has been assigned that IP address. – David Ruhmann Jan 20 '14 at 23:04
  • Testing with dynamics, my scheduled one that will be running will be on statics. I wanted to use the IPs in case there is an issue with name resolution at any point in time. – PY_ Jan 21 '14 at 11:22

4 Answers4

20

When you ping an non accesible address in your subnet, you get an "unreachable" answer, with 1 packet sent, 1 packed received, 0 packets lost. Errorlevel is not set.

When you ping an non accesible address out of your subnet, you get a "timeout" answer, with 1 packet sent, 0 packet received, 1 packet lost. Errorlevel is set.

And, you can ping an active machine, lost packets and get an errorlevel

And, you can ping an active/inactive machine, get TTL expired and get no errorlevel

Better, check for content of ping response.

ping -n 1 192.168.1.1 | find "TTL=" >nul
if errorlevel 1 (
    echo host not reachable
) else (
    echo host reachable
)
MC ND
  • 69,615
  • 8
  • 84
  • 126
1

While I cannot replicate your issue, I do have a few recommendations for your script. (See my comment for questions regarding the issue)

  1. When creating variables encapsulate the scope. setlocal and endlocal
  2. When exiting a script, use the /b flag as to not kill the caller's command prompt.
  3. nul not null.

Example ():

@echo off
setlocal
cls

set "IPaddress=www.google.com"
call :PingVerbose "%IPaddress%"
call :PingVerbose "www.yahoo.com"
call :PingVerbose "www.microsoft.com"

pause>nul
endlocal
exit /b 0

:Ping <Address>
ping "%~1" -n 1 >nul
exit /b %ErrorLevel%

:PingVerbose <Address>
call :Ping %1 && echo %~1 is Online || echo %~1 is Offline
exit /b %ErrorLevel%
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • This is a very thoughtful response and should be upvoted. I would explain how the "functions" work a little bit more perhaps but maybe it's intuitive enough? – Mike Q Mar 27 '21 at 19:08
1

Though I also cannot replicate your issue, and too have a suggestion to better your script -

@echo off & cls

set addresses=10.1.1.666 10.124.412.14 10.7.254.1

for %%a in (%addresses%) do ping %%a -n 1 > nul || echo %%a is offline

Note that the command after || will only be executed if an error level is set by the ping.

unclemeat
  • 5,029
  • 5
  • 28
  • 52
0

Taking what others have mentioned, I wanted to show how one may need to do everything shown above plus the use of modified variables such as a counter in a loop.

Note: using "setlocal enabledelayedexpansion" allows the use of modified variables in a loop etc..

@echo off
setlocal enabledelayedexpansion

REM List of systems to check
set list=www.google.com www.yahoo.com www.notasite.com

set /a failed=0
set /a passed=0
set /a count=0

echo PingTest Servers on %list% :

(for %%a in (%list%) do ( 
    set /a "count+=1"
    call :PingVerbose %%a && set /a "passed=!passed!+1" || set /a "failed=!failed!+1"
))

echo ------------
echo Result: %passed% of %count% systems are pingable
pause
endlocal
exit /b 0

:Ping <Address>
ping "%~1" -n 1 >NUL
exit /b %ErrorLevel%

:PingVerbose <Address>
call :Ping %1 && echo %~1 - [ONLINE] || echo %~1 - [OFFLINE] 
exit /b %ErrorLevel%

Output:

PingTest Servers on www.google.com www.yahoo.com www.notasite.com :
www.google.com - [ONLINE]
www.yahoo.com - [ONLINE]
www.notasite.com - [OFFLINE]
------------
Result: 2 of 3 systems are pingable
Press any key to continue . . .         
Mike Q
  • 6,716
  • 5
  • 55
  • 62