I am trying to automate DNS zone creation by using a batch file fired through one of Plesk's events.
Using the dnscmd command, the batch checks to see if the zone exists. If the zone does not exist, the script adds it according to specs. If it does exist, and it is a secondary zone, the script deletes and recreates it according to specs. If it exists, and it is a primary zone, the script leaves it alone. That part is all working.
Since we have some custom configurations, I also wanted to verify that if the zone is a secondary zone, it was also using the target server as the master. If it is using a different master, leave it alone. While I was able to retrieve the list of master servers, I was unable to match the text because of an odd problem with the output. Windows uses 0x0d,0x0a as the end-of-line marker, and the batch environment recognizes this. On this particular line of output, however, the end-of-line includes an additional 0x0d; the EOL is marked as 0x0d,0x0d,0x0a.
The problem section is after the :check3 label. I was receiving some weird feedback from the FOR /F loop, and added the echo commands to help debug. I eventually loaded the output of dnscmd directly into a hex editor to have a look. Using the algorithm shown in the script below, my test variables %%A and %%B retain the additional 0x0d, thus messing up my comparisons. The other lines I check from dnscmd do not show this issue - it is only with the output related to the MasterServers information. How can I resolve this?
The requirements: batch functionality only...we know we can recode this as VBScript and instantly resolve the issue, but that is not our goal here. The solution must not involve any other applications to parse the output from dnscmd.
@echo off
rem 1.2.3.4 = target server holding Plesk domains
rem 5.6.7.8 = our public nameservers
rem *** USER CONFIGURED VARIABLES
set dnsupdlog=test.log
set dnsupdip=1.2.3.4
rem *** other script variables (DO NOT MODIFY)
rem the next line is "set tab=<tab>%"
set tab= %
set nozone=%1
rem *** make sure a domain was provided
if "%1"=="" set nozone=**NO ZONE PROVIDED**
for /F "delims=" %%A IN ('date /T') DO SET dnsupdtime=%%A
echo --------%dnsupdtime% begin zone %nozone% > %dnsupdlog%
if "%nozone%"=="**NO ZONE PROVIDED**" (
echo You must provide a domain name, e.g., test.bat mydomain.com >> %dnsupdlog%
goto :endit
)
rem *** does domain exist yet? if not, just add the domain
"%plesk_bin%\dnscmd.exe" 5.6.7.8 /zoneinfo %1 | find "query failed" > NUL
if ERRORLEVEL 1 (
echo Zone exists ... continue checking >> %dnsupdlog%
goto :check2
)
echo Zone does not exist ... add domains >> %dnsupdlog%
goto :add_domains
:check2
rem *** domain already exists. Is it primary? if yes, skip
for /F "tokens=1-2 skip=1 delims=%tab%: " %%A IN ('"%plesk_bin%\dnscmd.exe" 5.6.7.8 /zoneinfo %1 Type') DO (
if "%%A"=="Dword" (if "%%B"=="1" (
echo Domain is a primary zone. No work to be done. >> %dnsupdlog%
goto :endit
)
echo Not a primary zone ... continue checking >> %dnsupdlog%
goto :check3
)
)
echo ***ERROR*** Could not determine zone type!! >> %dnsupdlog%
goto :endit
:check3
rem *** secondary domain exists. Is it using this as master? if not, skip
set isfound=no
for /F "skip=1 tokens=2-3 delims=%tab%=> " %%A IN ('"%plesk_bin%\dnscmd.exe" 5.6.7.8 /zoneinfo %1 MasterServers') DO (
echo %%A
echo %%B
echo %%A,
echo %%B,
if /i "%%A"=="count" (if /i "%%B" NEQ "1" (
echo Received unexpected master server count %%B!! >> %dnsupdlog%
goto :endit
)
)
if /i "%%A"=="Addr[0]" (
if /i "%%B" NEQ "%dnsupdip%" (
echo Different master server %%B. No work to be done. >> %dnsupdlog%
goto :endit
)
set isfound=yes
)
)
if /i "%isfound%" NEQ "yes" (
echo Did not find expected IP %dnsupdip% as master server. No work to be done. >> %dnsupdlog%
goto :endit
)
:del_domains
rem *** delete domains here
echo del >> %dnsupdlog%
:add_domains
rem *** add domains here
echo add >> %dnsupdlog%
:endit
echo --------%dnsupdtime% end zone %nozone% >> %dnsupdlog%
set isfound=
set nozone=
set dnsupdtime=
set dnsupdlog=
set dnsupdip=