Pregunta

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?

¿Fue útil?

Solución

As a continuation of my comment above, that errorlevel cannot be trusted as a true indicator whether the ping worked because of how it is set when the ping returns Destination host unreachable. Here's an example of what I mean:

c:\>ping -n 1 192.168.0.238&echo ERRORLEVEL = !errorlevel!

Pinging 192.168.0.238 with 32 bytes of data:
Request timed out.

Ping statistics for 192.168.0.238:
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
ERRORLEVEL = 1

c:\>ping -n 1 192.168.0.238&echo ERRORLEVEL = !errorlevel!

Pinging 192.168.0.238 with 32 bytes of data:
Reply from x.x.x.x: Destination host unreachable.

Ping statistics for 192.168.0.238:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
ERRORLEVEL = 0

c:\>ping -n 1 192.168.0.238&echo ERRORLEVEL = !errorlevel!

Pinging 192.168.0.238 with 32 bytes of data:
Request timed out.

Ping statistics for 192.168.0.238:
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
ERRORLEVEL = 1

This code seems to work a lot better:

@echo off

setlocal enableextensions enabledelayedexpansion

REM checking the state of the current ip addres
set ipaddr=%1
set oldstate=neither

:loop
set state=down
for /f "skip=2 tokens=6 delims= " %%i in ('ping -n 1 !ipaddr!') do if "%%i"=="TTL=128" set state=up

if not !state!==!oldstate! (
    echo.Link is !state!
    set oldstate=!state!
)

ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop

endlocal

When I run checklink 192.168.0.238, I get link is down and it never toggles to up. When I run checklink 127.0.0.1, I get link is up.

Otros consejos

Try this:

@echo off
setlocal

set IPaddy=%~1

:loop
Call :IsPingable %IPaddy% && (echo %IPaddy% is up & exit /b) || (echo %IPaddy% is down & goto :loop)

:IsPingable <comp>
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul  
exit /b

Script updated to display current date and time when a change occurs and working with any TTL value (looks like original script checklink.cmd comes from another stackoverflow post).

NB: Proposition with bytes=50 was not working for all locales.

@echo off

setlocal enableextensions enabledelayedexpansion

REM checking the state of the current ip address
set ipaddr=%1
set oldstate=neither

if x!ipaddr!==x (
echo Missing ip address argument
goto :end
)

:loop
set state=down

for /f %%i in ('ping -n 1 !ipaddr! ^| findstr "TTL="') do (
set state=up)

if not !state!==!oldstate! (
    echo.Link is !state! at %date% %time:~0,2%:%time:~3,2%:%time:~6,2%
    set oldstate=!state!
)

ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop

:end
endlocal
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top