Need a simple bat file to test ping on a list

March 21, 2011 at 11:01:25
Specs: Windows 7
I have a list of 300 machines I need to test and see if I can get a ping back from them. Does anybody have a simple bat file or script that I can dump these machine names into and have it outout to a txt file if it was able to ping or not?

See More: Need a simple bat file to test ping on a list

Report •


#1
March 21, 2011 at 11:29:24
Not a simple one but...

@echo off
setlocal ENABLEEXTENSIONS

set OutputFile=result.txt
set ListFile=servers.txt
echo y|del %OutputFile%

for /f "tokens=1,2 delims= " %%a in (%ListFile%) do (
  call :Sub "%%a" "%%b"
  echo Checking "%%a" "%%b")
notepad "%OutputFile%"
goto :eof

:Sub
  ping %1 -n 1 > NUL
  if ERRORLEVEL 1 (
    set state=Off Line
    set name=%1
    set IP=0.0.0.0
  ) else (
    set Name=%2
    set state=On Line
    call :RDNS %1
  )
  echo %Name%,%state%   %IP%,%2 >> "%OutputFile%"
goto :eof

:RDNS
 for /f "tokens=1,2 delims= " %%A in ('nslookup %1') do (
  if "%%A"=="Name:" call :SetName "%%B"
  if "%%A"=="Address:" call :SetIP "%%B"
  if "%%A"=="Addresses:" call :SetIP "%%B")
goto :eof

:SetIP
  set IP=%1
goto :eof
:SetName
  set Name=%1
goto :eof

In servers.txt you can place IPs or FQDN along with descriptions...

10.100.100.8 File Server
10.100.100.7 SQL Server
10.100.100.9 DC
DESKTOP-02094 JohnDoe
DESKTOP-02095 JaneDoe

and the results in the result.txt file would look like...

svr3.mydomain.local,On Line   10.100.100.8,File Server 
svr2.mydomain.local,On Line   10.100.100.7,SQL Server
svr1.mydomain.local,On Line   10.100.100.9,DC 
DESKTOP-02094,Off Line   0.0.0.0,JohnDoe
DESKTOP-02095,On Line   10.100.100.55,JaneDoe

You can play with this line to change the order or remove things you don't want...

echo %Name%,%state%   %IP%,%2 >> "%OutputFile%"


Report •

#2
March 21, 2011 at 12:22:54
If you plan on doing this from a Win7 box, PowerShell makes this fairly trivial:
Get-Content serverList.txt | Where-Object {-not (Test-Connection $_ -Count 4 `
  -ErrorAction SilentlyContinue)} > noPing.txt

How To Ask Questions The Smart Way


Report •

#3
March 21, 2011 at 13:09:23
These work!


Thank you.


Report •

Related Solutions


Ask Question