Home > front end >  batch code to list ip addressess that do not ping
batch code to list ip addressess that do not ping

Time:06-17

I am using the code below to list ip addresses from a list of computer names on list.txt, but they have to be pinging to list. How can I edit the code to show these IP addressess that do not ping also?

@echo off

Echo Pinging list...

set ComputerList=list.txt

Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion

for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Results.csv
))

pause

list.txt contains the following:

enter image description here

and

Results.csv will populate

enter image description here

but it will not populate IP address for PCWINDATA103 because it is not pinging but I know an IP exists

CodePudding user response:

Force a custom output in case findstr doesn't find Reply (take care of "tokens=3"):

...
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
  for /f "tokens=3 delims=: " %%B in ('ping -4 -n 1 -l 1 %%A ^|findstr "Reply" ^|^|echo x x offline') do (
    echo %%A,%%B
  )
)
  • Related