ping - Batch check reachable machines -


i have used several scripts posted in forum unsuccesfully. script follows:

@echo off /f %%i in (computerstest.txt) (         ping -n 1 %%i | find "ttl" >nul 2>nul     echo %errorlevel% ) 

errorlevel 0 no matter if host reachable or not. if remove >nul see output following

reply 10.6.4.20: bytes=32 time<1ms ttl=64 0 0 reply 10.6.5.58: bytes=32 time<1ms ttl=126 0 

where second host in list not reachable errolevel 0

if execute in command prompt

ping -n 1 xxxxxxxxx | find "ttl" >nul 2>nul 

echo %errorlevel%

gives 1

i don't know doing wrong

tia ramon

batch lines or blocks of lines (lines enclosed in parenthesis) first parsed , executed. during parse phase, read operations on variables removed code, replaced value inside variable before starting execute line/block. so, if variable changed inside line/block, new value can not retrieved inside same line/block, there not read operation retrieve new value.

you can solve using delayed expansion , changing (where needed) syntax %var% !var!, indicate parser read operation must delayed until command executes

in case

@echo off setlocal enabledelayedexpansion /f %%i in (computerstest.txt) (     ping -n 1 %%i | find "ttl=" >nul 2>nul     echo %%i - !errorlevel! ) 

note: correct test in ipv4 response search ttl=, can have false positive in case of ttl expired error.

but in case of checking errorlevel variable, there more alternatives (if applicable).

you can use native if errorlevel n condition evaluated true errorlevel equal or greater n

@echo off /f %%i in (computerstest.txt) (     ping -n 1 %%i | find "ttl=" >nul 2>nul     if errorlevel 1 ( echo %%i offline ) else ( echo %%i online ) ) 

or can use conditional execution. operators && , || allow include execute depending on errorlevel (not set or set) previous command

@echo off /f %%i in (computerstest.txt) (     ( ping -n 1 %%i | find "ttl=" >nul 2>nul ) && echo %%i online || echo %%i offline ) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -