@echo off cls ::REM ***************************************************** ::REM B a t c h P r o g r a m m i n g S a m p l e ::REM from http://www.japan-page.net ::REM (C) 2003 John D. Seaman ::REM Copylefted, and released to the general Internet ::REM community, you may use, modify and release this ::REM at will as long as you retain this header. ::REM ***************************************************** ::REM Windows 2000 DNS KCC utility v.0.02 2003.2.22 ::REM What this script does: ::REM + Compares the number of DNS zones on your W2K DNS servers and alerts ::REM when they are out of synch. This can be useful in a large ::REM distributed environment when you have both primary and ::REM secondary DNS servers and multiple regional admins. ::REM + Reports and alerts on any zones found shut down. ::REM + Shows zones missing on inconsistent servers. ::REM Known Limitations ::REM You'll have to edit the script to change the number of DNS ::REM servers you want to monitor. I thought about creating loops ::REM but it's not worth the trouble. ::REM Requires diff.exe from the freeware GNU utilities for Win32 ::REM http://www.weihenstephan.de/~syring/win32/UnxUtils.html ::REM Uses postie.exe SMTP mailer from ::REM http://www.infradig.com/postie/index.shtml ::REM Substitute Blat or another mailer if needed. :REM --------------- S E T -- V A R I A B L E S --------------- ::REM Set target DNS servers here set _dnsServer1=192.168.0.100 set _dnsServer2=192.168.0.200 set _dnsServer3=192.168.0.220 set _dnsServer4=192.168.0.221 del *.txt /q >nul set _log=dns-kcc.log ::REM Set SMTP variables set _hst=my.smtp.server.com set _too=me@server.com set _frm=%COMPUTERNAME%-DNS-KCC@server.com :REM --------------- E N D -- V A R I A B L E S --------------- ::REM Initialize variables set _zonedown=0 set _outtasynch=0 ::REM Prepare log echo.>%_log% echo DNS KCC running on %computername% on %date% at %time% echo DNS KCC running on %computername% on %date% at %time%>>%_log% echo ------------------------------------------------------------ echo ------------------------------------------------------------>>%_log% echo. echo.>>%_log% ::REM Get a count of zones on each server dnscmd %_dnsServer1% /enumZones | find "Up" /c >_dns1.txt dnscmd %_dnsServer2% /enumZones | find "Up" /c >_dns2.txt dnscmd %_dnsServer3% /enumZones | find "Up" /c >_dns3.txt dnscmd %_dnsServer4% /enumZones | find "Up" /c >_dns4.txt ::REM Get count into a variable type _dns1.txt| t append 'set _dns1count=' b > dns1.bat type _dns2.txt| t append 'set _dns2count=' b > dns2.bat type _dns3.txt| t append 'set _dns3count=' b > dns3.bat type _dns4.txt| t append 'set _dns4count=' b > dns4.bat ::REM Get all zones on master DNS server for comparison dnscmd %_dnsServer1% /enumZones > _dns1zones.txt ::REM Cull out header ::REM Extract automatically created zones (127, ".", broadcast, etc). ::REM Yank "command completed successfully" text type _dns1zones.txt | t cull 'Enumerated ' ' . ' | t excl ' Auto ' 1 100 | t excl 'Command' 1 100 > _dns1a.txt ::REM Run a for loop, extract the zones only for /f "tokens=1,2*" %%i in (_dns1a.txt) do echo %%i >>_dns1b.txt type _dns1b.txt | t clean >_dns1c.txt del /q _dns1a.txt _dns1b.txt _dns1.txt ::REM Alert if any zones "Shutdn" type _dns1zones.txt | find "Shutdn" >_shutdown.txt if /i %errorlevel% EQU 0 ( echo Alert! Zones shut down! echo Alert! Zones shut down! >>%_log% type _shutdown.txt type _shutdown.txt >>%_log% set _zonedown=1 ) del /q _dns1zones.txt ::REM Get all zones on all other servers ::REM ------------- D N S 2 ------------- dnscmd %_dnsServer2% /enumZones > _dns2zones.txt type _dns2zones.txt | t cull 'Enumerated ' ' . ' | t excl ' Auto ' 1 100 | t excl 'Command' 1 100 > _dns2a.txt ::REM Run a for loop, extract the zones only for /f "tokens=1,2*" %%i in (_dns2a.txt) do echo %%i >>_dns2b.txt type _dns2b.txt | t clean >_dns2c.txt del /q _dns2a.txt _dns2b.txt _dns2.txt ::REM Alert if any zones "Shutdn" type _dns2zones.txt | find "Shutdn" >_shutdown.txt if /i %errorlevel% EQU 0 ( echo Alert! Zones shut down! echo Alert! Zones shut down! >>%_log% type _shutdown.txt type _shutdown.txt >>%_log% set _zonedown=1 ) del /q _dns2zones.txt ::REM ------------- D N S 3 ------------- dnscmd %_dnsServer3% /enumZones > _dns3zones.txt type _dns3zones.txt | t cull 'Enumerated ' ' . ' | t excl ' Auto ' 1 100 | t excl 'Command' 1 100 > _dns3a.txt ::REM Run a for loop, extract the zones only for /f "tokens=1,2*" %%i in (_dns3a.txt) do echo %%i >>_dns3b.txt type _dns3b.txt | t clean >_dns3c.txt del /q _dns3a.txt _dns3b.txt _dns3.txt ::REM Alert if any zones "Shutdn" type _dns3zones.txt | find "Shutdn" >_shutdown.txt if /i %errorlevel% EQU 0 ( echo Alert! Zones shut down! echo Alert! Zones shut down! >>%_log% type _shutdown.txt type _shutdown.txt >>%_log% set _zonedown=1 ) del /q _dns3zones.txt ::REM ------------- D N S 4 ------------- dnscmd %_dnsServer4% /enumZones > _dns4zones.txt type _dns4zones.txt | t cull 'Enumerated ' ' . ' | t excl ' Auto ' 1 100 | t excl 'Command' 1 100 > _dns4a.txt ::REM Run a for loop, extract the zones only for /f "tokens=1,2*" %%i in (_dns4a.txt) do echo %%i >>_dns4b.txt type _dns4b.txt | t clean >_dns4c.txt del /q _dns4a.txt _dns4b.txt _dns4.txt ::REM Alert if any zones "Shutdn" type _dns4zones.txt | find "Shutdn" >_shutdown.txt if /i %errorlevel% EQU 0 ( echo Alert! Zones shut down! echo Alert! Zones shut down! >>%_log% type _shutdown.txt type _shutdown.txt >>%_log% set _zonedown=1 ) del /q _dns4zones.txt ::REM Logic - All DNS servers should have the same number of zones as the master, ::REM though the types of zones (primary / secondary) will vary. ::REM Get zone count into a variable call dns1.bat call dns2.bat call dns3.bat call dns4.bat del /q dns1.bat dns2.bat dns3.bat dns4.bat echo DNSserver1 (%_dnsServer1%) has %_dns1count% zones echo DNSserver2 (%_dnsServer2%) has %_dns2count% zones echo DNSserver3 (%_dnsServer3%) has %_dns3count% zones echo DNSserver4 (%_dnsServer4%) has %_dns4count% zones echo DNSserver1 (%_dnsServer1%) has %_dns1count% zones >>%_log% echo DNSserver2 (%_dnsServer2%) has %_dns2count% zones >>%_log% echo DNSserver3 (%_dnsServer3%) has %_dns3count% zones >>%_log% echo DNSserver4 (%_dnsServer4%) has %_dns4count% zones >>%_log% echo. echo.>>%_log% if /i not %_dns2count% EQU %_dns1count% ( echo *** DNS server 2 not in synch! Missing zones follow: *** echo *** DNS server 2 not in synch! Missing zones follow: ***>>%_log% diff -i -w _dns1c.txt _dns2c.txt >_delta2.txt set _outtasynch=1 type _delta2.txt | find "." type _delta3.txt | find "." >>%_log% echo. echo.>>%_log% ) if /i not %_dns3count% EQU %_dns1count% ( echo *** DNS server 3 not in synch! Missing zones follow: *** echo *** DNS server 3 not in synch! Missing zones follow: ***>>%_log% diff -i -w _dns1c.txt _dns3c.txt >_delta3.txt set _outtasynch=1 type _delta3.txt | find "." type _delta3.txt | find "." >>%_log% echo. echo.>>%_log% ) if /i not %_dns4count% EQU %_dns1count% ( echo *** DNS server 4 not in synch! Missing zones follow: *** echo *** DNS server 4 not in synch! Missing zones follow: ***>>%_log% diff -i -w _dns1c.txt _dns4c.txt >_delta4.txt set _outtasynch=1 type _delta4.txt | find "." type _delta4.txt | find "." >>%_log% echo. echo.>>%_log% ) ::REM Blast SMTP alert if any zones shutdown, or outta sync if /i %_zonedown% EQU "1" ( set _sbj="One or more DNS zone(s) found shutdown! Check the log file." postie -to:%_too% -from:%_frm% -host:%_hst% -s:%_sbj% -nomsg -file:%_log% -high ) if /i %_outtasynch% EQU 1 ( postie -to:%_too% -from:%_frm% -host:%_hst% -s:"DNS consistency error detected!" -msg:%_sbj% -file:%_log% -high ) ::REM Cleanup del /q *.txt >nul :EOF