Introduction


The Dynamic Host Configuration Protocol (DHCP) is prevalent within most organizations. If you have more than three or four client computers, statically configuring IP addresses and network settings can be a support burden. DHCP makes the job of assigning IP addresses much easier because instead of manually configuring each computer on your network, DHCP does it for you. Dynamically assigning IP addresses and reclaiming them when they are no longer being used also makes more efficient use of your address space.

DHCP is a simple yet effective protocol that allows a computer booting up with no prior TCP/IP network configuration to obtain an IP address, called a lease, and various network settings, called options, such as the default router, DNS servers, and default domain name. For details on how DHCP works, see RFC 2131: http://www.ietf.org/rfc/rfc2131.txt.

The Microsoft DHCP Server is one of the most popular DHCP servers available. It's included with the Windows Server operating system and is simple to configure and maintain. In this chapter, I'll cover several recipes that walk you through the setup and configuration of DHCP Server.

Using a Graphical User Interface

DHCP Server comes with the DHCP MMC snap-in (dhcpmgmt.msc) that can be used to configure and manage scopes, superscopes, leases, reservations, and options. This is the graphical tool I use for most recipes in this chapter.

Using a Command-Line Interface

The netsh tool allows you to configure from the command line just about everything you can control with the DHCP snap-in, plus some additional advanced settings that you can't even access with the snap-in. The netsh tool can be run in a variety of different ways. There is an interactive mode that you get to by simply typing netsh at a command line. If it is a DHCP Server, type dhcp server and press the Enter key to configure the local computer. Netsh will return an error if the local computer doesn't have DHCP Server running. Alternatively, you can type dhcp server \\<ServerName> to configure a remote DHCP Server. Type list and press the Enter key to get a list of all available commands you can run from within a given mode.

Netsh supports a command-line mode that lets you run a single command and return to the command prompt. Instead of typing netsh and pressing Enter, type the full path of the command. For example:

> netsh dhcp server dump

Finally, netsh supports a batch mode that lets you run multiple netsh commands at a time. Simply write each command to a text file. Then run the following command:

> netsh exec <Filename>

where <Filename> is the path to the file containing the commands.

The only other command-line tools I cover in this chapter are dhcploc and sysocmgr. dhcploc is useful for finding all the DHCP Servers on a given subnet. You can find it in the Windows 2000 Resource Kit Supplement 1 or in the Windows Server 2003 Support Tools. See Recipe 14.18 for more information. I use sysocmgr in Recipe 14.1 to install DHCP Server and it can be found in %systemroot%/system32.

Using VBScript

Both the graphical and command-line tools for managing DHCP Server are robust and flexible. Unfortunately, Microsoft has no corresponding programmatic interfaces for managing DHCP Server from scripts. There are no WSH or WMI APIs for DHCP Server. Microsoft did throw its customers a bone by providing a DLL called dhcpobjs.dll in the Windows 2000 Resource Kit. It provides an interface that scripts can use to manage various aspects of DHCP Server, but it has a few problems. It isn't truly supported by Microsoft since it is part of a Resource Kit. This wouldn't be a huge issue, but it turns out that dhcpobjs is pretty buggy; especially when dealing with a large number of scopes and leases. There are numerous threads in the Microsoft newsgroups about people running into problems when using dhcpobjs. So if you use it, understand that it isn't the most stable interface. It is also poorly documented.

The only other option is to call out to the netsh command from within scripts. Programming purists will hate the thought of doing this, but if you want to automate the configuration and maintenance of your DHCP Servers, this is really your only option. You can use the Run method I've used numerous times in this book to run netsh. The following code snippet shows how to use Run to set the audit log location:

strServer = "\\dhcp01"  ' set this to "" to target the local computer strCommand = "netsh dhcp server " & strServer & " set auditlog d:\dhcp\audit"     set objWshShell = WScript.CreateObject("WScript.Shell") intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then    WScript.Echo "Error returned from running the command: " & intRC else    WScript.Echo "Command executed successfully" end if

The main issue with the Run method is you don't have access to standard error or standard out to capture any output generated by the command. If you are going to use netsh extensively in scripts, you may want to use the Exec method instead, which has options for accessing standard out and standard error. The following code performs the same function as the previous code, except it prints everything sent to standard out and, if an error occurs, everything sent to standard error:

strServer = "" strCommand = "netsh dhcp server " & strServer & " set auditlog d:\dhcp\audit"     WScript.Echo "Running command: " & strCommand WScript.Echo set objShell = CreateObject("Wscript.Shell") set objProc  = objShell.Exec(strCommand)     Do    WScript.Sleep 100 Loop Until objProc.Status <> 0     if objProc.ExitCode <> 0 then    WScript.Echo "EXIT CODE: " & objProc.ExitCode    WScript.Echo "ERROR: " & objProc.StdErr.ReadAll end if     WScript.Echo "OUTPUT: " & objProc.StdOut.ReadAll

With Exec, it is a good idea to check the Status property to ensure the command completed. The script uses this feature to test for command completion and if it hasn't completed, the script sleeps for 100 milliseconds and check again. It then looks at the ExitCode. A nonzero exit code indicates an error and if that occurs, I print the code and the text sent to standard error. Lastly, I print the output from standard out.

I don't include scripting examples using netsh in this chapter, but they are available on my web site: http://www.rallenhome.com/books/.




Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net