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

a-weinert.de,  weinert-automation.de
|< < > >|

blog... /windows-2003-dns-and-dhcp-from-command-line/   [en]
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 hostAdd.bat V.01.00 28.09.2009 (c) Copyright Albrecht Weinert (a-weinert.de)
@Echo hostAdd.bat adds or removes DNS and (optionally) DHCP entries

@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

@Echo Calling the first time in this environment
@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 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!

@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 IP-End = %1 IP = %NET_START%.%1
@echo simple name = %2 (no dot or domain added!)
@echo MAC-address = %3 (Form is: 000e8c8faabb)
@if %2X==X goto :usage
@if %3X==X goto :dns

@Echo making DHCP for %NET_START%.%1 = %2 = %3, „%4“ %5
@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 making DNS for %NET_START%.%1 = %2

%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

@echo Deleting %1 %2 %3
@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 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

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

@echo settings used:
@set DNS_CMD


Post comments feed

Your Comment

Bitte loggen Sie sich zum Kommentieren ein beziehungsweise registrieren Sie sich so als willkommener neuer Nutzer des Blogs.

Copyright   ©   2013, 2019   Albrecht Weinert,       E-Mail (webmaster)