VMware SRM DNS Script failover example

This is the dns_update.cmd script:



@REM ------------------------------------------------------------------------

@REM VMware Inc, 2013

@REM

@REM WinXP/Win2k script for DNS record updates on

@REM Microsoft DNS or BIND Server

@REM ------------------------------------------------------------------------

@REM By using this script, you agree to the following.

@REM If you do not agree with the following, do not use the script.

@REM This script is a sample showing how to update DNS records during

@REM protected site failover/failback workflows. This script is not supported

@REM under any VMware support program or service.

@REM THIS SCRIPT IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND.

@REM VMWARE FURTHER DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING, WITHOUT

@REM LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT,

@REM OR OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK ARISING OUT OF

@REM THE USE OR PERFORMANCE OF THIS SAMPLE SCRIPT REMAINS WITH YOU. IN NO

@REM EVENT SHALL VMWARE, ITS AUTHORS, OR ANYONE ELSE INVOLVED IN THE CREATION,

@REM PRODUCTION, OR DELIVERY OF THIS SCRIPT BE LIABLE FOR ANY DAMAGES

@REM WHATSOEVER (INCLUDING, WITHOUT LIMITATION, CONSEQUENTIAL DAMAGES,

@REM INDIRECT DAMAGES, DIRECT DAMAGES, INCIDENTAL DAMAGES, OR DAMAGES FOR LOSS

@REM OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION,

@REM OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE

@REM THIS SCRIPT OR DOCUMENTATION, EVEN IF VMWARE HAS BEEN ADVISED OF THE

@REM POSSIBILITY OF SUCH DAMAGES.

@REM ------------------------------------------------------------------------

@ECHO OFF




REM -------------------------------------------------------------------------

REM ------------------------------ !IMPORTANT! ------------------------------

REM -------------------------------------------------------------------------

REM PLEASE, DO NOT EDIT THIS SCRIPT!

REM USE THE COMMAND LINE PARAMETERS TO CONTROL ITS OPERATION!

REM -------------------------------------------------------------------------

REM ------------------------------ !IMPORTANT! ------------------------------

REM -------------------------------------------------------------------------




REM -------------------------------------------------------------------------

REM                             Internal Constants

REM -------------------------------------------------------------------------

SETLOCAL ENABLEDELAYEDEXPANSION

REM Changes in this version:

REM - Added support for IPv6. Verification for IPv6 address and reverse zone

REM   is not implemented, because zone address represented in octal number,

REM   IPv6 address represented in decimal.

SET VERSION=1.1.0

SET /A EXITCODE=0

SET ADDR-SEP=.

SET IP-SUFF=in-addr.arpa

SET IPv6-SUFF=ip6.arpa

SET /A TTL=86400

SET ZONE-NONE=NONE

REM Setup temporary random filename

FOR /F "tokens=1-4 delims=:,." %%A IN ("%TIME%") DO (

  SET RANDOM_PREFIX=%TEMP%\%%B_%%C_%%D_%RANDOM%_

)

SET BIND-CMD-FILE=%RANDOM_PREFIX%bind-cmd.txt

SET DNSCMD=dnscmd.exe

SET PSEXECCMD=psexec.exe -i /accepteula

REM The nsupdate command line

REM (please see http://www.isc.org/sw/bind/arm95/Bv9ARM-all.html)

REM The nsupdate program supports TSIG via the -k and -y

REM command line options or inline by use of the key.

REM   e.g. nsupdate -k cert.key ns.commands

SET NSUPDATECMD=nsupdate.exe

REM Number of tries to launch the nsupdate command

SET /A NUMBER_OF_NSUPDATE_RETRIES=15

SET /A STEPNUM=0

SET WRONG_USAGE=WRONG COMMAND USAGE

SET SKIPMODE=0

SET /A DNS_ERROR_NAME_DOES_NOT_EXIST=9714


IF [%1]==[] (

  ECHO.

  ECHO %WRONG_USAGE%: No parameters supplied

  GOTO :COMMAND_HELP

)

REM -------------------------------------------------------------------------

REM                        DNS Update Parameters

REM -------------------------------------------------------------------------

REM    - DNS-SERV   Hostname or IP address of the DNS server to update

REM    - DNS-SERV-USER,    Windows DNS username/password. These

REM      DNS-SERV-PASS     are needed ONLY in case the user

REM                        running this script (usually the

REM                        SRM server's account) does not have

REM                        enough privileges to update the DNS

REM                        server.

REM    - DNS_SERVER_TYPE   Denotes the type of the DNS server:

REM                        1 for Microsoft Windows DNS and

REM                        2 for BIND

REM                        3 try both (Windows first)

REM    - DNS_COMMAND       Denotes the command of the DNS server:

REM                        1 for add forward and reverse mapping

REM                        2 for delete forward and reverse mapping

REM    - FORWARD-ZONE      specifies the forward lookup zone name.

REM    - REVERSE-ZONE      specifies the reverse lookup zone.

REM                        NOTE: the zone address is not in reversed format.

REM                        NOTE: specify NONE for this column so that reverse

REM                              zone update is not performed. In this case the

REM                              IP part (column D) must contain entire IP.

REM    - REVERSE-ZONE-V6   specifies the IPv6 reverse lookup zone.

REM                        NOTE: the zone address is not in reversed format.

REM    - REVERSE-ZONE-V6-REV specifies the IPv6 reverse lookup zone in reversed format.

REM    - HOST-NAME column denotes the hostname part of new DNS record.

REM    - HOST-IP column specifies the IP part of the new DNS record. Multiple IP

REM        addresses per hostname are allowed - separate them with commas.

REM        The values in this column are appended to the zone address

REM        specified in column (B) in order to form the complete IP address.

REM    - HOST-IPV6-REV     specifies the IPv6 address in reverse order format.

REM                        this is required for Bind DNS

REM    - HOST-IPV6-REV-MSDNS specifies the local part of IPv6 address

REM                           (without reverse IPv6 zone) in reverse order format

REM                           this is required for MS DNS

REM    - FORWARD-KEY specifies the forward lookup zone shared secret for BIND

REM                in the format <keyname>:<secret>

REM                NOTE: when it is empty the shared secret is not used.

REM    - REVERSE-KEY specifies the reverse lookup zone shared secret for BIND

REM                in the format <keyname>:<secret>.

REM                NOTE: when it is empty the (E) column is used.

REM    - REVERSE-KEY-V6 specifies the IPv6 reverse lookup zone shared secret for BIND

REM                in the format <keyname>:<secret>.

REM    - TOOLSPATH   Tool paths which will be appended to

REM                  the PATH variable.

REM                  NOTE:

REM                  1. Windows DNS requires the paths to PSExec.exe and

REM                     DNSCmd.exe

REM                  2. Linux BIND requires the path to nsupdate.exe

REM    - TTL         DNS records Time To Live value (in seconds). Default is 86400

REM    - SKIPMODE    Script immediately exits if run under the given recovery plan

REM                  mode. Possible values:

REM                  0 always run (default)

REM                  1 for test mode

REM                  2 for recovery mode

REM

:GETARGS

IF NOT [%1]==[] (

  IF /I [%1] == [-srv] (

     SET DNS-SERV=%~2

  ) ELSE IF /I [%1] == [-u] (

     SET DNS-SERV-USER=%~2

  ) ELSE IF /I [%1] == [-p] (

     SET DNS-SERV-PASS=%2

  ) ELSE IF /I [%1] == [-srvtype] (

     SET SRVTYPE=%~2

     IF /I [!SRVTYPE!] == [WINDNS] (

        SET DNS_SERVER_TYPE=1

     ) ELSE IF /I [!SRVTYPE!] == [BIND] (

        SET DNS_SERVER_TYPE=2

     )

  ) ELSE IF /I [%1] == [-cmd] (

     SET SRVCMD=%~2

     IF /I [!SRVCMD!] == [ADD] (

        SET DNS_COMMAND=1

     ) ELSE IF /I [!SRVCMD!] == [DEL] (

        SET DNS_COMMAND=2

     ) ELSE (

        ECHO.

        ECHO %WRONG_USAGE%: incorrect -cmd parameter: !SRVCMD!

        GOTO :COMMAND_HELP

     )

  ) ELSE IF /I [%1] == [-fzone] (

     SET FORWARD-ZONE=%~2

  ) ELSE IF /I [%1] == [-rzone] (

     SET REVERSE-ZONE=%~2

  ) ELSE IF /I [%1] == [-rzonev6] (

     SET REVERSE-ZONE-V6=%~2

  ) ELSE IF /I [%1] == [-rzonev6rev] (

     SET REVERSE-ZONE-V6-REV=%~2

  ) ELSE IF /I [%1] == [-hostname] (

     SET HOST-NAME=%~2

  ) ELSE IF /I [%1] == [-ip] (

     SET HOST-IP=%~2

  ) ELSE IF /I [%1] == [-ipv6] (

     SET HOST-IPV6=%~2

  ) ELSE IF /I [%1] == [-ipv6rev] (

     SET HOST-IPV6-REV=%~2

  ) ELSE IF /I [%1] == [-ipv6rev_local] (

     SET HOST-IPV6-REV-MSDNS=%~2

  ) ELSE IF /I [%1] == [-fkey] (

     SET FORWARD-KEY=%~2

  ) ELSE IF /I [%1] == [-rkey] (

     SET REVERSE-KEY=%~2

  ) ELSE IF /I [%1] == [-rkeyv6] (

     SET REVERSE-KEY-V6=%~2

  ) ELSE IF /I [%1] == [-toolspath] (

     SET TOOLSPATH=%~2

  ) ELSE IF /I [%1] == [-ttl] (

     SET TTL=%~2

  ) ELSE IF /I [%1] == [-skipmode] (

     SET SKIPMODE_NAME=%~2

     IF /I [!SKIPMODE_NAME!] == [TEST] (

        SET SKIPMODE=1

     ) ELSE IF /I [!SKIPMODE_NAME!] == [RECOVERY] (

        SET SKIPMODE=2

     ) ELSE (

        ECHO.

        ECHO %WRONG_USAGE%: incorrect -skipmode parameter: !SKIPMODE_NAME!

        GOTO :COMMAND_HELP

     )

  ) ELSE (

     ECHO.

     ECHO %WRONG_USAGE%: Unknown parameter %1

     GOTO :COMMAND_HELP

  )

  SHIFT

  SHIFT


  GOTO :GETARGS

)

IF NOT DEFINED REVERSE-ZONE (

  SET REVERSE-ZONE=%ZONE-NONE%

)

IF NOT DEFINED REVERSE-ZONE-V6 (

  SET REVERSE-ZONE-V6=%ZONE-NONE%

)


REM -------------------------------------------------------------------------

REM                       Internal DNS Update Logic

REM -------------------------------------------------------------------------

ECHO.

SET /A STEPNUM=%STEPNUM%+1

ECHO %STEPNUM%. Verifying input data


IF NOT DEFINED DNS_SERVER_TYPE (

  ECHO.

  ECHO WARNING: DNS_SERVER_TYPE parameter is not defined. DNS server will first be treated as Windows DNS and in case of error as BIND server.

  ECHO.

)


REM -------------------------------------------------------------------------

REM                     Verification of the input data

REM -------------------------------------------------------------------------

IF NOT DEFINED DNS-SERV (

  ECHO.

  ECHO %WRONG_USAGE%: missing -srv parameter

  GOTO :COMMAND_HELP

)

IF DEFINED DNS-SERV-USER (

  IF NOT DEFINED DNS-SERV-PASS (

     ECHO.

     ECHO %WRONG_USAGE%: Password for user "%DNS-SERV-USER%" not supplied

     GOTO :COMMAND_HELP

  )

)

IF NOT DEFINED DNS_COMMAND (

  ECHO.

  ECHO %WRONG_USAGE%: missing -cmd parameter

  GOTO :COMMAND_HELP

)

IF NOT DEFINED FORWARD-ZONE (

  ECHO.

  ECHO %WRONG_USAGE%: missing -fzone parameter

  GOTO :COMMAND_HELP

) ELSE IF NOT DEFINED HOST-NAME (

  ECHO.

  ECHO %WRONG_USAGE%: missing -hostname parameter

  GOTO :COMMAND_HELP

) ELSE IF NOT DEFINED HOST-IP (

  ECHO.

  ECHO %WRONG_USAGE%: one of two parameters -ip or -ipv6 is required

  GOTO :COMMAND_HELP

)


FOR /F "delims=. tokens=1-4" %%A IN ("%REVERSE-ZONE%") DO (


  IF not [%%D] == [] (

     ECHO.

     ECHO %WRONG_USAGE%: incorrect reverse zone value

     GOTO :COMMAND_HELP

  ) ELSE IF not [%%C] == [] (

     FOR %%i in (%HOST-IP%) DO (

        FOR /F "delims=. tokens=1-3" %%X IN ("%%i") DO (

           IF not "%%Y" == "" (

              ECHO.

              ECHO %WRONG_USAGE%: IP does not match reverse zone

              GOTO :COMMAND_HELP

           )

        )

     )

  ) ELSE IF not [%%B] == [] (

     FOR %%i in (%HOST-IP%) DO (

        FOR /F "delims=. tokens=1-3" %%X IN ("%%i") DO (

           IF not [%%Z] == [] (

              ECHO.

              ECHO %WRONG_USAGE%: IP does not match reverse zone

              GOTO :COMMAND_HELP

           ) ELSE IF "%%Y" == "" (

              ECHO.

              ECHO %WRONG_USAGE%: IP does not match reverse zone

              GOTO :COMMAND_HELP

           )

        )

     )

  ) ELSE IF %REVERSE-ZONE% == %ZONE-NONE% (

     FOR %%i in (%HOST-IP%) DO (

        FOR /F "delims=. tokens=1-4" %%P IN ("%%i") DO (

           IF [%%S] == [] (

              ECHO.

              ECHO %WRONG_USAGE%: IP address is not complete

              GOTO :COMMAND_HELP

           )

        )

     )

  ) ELSE (

     FOR %%i in (%HOST-IP%) DO (

        FOR /F "delims=. tokens=1-4" %%P IN ("%%i") DO (

           IF not [%%S] == [] (

              ECHO.

              ECHO %WRONG_USAGE%: IP does not match reverse zone

              GOTO :COMMAND_HELP

           ) ELSE IF [%%R] == [] (

              ECHO.

              ECHO %WRONG_USAGE%: IP does not match reverse zone

              GOTO :COMMAND_HELP

           )

        )

     )

  )

)

ECHO.


REM -------------------------------------------------------------------------

REM                       Setup internal variables

REM -------------------------------------------------------------------------

IF DEFINED TOOLSPATH SET PATH=%PATH%;%TOOLSPATH%

IF NOT "%DNS-SERV-USER%" == "" (

  SET DNSCMD=%PSEXECCMD% \\%DNS-SERV% -u "%DNS-SERV-USER%" -p %DNS-SERV-PASS% %DNSCMD%

)


REM -------------------------------------------------------------------------

REM                       Check Recovery Plan Mode

REM -------------------------------------------------------------------------

SET /A STEPNUM=%STEPNUM%+1

ECHO %STEPNUM%. Checking Recovery Plan Mode

ECHO.

SET RUN=1

IF DEFINED VMware_RecoveryMode (

  IF [%VMware_RecoveryMode%] == [test] (

     IF %SKIPMODE% == 1 SET RUN=0

  ) ELSE IF [%VMware_RecoveryMode%] == [recovery] (

     IF %SKIPMODE% == 2 SET RUN=0

  )

)

IF %RUN% == 0 (

  ECHO Mode "%VMware_RecoveryMode%" is skipped. Exiting...

  GOTO :END

)


REM -------------------------------------------------------------------------

REM                       Perform DNS Record Updates

REM -------------------------------------------------------------------------

IF NOT DEFINED DNS_SERVER_TYPE (

  GOTO :WINDOWS_DNS_UPDATES

)

IF %DNS_SERVER_TYPE% == 2 (

  GOTO :BIND_DNS_UPDATES

) ELSE (

  GOTO :WINDOWS_DNS_UPDATES

)


:WINDOWS_DNS_UPDATES


SET /A STEPNUM=%STEPNUM%+1

IF [%DNS_SERVER_TYPE%] == [1] (

  ECHO %STEPNUM%. Performing Windows DNS Server Records Updates...

) ELSE (

  ECHO %STEPNUM%. Trying Windows DNS Server Updates...

  %DNSCMD% /Info

  ECHO.

  IF NOT !ERRORLEVEL! == 0 GOTO :BIND_DNS_UPDATES

)


IF %DNS_COMMAND% == 1 (

  ECHO ---------------[Updating forward zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

) ELSE (

  ECHO ---------------[Deleting forward zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

)


IF %REVERSE-ZONE% == %ZONE-NONE% (

  SET IP4ADDR_PREFIX=

) ELSE (

  SET IP4ADDR_PREFIX=%REVERSE-ZONE%%ADDR-SEP%

)


FOR %%i in (%HOST-IP%) DO (

  %DNSCMD% %DNS-SERV% /RecordDelete %FORWARD-ZONE% %HOST-NAME% A %IP4ADDR_PREFIX%%%i /f

  IF %DNS_COMMAND% == 1 (

     IF !ERRORLEVEL! EQU %DNS_ERROR_NAME_DOES_NOT_EXIST% (

        SET ERRORLEVEL=0

        ECHO WARNING: The name %%i does not exist and it will be added.

     )

  )

  IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

)


IF %DNS_COMMAND% == 1 (

  FOR %%i in (%HOST-IP%) DO (

     %DNSCMD% %DNS-SERV% /RecordAdd %FORWARD-ZONE% %HOST-NAME% %TTL% A %IP4ADDR_PREFIX%%%i

     IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

  )

)


FOR %%i in (%HOST-IPV6%) DO (

  %DNSCMD% %DNS-SERV% /RecordDelete %FORWARD-ZONE% %HOST-NAME% AAAA %%i /f

  IF %DNS_COMMAND% == 1 (

     IF !ERRORLEVEL! EQU %DNS_ERROR_NAME_DOES_NOT_EXIST% (

        SET ERRORLEVEL=0

        ECHO WARNING: The name does not exist and it will be added.

     )

  )

  IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

)


IF %DNS_COMMAND% == 1 (

  FOR %%i in (%HOST-IPV6%) DO (

     %DNSCMD% %DNS-SERV% /RecordAdd %FORWARD-ZONE% %HOST-NAME% %TTL% AAAA %%i

     IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

  )

)


IF %REVERSE-ZONE% NEQ %ZONE-NONE% (

  ECHO.

  IF %DNS_COMMAND% == 1 (

     ECHO ---------------[Updating reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  ) ELSE (

     ECHO ---------------[Deleting reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  )

  FOR /F "delims=. tokens=1-3" %%P IN ("%REVERSE-ZONE%") DO (

     IF NOT "%%R" == "" (

        FOR %%i in (%HOST-IP%) DO (

           %DNSCMD% %DNS-SERV% /RecordDelete %%R%ADDR-SEP%%%Q%ADDR-SEP%%%P%ADDR-SEP%%IP-SUFF% %%i PTR /f

           IF !ERRORLEVEL! NEQ 0 GOTO :FAIL


           IF %DNS_COMMAND% == 1 (

              %DNSCMD% %DNS-SERV% /RecordAdd %%R%ADDR-SEP%%%Q%ADDR-SEP%%%P%ADDR-SEP%%IP-SUFF% %%i %TTL% PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%

              IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

           )

        )

     ) ELSE IF NOT "%%Q" == "" (

        FOR %%i in (%HOST-IP%) DO (

           FOR /F "delims=. tokens=1-3" %%X IN ("%%i") DO (

              %DNSCMD% %DNS-SERV% /RecordDelete %%Q%ADDR-SEP%%%P%ADDR-SEP%%IP-SUFF% %%Y%ADDR-SEP%%%X PTR /f

              IF !ERRORLEVEL! NEQ 0 GOTO :FAIL


              IF %DNS_COMMAND% == 1 (

                 %DNSCMD% %DNS-SERV% /RecordAdd %%Q%ADDR-SEP%%%P%ADDR-SEP%%IP-SUFF% %%Y%ADDR-SEP%%%X %TTL% PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%

                 IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

              )

           )

        )

      ) ELSE (

        FOR %%i in (%HOST-IP%) DO (

           FOR /F "delims=. tokens=1-3" %%X IN ("%%i") DO (

              %DNSCMD% %DNS-SERV% /RecordDelete %REVERSE-ZONE%%ADDR-SEP%%IP-SUFF% %%Z%ADDR-SEP%%%Y%ADDR-SEP%%%X PTR /f

              IF !ERRORLEVEL! NEQ 0 GOTO :FAIL


              IF %DNS_COMMAND% == 1 (

                 %DNSCMD% %DNS-SERV% /RecordAdd %REVERSE-ZONE%%ADDR-SEP%%IP-SUFF% %%Z%ADDR-SEP%%%Y%ADDR-SEP%%%X %TTL% PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%

                 IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

              )

           )

        )

     )

  )

)


IF %REVERSE-ZONE-V6% NEQ %ZONE-NONE% (

  IF %DNS_COMMAND% == 1 (

     ECHO ---------------[Updating IPv6 reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  ) ELSE (

     ECHO ---------------[Deleting IPv6 reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  )

  IF [%HOST-IPV6-REV-MSDNS%] == [] (

     ECHO parameter -ipv6rev_local is missing. MS Dns update commands for IP6 reverse zone can fail

     SET HOST-IPV6-REV-MSDNS=%HOST-IPV6-REV%

  )

  FOR %%i in (%HOST-IPV6-REV-MSDNS%) DO (

     %DNSCMD% %DNS-SERV% /RecordDelete %REVERSE-ZONE-V6-REV%%ADDR-SEP%%IPv6-SUFF% %%i PTR /f

     IF !ERRORLEVEL! NEQ 0 GOTO :FAIL


     IF %DNS_COMMAND% == 1 (

        %DNSCMD% %DNS-SERV% /RecordAdd %REVERSE-ZONE-V6-REV%%ADDR-SEP%%IPv6-SUFF% %%i %TTL% PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%

        IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

     )

  )

)

ECHO.

ECHO ---------------[Finished updating records for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

ECHO.

ECHO.

ECHO.

ECHO.

ECHO.


GOTO :END


:BIND_DNS_UPDATES

SET /A STEPNUM=%STEPNUM%+1

IF [%DNS_SERVER_TYPE%] == [2] (

  ECHO %STEPNUM%. Performing BIND Server Records Updates...

) ELSE (

  ECHO %STEPNUM%. Trying BIND DNS Server Updates...

)


REM -------------------------------------------------------------------------

REM                        Create NS command file

REM -------------------------------------------------------------------------

ECHO [Creating temporary BIND cmd file "%BIND-CMD-FILE%" for forward update]

ECHO.server %DNS-SERV% > "%BIND-CMD-FILE%"


ECHO.zone %FORWARD-ZONE% >> "%BIND-CMD-FILE%"


IF %REVERSE-ZONE% == %ZONE-NONE% (


  FOR %%i in (%HOST-IP%) DO (

     ECHO.update delete %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% A %%i >> "%BIND-CMD-FILE%"

  )


  IF %DNS_COMMAND% == 1 (

     FOR %%i in (%HOST-IP%) DO ECHO.update add %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% %TTL% A %%i >> "%BIND-CMD-FILE%"

  )


) ELSE (


  FOR %%i in (%HOST-IP%) DO (

     ECHO.update delete %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% %TTL% A %REVERSE-ZONE%%ADDR-SEP%%%i >> "%BIND-CMD-FILE%"

  )


  IF %DNS_COMMAND% == 1 (

     FOR %%i in (%HOST-IP%) DO (

        ECHO.update add %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% %TTL% A %REVERSE-ZONE%%ADDR-SEP%%%i >> "%BIND-CMD-FILE%"

     )

  )

)


REM IPv6

FOR %%i in (%HOST-IPV6%) DO (

  ECHO.update delete %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% AAAA %%i >> "%BIND-CMD-FILE%"

)


IF %DNS_COMMAND% == 1 (

  FOR %%i in (%HOST-IPV6%) DO ECHO.update add %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% %TTL% AAAA %%i >> "%BIND-CMD-FILE%"

)

ECHO.send >> "%BIND-CMD-FILE%"

REM we are done with forward zone for both IPv4 and IPv6 addresses.


ECHO.quit >> "%BIND-CMD-FILE%"

ECHO.     >> "%BIND-CMD-FILE%"


IF %DNS_COMMAND% == 1 (

  ECHO ---------------[Updating forward zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

) ELSE (

  ECHO ---------------[Deleting forward zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

)

FOR /L %%i in (1,1,%NUMBER_OF_NSUPDATE_RETRIES%) DO (

  IF DEFINED FORWARD-KEY (

     %NSUPDATECMD% -y "%FORWARD-KEY%" "%BIND-CMD-FILE%"

  ) ELSE (

     %NSUPDATECMD% "%BIND-CMD-FILE%"

  )

  IF !ERRORLEVEL! EQU 0 GOTO :BIND_DNS_UPDATES_REVERSE

)

IF !ERRORLEVEL! NEQ 0 GOTO :FAIL


:BIND_DNS_UPDATES_REVERSE

IF not %REVERSE-ZONE% == %ZONE-NONE% (


  REM -------------------------------------------------------------------------

  REM                        Create NS command file

  REM -------------------------------------------------------------------------

  ECHO [Creating temporary BIND cmd file "%BIND-CMD-FILE%" for reverse update]

  ECHO.server %DNS-SERV% > "%BIND-CMD-FILE%"


  FOR /F "delims=. tokens=1-3" %%A IN ("%REVERSE-ZONE%") DO (

     IF not "%%C" == "" (

        ECHO.zone %%C%ADDR-SEP%%%B%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% >> "%BIND-CMD-FILE%"


        FOR %%i in (%HOST-IP%) DO (

           ECHO.update delete %%i%ADDR-SEP%%%C%ADDR-SEP%%%B%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% PTR >> "%BIND-CMD-FILE%"

           IF %DNS_COMMAND% == 1 (

              ECHO.update add %%i%ADDR-SEP%%%C%ADDR-SEP%%%B%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% %TTL% IN PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% >> "%BIND-CMD-FILE%"

           )

        )

     ) ELSE IF not "%%B" == "" (

        ECHO.zone %%B%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% >> "%BIND-CMD-FILE%"


        FOR %%i in (%HOST-IP%) DO (

           FOR /F "delims=. tokens=1-2" %%X IN ("%%i") DO (

              ECHO.update delete %%Y%ADDR-SEP%%%X%ADDR-SEP%%%B%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% PTR >> "%BIND-CMD-FILE%"

              IF %DNS_COMMAND% == 1 (

                 ECHO.update add %%Y%ADDR-SEP%%%X%ADDR-SEP%%%B%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% %TTL% IN PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% >> "%BIND-CMD-FILE%"

              )

           )

        )

     ) ELSE (

        ECHO.zone %%A%ADDR-SEP%%IP-SUFF% >> "%BIND-CMD-FILE%"


        FOR %%i in (%HOST-IP%) DO (

           FOR /F "delims=. tokens=1-3" %%X IN ("%%i") DO (

              ECHO.update delete %%Z%ADDR-SEP%%%Y%ADDR-SEP%%%X%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% PTR >> "%BIND-CMD-FILE%"

              IF %DNS_COMMAND% == 1 (

                 ECHO.update add %%Z%ADDR-SEP%%%Y%ADDR-SEP%%%X%ADDR-SEP%%%A%ADDR-SEP%%IP-SUFF% %TTL% IN PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% >> "%BIND-CMD-FILE%"

              )

           )

        )

     )

  )


  ECHO.send >> "%BIND-CMD-FILE%"

  ECHO.quit >> "%BIND-CMD-FILE%"

  ECHO.     >> "%BIND-CMD-FILE%"


  IF %DNS_COMMAND% == 1 (

     ECHO ---------------[Updating reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  ) ELSE (

     ECHO ---------------[Deleting reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  )

  FOR /L %%i in (1,1,%NUMBER_OF_NSUPDATE_RETRIES%) DO (

     IF DEFINED REVERSE-KEY (

        %NSUPDATECMD% -y "%REVERSE-KEY%" "%BIND-CMD-FILE%"

     ) ELSE (

        %NSUPDATECMD% "%BIND-CMD-FILE%"

     )

     IF !ERRORLEVEL! EQU 0 GOTO :BIND_DNS_UPDATES_IP6_REVERSE

  )

  IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

)


:BIND_DNS_UPDATES_IP6_REVERSE

IF [%REVERSE-ZONE-V6%] NEQ [%ZONE-NONE%] (

  ECHO.server %DNS-SERV% > "%BIND-CMD-FILE%"

  ECHO.zone %REVERSE-ZONE-V6-REV%%ADDR-SEP%IP6%ADDR-SEP%ARPA%ADDR-SEP% >> "%BIND-CMD-FILE%"

  FOR %%i in (%HOST-IPV6-REV%) DO (

     ECHO.update delete %%i%ADDR-SEP%IP6%ADDR-SEP%ARPA%ADDR-SEP% PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% >> "%BIND-CMD-FILE%"

  )


  IF %DNS_COMMAND% == 1 (

     FOR %%i in (%HOST-IPV6-REV%) DO (

        ECHO.update add %%i%ADDR-SEP%IP6%ADDR-SEP%ARPA%ADDR-SEP% %TTL% PTR %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE% >> "%BIND-CMD-FILE%"

     )

  )

  ECHO.send >> "%BIND-CMD-FILE%"


  IF %DNS_COMMAND% == 1 (

     ECHO ---------------[Updating reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  ) ELSE (

     ECHO ---------------[Deleting reverse zone record for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

  )

  FOR /L %%i in (1,1,%NUMBER_OF_NSUPDATE_RETRIES%) DO (

     IF DEFINED REVERSE-KEY-V6 (

        %NSUPDATECMD% -y "%REVERSE-KEY-V6%" "%BIND-CMD-FILE%"

     ) ELSE (

        %NSUPDATECMD% "%BIND-CMD-FILE%"

     )

     IF !ERRORLEVEL! EQU 0 GOTO :BIND_DNS_UPDATES_END

  )

  IF !ERRORLEVEL! NEQ 0 GOTO :FAIL

)


:BIND_DNS_UPDATES_END

ECHO.

ECHO ---------------[Finished updating records for %HOST-NAME%%ADDR-SEP%%FORWARD-ZONE%]---------------

ECHO.

ECHO.

ECHO.

ECHO.

ECHO.


GOTO :END

:COMMAND_HELP

ECHO.

ECHO.DNS Records Update Script Usage:

ECHO.   dns_update.cmd -srv {dns-server} [-u {user} -p {pass}] [-srvtype {dns-type}] -cmd {command} -fzone {forward_zone} [-rzone {reverse_zone}] -hostname {name} -ip {ip} [-fkey {forward-zone-key}] [-rkey {reverse-zone-key}] [-toolspath {tools-path}] [-ttl {dns-record-ttl}] [-skipmode {mode}]

ECHO.

ECHO.   -srv          DNS server to update.

ECHO.   -u            [optional] Username of Windows account to use when updating

ECHO.                            the Windows DNS server.

ECHO.   -p            [optional] Password of Windows account to use when updating

ECHO.                            the Windows DNS server. Required if '-u' parameter

ECHO.                            is used.

ECHO.   -srvtype      [optional] DNS server type. Possible values:

ECHO.                            bind      for Bind server

ECHO.                            windns    for Windows DNS server

ECHO.   -cmd          Specifies the server command. Possible values:

ECHO.                            add       add given forward and reverse mapping

ECHO.                            del       delete given forward and reverse mapping

ECHO.   -fzone        Specifies the forward lookup zone name.

ECHO.   -rzone        [optional] Specifies the reverse lookup zone.

ECHO.                            NOTE: the zone address is not in reversed format.

ECHO.                            NOTE: you can specifies NONE so that the reverse

ECHO.                                  mapping will be skipped and the IP part must

ECHO.                                  contain entire IP.

ECHO.   -hostname     Denotes the hostname part of the new DNS record.

ECHO.   -ip           Specifies the IP part of the new DNS record. Multiple IP

ECHO.                 addresses per hostname are allowed - separate them with

ECHO.                 commas. The values in this column are appended to the zone

ECHO.                 address specified in zone-ip in order to form the complete

ECHO.                 IP address.

ECHO.   -fkey         [optional] Specifies the forward lookup zone shared secret

ECHO.                            for BIND in the format {keyname}:{secret}.

ECHO.   -rkey         [optional] Specifies the reverse lookup zone shared secret

ECHO.                            for BIND in the format {keyname}:{secret}.

ECHO.   -toolspath    [optional] Tool paths which will be appended to the PATH

ECHO.                            variable.

ECHO.                            NOTE:

ECHO.                            1. Windows DNS requires the paths to PSExec.exe

ECHO.                               and DNSCmd.exe

ECHO.                            2. Linux BIND requires the path to nsupdate.exe

ECHO.   -ttl          [optional] DNS records Time To Live value (in seconds).

ECHO.                            Default is 86400 (1 day)

ECHO.   -skipmode     [optional] Script immediately exits if run under the given

ECHO.                            recovery plan mode. Possible values:

ECHO.                            test     Do not update DNS in test mode

ECHO.                            recovery Do not update DNS in recovery mode

ECHO.

ECHO.

EXIT /B 1


:FAIL

SET EXITCODE=!ERRORLEVEL!

ECHO.

ECHO.Last command failed with exit code %EXITCODE%

IF EXIST "%BIND-CMD-FILE%" (

  ECHO.Dumping temporary file "%BIND-CMD-FILE%":

  ECHO.

  ECHO ---------------------------- BIND COMMAND FILE ----------------------------

  TYPE "%BIND-CMD-FILE%"

  ECHO -------------------------- END BIND COMMAND FILE --------------------------

)

ECHO.


:END

REM -------------------------------------------------------------------------

REM Set EXITCODE to last error encountered and ignore any further errors

REM while deleting the temporary files below.

REM -------------------------------------------------------------------------

IF !EXITCODE!==0 (

  SET EXITCODE=!ERRORLEVEL!

)

IF EXIST "%BIND-CMD-FILE%" (

  ECHO Deleting temporary file: %BIND-CMD-FILE%

  del /f "%BIND-CMD-FILE%"

  IF !ERRORLEVEL! NEQ 0 (

     ECHO WARNING: File deletion failed with exit code !ERRORLEVEL!

  )

)

EXIT /B %EXITCODE%


Comments