Creating Script to report active computers

zebzz

Seniorius Lurkius
2
Hi,

I saw that there was a thread created to assist in creating a script that would generate a file showing if a computer has a ping response, I need some advice on how to now not only show that status but to keep the IP address that is associated with that computer.

This was created by quux :-

Code:
@echo off
for /f %%a in (computers.txt) do call :DOIT %%a
echo.
echo DONE!
goto :eof

:DOIT
set pingreply=
for /f "tokens=1" %%a in ('"ping -n 1 |findstr "Reply""') do set pingreply=%%a
if not defined pingreply (echo %1 NO PING REPLY >> tracker.txt) & goto :eof
echo %1 Ping fine >> tracker.txt
goto :eof

This script shows:-

ComputerName Ping fine
or
ComputerName NO PING REPLY

I would like to see:

ComputerName - IP Address Ping fine
or
ComputerName - <Blank> NO PING REPLY

Does anyone know how to add this into the script?
 

Whittey

Ars Tribunus Militum
2,026
Give this a go. You can also add in another if statement to add a FAIL or something in the event there is no name resolution.

Code:
@echo off
for /f %%a in (computers.txt) do call :DOIT %%a
echo.
echo DONE!
goto :eof

:DOIT
setlocal enabledelayedexpansion
set count=1
for /f "usebackq tokens=*" %%a in (`" ping -n 1 -w 10 %1 |findstr "^Pinging ^Request ^Reply not""`) do (
set reply!count!=%%a
set /a count=!count!+1)

:: IP grabbing and name resolution failure
for /f "usebackq tokens=3" %%a in (`echo "!reply1!"`) do if %%a==could (
set reply1=NAME_RES_FAIL
) ELSE (
set reply1=%%a)

:: Ping status
for /f "usebackq tokens=3" %%a in (`echo "!reply2!"`) do (
 if %%a==out.^" (set reply2=Timeout
 ) ELSE (
 set reply2=Success)
)

echo %1 - !reply1! - !reply2! >>tracker.txt
goto :eof