我正在尝试使用通过 Plesk 的一个事件触发的批处理文件来自动创建 DNS 区域。
使用 dnscmd 命令,批处理检查区域是否存在。如果该区域不存在,脚本将根据规范添加它。如果它确实存在,并且它是辅助区域,则脚本会根据规范删除并重新创建它。如果它存在,并且它是一个主要区域,则脚本将不理会它。那部分一切正常。
由于我们有一些自定义配置,我还想验证该区域是否是辅助区域,它也使用目标服务器作为主服务器。如果它正在使用不同的主服务器,请不要理会它。虽然我能够检索主服务器列表,但由于输出存在奇怪问题,我无法匹配文本。Windows 使用 0x0d,0x0a 作为行尾标记,批处理环境可以识别这一点。然而,在这个特定的输出行上,行尾包含一个额外的 0x0d;EOL 标记为 0x0d,0x0d,0x0a。
问题部分在 :check3 标签之后。我从 FOR /F 循环中收到了一些奇怪的反馈,并添加了 echo 命令来帮助调试。我最终将 dnscmd 的输出直接加载到十六进制编辑器中进行查看。使用下面脚本中显示的算法,我的测试变量 %%A 和 %%B 保留了额外的 0x0d,从而打乱了我的比较。我从 dnscmd 检查的其他行没有显示此问题 - 它仅与与 MasterServers 信息相关的输出有关。我该如何解决这个问题?
要求:仅批处理功能......我们知道我们可以将其重新编码为 VBScript 并立即解决问题,但这不是我们的目标。该解决方案不得涉及任何其他应用程序来解析来自 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=