• 4
  • 13
  • 5
  • 1
  • 2
Der MEVA - Blog
Albrecht Weinert


a-weinert.de,  meva-lab.de
|< < > >|


blog... /windows-2003-dns-and-dhcp-from-command-line/   [de]
Albrecht Weinert

Windows (2003) DNS and DHCP from command line

Administering a well running DHCP and DNS on a Windows 2003 server is quite easy with the graphical tools — if the domain and number of tasks is small. But sooner or later the administrator wants automating those tasks by using the command line and scripting.

Adding a workstation, printer or other device to one of your your domains net usually involves

  • adding an entry to the DHCP,
  • adding an entry (A) to the DNS forward lookup zone and
  • adding one to three reverse (PTR) entries.

Removing the thing later involves the deleting of all those entries made.

Doing that by the GUI for more than three devices usually guarantees mistakes and inconsistencies. But alas, Microsofts DNS and DHCP command line tools are neither simple to use nor well documented. Some hide essential sub-commands and options in their help text. And, as usual, one of the tools has to be installed afterwards even on the enterprise edition server.

So, after several hours of googling (finding more of the same questions than answers) and (partly dangerous) experiments I put all in one 100 line batch script. The size might be deterring at first sight. But I think it’s self documenting in the sense that ist answers a lot of the questions found. Experience so far shows the thing working quite well running (remote logged in) on the DHCP server itself.

The script might might be easily adapted to your environment by changing lines 16 to 21 accordingly. Here it is:

@Echo.
@Echo hostAdd.bat V.01.00 28.09.2009 (c) Copyright Albrecht Weinert (a-weinert.de)
@Echo.
@Echo hostAdd.bat adds or removes DNS and (optionally) DHCP entries
@Echo.

@if %2X==X goto :usage
@if /I %1==-help goto :usage
@if /I %1==/help goto :usage
@if %1==-? goto :usage
@if /I %2==-help goto :usage
@if /I %2==/help goto :usage
@if %2==-? goto :usage

@if %NET_START%X==X goto :setDefault
@if %REVERSE_ZONE%X==X goto :setDefault
@if %FORWARD_ZONE%X==X goto :setDefault
@if %DHCP_SERVER%X==X goto :setDefault
@if %DHCP_SCOPE%X==X goto :setDefault
@if %DNS_CMD%X==X goto :setDefault
@goto :domSubIsSet

:setDefault
@Echo.
@Echo Calling the first time in this environment
@Echo.
@echo setting Domain (FORWARD_ZONE), subnet (NET_START) etc.

@REM Do change these settings according to your DNS/DHCP-Server
set NET_START=192.168.89
set DHCP_SERVER=192.168.89.11
set DHCP_SCOPE=192.168.89.0
set REVERSE_ZONE=89.168.192.in-addr.arpa

set FORWARD_ZONE=fb3-meva.fh-bochum.de
set DNS_CMD=”C:\Programme\Support Tools\dnscmd.exe”

@echo did set zones, subnet etc.: Is it so OK? cntl-C if not!
@Echo.
@pause
@Echo.
:domSubIsSet

@if /I %1==-delete goto :delete
@if /I %1==/delete goto :delete
@if /I %1==-add shift
@if /I %1X==/addX shift

@if %2X==X goto :usage
@if NOT %6X==X goto :usage

@Echo Add Host to %NET_START%. private subnet of domain %FORWARD_ZONE%
@Echo.
@echo IP-End = %1 IP = %NET_START%.%1
@echo simple name = %2 (no dot or domain added!)
@echo MAC-address = %3 (Form is: 000e8c8faabb)
@echo.
@if %2X==X goto :usage
@if %3X==X goto :dns

:dhcp
@Echo making DHCP for %NET_START%.%1 = %2 = %3, “%4″ %5
@echo.
@REM add reservedip IP MAC [ClientName] [ClientComment] [{DHCP | BOOTP | BOTH}]
@echo Dhcp Server %DHCP_SERVER% Scope %DHCP_SCOPE% Add reservedip %NET_START%.%1 %3 %2 %4 %5
@netsh Dhcp Server %DHCP_SERVER% Scope %DHCP_SCOPE% Add reservedip %NET_START%.%1 %3 %2 %4 %5
@echo.

:dns
@Echo making DNS for %NET_START%.%1 = %2
@echo.

%DNS_CMD% . /recordadd %FORWARD_ZONE% %2 A %NET_START%.%1
%DNS_CMD% . /recordadd %REVERSE_ZONE% %1 PTR %2.
%DNS_CMD% . /recordadd %REVERSE_ZONE% %1 PTR %2.%FORWARD_ZONE%
@goto :end

:delete
@shift
@echo.
@echo Deleting %1 %2 %3
@echo.
@if %2X==X goto :usage
@if NOT %4X==X goto :usage
@if /I %1==-help goto :usage
@if /I %1==/help goto :usage
@if %1==-? goto :usage

if %3X==X goto :noMAC

@Echo Deleting DNS entries for %3 = %NET_START%.%1 (%2)
@netsh Dhcp Server %DHCP_SERVER% Scope %DHCP_SCOPE% delete reservedip %NET_START%.%1 %3
@echo.

:noMAC
@Echo Deleting DNS entries for %2 = %NET_START%.%1
%DNS_CMD% . /recorddelete %REVERSE_ZONE% %1 PTR %2. /f
%DNS_CMD% . /recorddelete %REVERSE_ZONE% %1 PTR %2 /f
%DNS_CMD% . /recorddelete %REVERSE_ZONE% %1 PTR %2.%FORWARD_ZONE% /f
%DNS_CMD% . /recorddelete %FORWARD_ZONE% %2 A %NET_START%.%1 /f
@goto :end

:usage
@echo.
@echo Usage of hostAdd.bat
@echo.
@Echo hostAdd [-add] IP-end name
@Echo adds DNS A and PTR records to this servers DNS
@Echo.
@Echo hostAdd [-add] IP-end name MAC ["comment " [type]]
@Echo additionally adds DHCP entry
@Echo.
@Echo ————————————-
@Echo.
@Echo.
@Echo hostAdd -delete IP-end name
@Echo deletes the DNS entries
@Echo.
@Echo hostAdd -delete IP-end name MAC
@Echo additionally deletes the DHCP entry
@Echo.
@Echo ————————————-
@Echo.

@echo settings used:
@set NET_START
@set FORWARD_ZONE
@set REVERSE_ZONE
@set DHCP_SERVER
@set DHCP_SCOPE
@set DNS_CMD

:end
@echo.

Feed für Kommentare zum Beitrag

Your Comment

Please log in to write a comment. Use the same link to register as a new welcome user.

Copyright   ©   2013   Albrecht Weinert,       E-Mail (webmaster)
Feed on RSS: Post Feed RSS   Posts Feed,   Comments Feed RSS   Comments Feed