PTR Check: takes a host/IP and tests for a PTR record

If you have information, script, utility, or idea that can be useful for HostMonitor community, you welcome to share information in this forum.
Post Reply
terje
Posts: 88
Joined: Mon Jul 25, 2005 7:45 pm
Location: Sydney

PTR Check: takes a host/IP and tests for a PTR record

Post by terje »

We have created a script for testing whether a host/IP has a corresponding PTR record.

It takes one or two parameters.

The first parameter should be an IP address or a host name that can be resolved to an IP address.

If the IP address has a PTR record then it is compared with the original host name. If they match then the test is a success.

An optional second parameter can be used. If so then the PTR record must match this parameter for the test to be a success.

The script has been tested on a WINDOWS-2003 server running HOSTMONITOR v5.6beta.

The Hint for the script is: Params: <IP or Host> optional <Expected Result>
The start command for this script is: cmd /c %Script% %Params%

The body of the script is:-
@echo off
REM query host name and return a PTR record status

if "%1"=="" goto NOPARAMS

setlocal

set Hostname=%1
set hostfail=***
set FILE=%temp%\~ptrcheck.%1.tmp
set FILE2=%temp%\~ptrcheck2.%1.tmp

if "%2"=="" set Expected=%hostname% else set Expected=%2

nslookup -q=a %Hostname% > %FILE2% 2>&1

FOR /F "tokens=1" %%A IN ('type %FILE2%') DO CALL :CHECKHOST %%A

FOR /F "tokens=2" %%X IN ('type %FILE2% ^| find /n "Address" ^| find "[6]Address"') DO CALL :MAINLOOP %%X
FOR /F "tokens=2" %%X IN ('type %FILE2% ^| find /n "Address" ^| find "[5]Address"') DO CALL :MAINLOOP %%X

goto ALLDONE

:MAINLOOP

nslookup -q=ptr %1 > %FILE% 2>&1

FOR /F "tokens=4" %%B IN ('type %FILE% ^| FIND /I "name ="') DO CALL :OKRECORD %%B %EXPECTED%

echo ScriptRes:Bad:%1 has no PTR
goto ALLDONE


:CHECKHOST

IF NOT %1==%hostfail% GOTO :EOF

echo ScriptRes:Unknown:host not found
goto ALLDONE

:OKRECORD

echo we are here

if "%2"=="" goto GOTEXPECTED
if "%1"=="%2" goto GOTEXPECTED

echo ScriptRes:Bad:%1
goto ALLDONE

:GOTEXPECTED

echo ScriptRes:Ok:%1
goto ALLDONE

:NOPARAMS
echo ScriptRes:Unknown:not enough parameters
goto ALLDONE

:ALLDONE

del %FILE%
del %FILE2%
exit
Post Reply