I'm always looking for tools to simplify my job as a systems administrator. Sometimes those administrative tools are available from a third-party vendor; sometimes I need to create them. Many of the tools I create are scripts that run from the command line—nothing fancy, but they do what I need. Learning administrative scripting is crucial because of the limitations of many Windows-based administration tools: Scripting provides customization that prebuilt solutions often can't. (For more information about command-line tools for account management, see "Take Command of Your Management Tasks," February 2001, InstantDoc ID 16426).
I created a script a few years ago that continues to be one of the most helpful pieces of code I've written. I developed the script to help me troubleshoot network connectivity problems because I was tired of manually looking up information about a remote workstation or server in the following network-service databases:
- DHCP server. This server stores IP reservations, assigns dynamic IP addresses, and hands out machine configuration information. The DHCP server database stores the computer name and network adapter media access control (MAC) address of each computer on the network.
- DNS. DNS provides the lookup mechanism for matching a Fully Qualified Domain Name (FQDN) to an IP address.
- WINS. WINS servers resolve NetBIOS names to IP addresses. Windows clients register with WINS servers to share their NetBIOS name, MAC address, and NetBIOS registration information.
Querying all these databases for network information is a time-consuming task. My code uses command-line tools and scripting techniques to create an all-in-one lookup tool that returns information about a specific computer.
The Right Tools for the Job
Three tools let you query the network-services databases: Dhcpcmd, for DHCP; Winscl, for WINS; and Nslookup, for DNS. The first two tools, Dhcpcmd and Winscl, are both available in the Windows NT 4.0 Resource Kit Support Tools at http://www.microsoft.com/ntserver/nts/downloads/recommended/ntkit/default.asp (Winscl is also available with the Microsoft Windows 2000 Resource Kit CD-ROM). Nslookup is included with the Win2K and NT OSs.
My script, netlookup.bat (which Listing 1, page 58, shows), has three major sections, each of which uses one of these tools to perform a lookup of one of the network-services databases. The script then redirects the results of those lookups to a text file that you can display on screen. When you run the script from the command line, you provide as the first parameter the name of the computer about which you want information. For example, to query for information about a machine named Niser, you would type
Netlookup niser
at the command line. You might want to copy the query tools into one directory or make sure they're part of your system path so that netlookup.bat can easily find them. Also, each utility needs appropriate permissions to access each database.
The code at callout A in Listing 1 defines variables for the temporary files the script uses: dhcpdump stores the results of the DHCP query, lookupresults holds the data gathered during the script's execution, and winsclinput stores the WINS input file.