I have a batch file which is supposed to echo link is up
if the ip address is linked (pinging is successful) and should echo link is down
if not, for some reason if I type in in command prompt
checklink 192.168.0.238
which is not a linked address (suppose to get down
signal), I get first up
then I get the correct signal down
output is:
link is up
link is down
Here is the batch file:
@setlocal enableextensions enabledelayedexpansion
@echo off
REM checking the state of the current ip addres
set ipaddr=%1
set oldstate=neither
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
if not !state!==!oldstate! (
echo.Link is !state!
set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
My question why it does not work initially then it starts working?