Ping A List Of Computers and Double Check DNS name against NetBios name

I’ve seen in the past where environments use Dynamic DNS to update clients, where due to bad configuration the DNS on clients gets out of whack, especially if many users moving sites/or PCs replaced. In any case if you have Windows machines with Workstation service running, this batch file will log PC names, IP address recorded, Ping Success/fail, NetBios sucesss/fail, NetBios name. To use this simply place a list of computers in a text file called computers.txt, each computer separated by a line. For the NetBios check to work you should not use fully qualified domain names. Additionally the script will run multiple times, the default is configured to five runs. Each time it runs again it will only re-scan machines that failed, this way you can capture machines that are not on the network during initial run, but maybe came online later. The output goes into a file called PC_LOG.csv

image

The output as below:

image

The script is available here:

https://skydrive.live.com/redir?resid=E1A3C870740A073D!543&authkey=!AJiJEiYYIz2iQGc

Or copy the text below


@echo off
Rem pingPCs by malcolm mccaffery - pings all PCs in a text file and logs results to CSV
REM enabled delayed expansion so we can change variables during batch file execution
SETLOCAL ENABLEDELAYEDEXPANSION

Rem set the maximum number of runs...
SET MAXRUNS=5

REM the output CSV file
SET OUTPUTFILE=PC_LOG.csv

REM set input file, which is a list of computer names in an ASCII text file, each computer name on a new line
SET INPUTFILE=computers.txt

REM create the header row for the CSV file
echo Computer Name,IP Address,Ping Status,NetBios Name,NetBios Status,Date Time > "%OUTPUTFILE%"

echo Reading computer list from %INPUTFILE%
echo Saving log to %OUTPUTFILE%
FOR /F %%i IN (%INPUTFILE%) DO (
echo Pinging %%i...
ping %%i > "%temp%\ping.txt"

IF !ERRORLEVEL! EQU 0 (
REM machine is pingable!
SET result=SUCCESS

REM we filter by 'ping statistics' to get IP because it's easy to process,
REM whether IPv6 or IPv4 it's consistent across legacy and lates MS OSes
REM dummy delims \ as we don't want it separating any of the output
FOR /F "delims=\" %%j IN ('type ^"%temp%\ping.txt^" ^| find ^"Ping statistics^"') DO (
SET IPAddress=%%j
SET IPAddress=!IPAddress:~20,-1!
echo SUCCESS ... !IPAddress!
)
) ELSE (
REM machine is not pingable!
SET RESULT=FAILED

REM Assume guilty until proven innocent
SET IPAddress=NOT FOUND
REM did we get IP, but machine not online?
type "%temp%\ping.txt" | find "Request timed out."
IF !ERRORLEVEL! EQU 0 (
FOR /F "delims=\" %%j IN ('type ^"%temp%\ping.txt^" ^| find ^"Ping statistics^"') DO (
SET IPAddress=%%j
SET IPAddress=!IPAddress:~20,-1!
echo FAILED ... !IPAddress!
)
) ELSE (
echo FAILED ... NOT FOUND
)
)

Rem Assume guility until proven innocent
SET NETBIOSNAME=NOT FOUND
SET NETBIOSRESULT=FAILED
IF "!RESULT!" EQU "SUCCESS" (
Rem double check computer name with NetBIOS workstation service...
echo Checking NetBIOS workstation service on %%i
nbtstat -a "%%i" | find "<00>" | find "UNIQUE" > "%temp%\nbtstat.txt"
IF !ERRORLEVEL! EQU 0 (
FOR /F %%j IN ('type ^"%temp%\nbtstat.txt^"') DO (
SET NETBIOSNAME=%%j
)

Rem Check Netbios Name is a Match
IF /I "!NETBIOSNAME!" EQU "%%i" SET NETBIOSRESULT=SUCCESS
)
)
echo NETBIOS NAME: !NETBIOSNAME!
echo %%i,!IPAddress!,!result!,!NETBIOSNAME!,!NETBIOSRESULT!,%DATE%%TIME% >> "%OUTPUTFILE%"
)

SET COUNTER=0

:REPEAT

SET /A COUNTER=!COUNTER!+1

echo Completed Cycle #!COUNTER!. Output in "%OUTPUTFILE%"
IF !COUNTER! GEQ !MAXRUNS! GOTO :EOF

