batch-file

Batch file filtering out ping results


I have a script to continuously ping a set of machines (stored in SiteName.txt) and once a machine comes online, a counter increments, and the machine is removed from the list so it doesn't ping again (I just need to know if a machine has been online, not if it's on right now).

The issue I have is I've noticed there are a few phantom pings coming up with the IP address of the site they were built at (and that IP hasn't already been taken over by another machine) so I get false positives.

My current code is:

@echo off & setlocal EnableDelayedExpansion

TITLE %~n0

set /a counteron=0

for /F %%a in (%~n0.txt) do set "NVC=!NVC! %%a"

:ping
ping -n 61 127.0.0.1>nul
for %%i in (%NVC%) do (
    ping %%i -n 1 | find "TTL=" >nul 
    if !errorlevel! GEQ 1 (
        echo %%i is offline.
    ) else (
        set /a counteron+=1
        echo %%i is online
    set "NVC=!NVC: %%i=!"
    )
)

echo.
cls
echo. %counteron% machines are online.
if defined NVC goto :ping

cls
echo All machines in %~n0 are online.
pause

Is it possible to do the same thing, but if the IP's first 3 octets match a specific set (10.79.208.xxx), then it still comes up as offline?

Thanks in advance


Solution

  • Perhaps

    ping %%i -n 1 | find "TTL=" |findstr /b /l /v /c:"Reply from 10.79.208." >nul
    

    which should set errorlevel to zero only if both TTL= is found AND a line beginning (/b) with the /l literal string /c: "Reply from 10.79.208." is /v not found

    which I believe is your quest...