Andrei Sevtsenko
Andrei Sevtsenko

Reputation: 223

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

Upvotes: 2

Views: 244

Answers (2)

soja
soja

Reputation: 1607

Perhaps this slight alteration to your script is what you're after.

@echo off & setlocal enabledelayedexpansion
set nvc=192.168.1.1
for %%i in (%NVC%) do (
    ping %%i -n 1 | find "TTL=" >nul 
    if !errorlevel! GEQ 1 (
        echo %%i is offline.
    ) else (
        for /f "tokens=1,2,3 delims=." %%j in ("%%i") do (
            set octets=%%j.%%k.%%l
            if "!octets!"=="10.79.208" (
                echo %%i is false positive
            ) else (
                set /a counteron+=1
                echo %%i is online
                set "NVC=!NVC: %%i=!"
            )
        )
    )
)

Upvotes: 0

Magoo
Magoo

Reputation: 80023

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...

Upvotes: 2

Related Questions