Rem Allows for conditional operations in batch processing.
IF EXIST "%INPUTFILE%.csv" DEL "%INPUTFILE%.csv"
copy "%OUTPUTFILE%" "%INPUTFILE%.csv" /y
IF EXIST "%OUTPUTFILE%.tmp" DEL "%OUTPUTFILE%.tmp"
Rem Allows for conditional operations in batch processing.
FOR /F "delims=, TOKENS=1,2,3,4,5,6,7" %%i IN (%INPUTFILE%.csv) DO (
Rem only retry failed machines...
SET RETRY=FALSE

Rem if ping failed, retry
IF "%%k" EQU "FAILED" SET RETRY=TRUE

Rem if netbios failed, retry
IF "%%m" EQU "FAILED" SET RETRY=TRUE

IF "!RETRY!" EQU "TRUE" (
Rem Allows for conditional operations in batch processing.
echo Pinging %%i...
ping %%i > "%temp%\ping.txt"

IF !ERRORLEVEL! EQU 0 (
REM machine is pingable!
SET result=SUCCESS

REM we filter by 'ping statistics' to get IP because it's easy to process,
REM whether IPv6 or IPv4 it's consistent across legacy and lates MS OSes
REM dummy delims \ as we don't want it separating any of the output
FOR /F "delims=\" %%x IN ('type ^"%temp%\ping.txt^" ^| find ^"Ping statistics^"') DO (
SET IPAddress=%%x
SET IPAddress=!IPAddress:~20,-1!
echo SUCCESS ... !IPAddress!
)
) ELSE (
REM machine is not pingable!
SET RESULT=FAILED

REM Assume guilty until proven innocent
SET IPAddress=NOT FOUND

REM did we get IP, but machine not online?
type "%temp%\ping.txt" | find "Request timed out."
IF !ERRORLEVEL! EQU 0 (
FOR /F "delims=\" %%x IN ('type ^"%temp%\ping.txt^" ^| find ^"Ping statistics^"') DO (
SET IPAddress=%%x
SET IPAddress=!IPAddress:~20,-1!
echo FAILED ... !IPAddress!
)
) ELSE (
echo FAILED ... NOT FOUND
)
)

Rem Assume guility until proven innocent
SET NETBIOSNAME=NOT FOUND
SET NETBIOSRESULT=FAILED
IF "!RESULT!" EQU "SUCCESS" (
Rem double check computer name with NetBIOS workstation service...
echo Checking NetBIOS workstation service on %%i
nbtstat -a "%%i" | find "<00>" | find "UNIQUE" > "%temp%\nbtstat.txt"
IF !ERRORLEVEL! EQU 0 (
FOR /F %%x IN ('type ^"%temp%\nbtstat.txt^"') DO (
SET NETBIOSNAME=%%x
)

Rem Check Netbios Name is a Match
IF /I "!NETBIOSNAME!" EQU "%%i" SET NETBIOSRESULT=SUCCESS
)
)
echo NETBIOS NAME: !NETBIOSNAME!
echo %%i,!IPAddress!,!result!,!NETBIOSNAME!,!NETBIOSRESULT!,%DATE%%TIME% >> "%OUTPUTFILE%.tmp"
) ELSE (
echo %%i was SUCCESSFUL in previous test
echo %%i,%%j,%%k,%%l,%%m,%%n >> "%OUTPUTFILE%.tmp"
)
)

IF EXIST %OUTPUTFILE% del %OUTPUTFILE%
rename %OUTPUTFILE%.tmp %OUTPUTFILE%

GOTO :REPEAT

 

 

 

About chentiangemalc

specializes in end-user computing technologies. disclaimer 1) use at your own risk. test any solution in your environment. if you do not understand the impact/consequences of what you're doing please stop, and ask advice from somebody who does. 2) views are my own at the time of posting and do not necessarily represent my current view or the view of my employer and family members/relatives. 3) over the years Microsoft/Citrix/VMWare have given me a few free shirts, pens, paper notebooks/etc. despite these gifts i will try to remain unbiased.
This entry was posted in Batch Files, Network and tagged . Bookmark the permalink.

7 Responses to Ping A List Of Computers and Double Check DNS name against NetBios name

  1. Pogo says:

    I was trying to copy this but the displayed code uses unicode characters that make it rather painful to convert. The link to the zip file no longer exists. This is exactly what I have been looking to accomplish but far more functional than I have to date. Any assistance you can provide in getting a copy of this script would be much appreciated. thank you.

  2. Joe says:

    Great idea.. but it doesn’t work… too bad.

  3. troystarkey says:

    Amazing, thanks! worked a treat.

    Create a txt file from your AD dump, delete all the extra rubbish, but keep the PC names, one on each line. Save as “Computers.txt” as the batch file searches for that as the input file. Run file. Could not be easier, thanks again.

    Troy.

  4. Blake says:

    Works great just place your computers.txt in the same folder you extracted and you are good to go.

  5. Les says:

    Many thanks, appreciated you sharing this and worked fine!

  6. Ray says:

    Any possible way to use NSLOOKUP instead of nbtstat?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s