Using Dhcpcmd to Query DHCP
The code at callout B calls Dhcpcmd to query a specific scope on a specific DHCP server to return the lease information from the database. The generic syntax is
Dhcpcmd DHCPServerIPAddress
command [command options]
Command options range from one that creates subnets to one that adds reservations. The Enumclients command tells the DHCP server to query the database for IP address lease information. Netlookup uses a DHCP server with the IP address 10.0.1.5 configured with the subnet 10.0.1.0. The -h command option displays the MAC address for each computer along with the lease information. Enumclients returns the entry record number in the database, the IP address leased, and the NetBIOS name of the computer. The query returns all the subnet's leases, so the script redirects the Dhcpcmd output to the variable %dhcpdump%, which Netlookup then filters for information about the computer Niser. At callout C, Netlookup uses the Findstr utility, a helpful text patternmatching tool, to search for "Niser" in dhcpdump.txt. The -i tells Findstr to ignore case, and %1 represents the parameter Niser that I specified on the call to Netlookup. The results appear on screen, as follows:
10.0.1.15 NISER a1b2c3d4e5f6
If you have multiple scopes or DHCP servers to search, add them as additional lines in Netlookup. Because the variables Netlookup defines point to text files under the %temp% system variable, you don't have to worry about the location of the Temp directory. In Win2K, %temp% defaults to %userprofile%\Local Settings in the user's profile; in NT, the variable usually points to C:\Temp.
Querying WINS with Winscl
Next, Netlookup searches the WINS database. Winscl is a great tool for looking up information in WINS and an excellent alternative to using the database's time-consuming GUI. When you run Winscl from the command line, the tool prompts you for several responses. Netlookup supplies the responses for you in an answer file embedded in the code at callout D. The code at Callout D echos the header information for the lookupresults file and redirects the text to the Winscl file. Then, Netlookup specifies 1 to tell Winscl that you use TCP/IP (not named pipes) and supplies the NetBIOS name of the WINS server to query. Netlookup then specifies QN (to tell Winscl to query names), %1 to represent the computer name to search for, and 0 to indicate that you don't want to query for specific NetBIOS registrations. Finally, Netlookup uses the EX command to exit the tool.
As it did with the Dhcpcmd results, Netlookup uses Findstr to pick out only the information you need. The code at callout E calls Winscl and Findstr with a pipe symbol between them, which tells the system to run Winscl, then do something with the results. The information you want to filter from the results of Winscl is in two different lines, so Findstr matches two patterns. Two /c switches on the Findstr command pattern-match on text strings that the developer specifies (in this case, Name= and "Address is"). The script redirects the results to lookupresults.txt and adds them to the DHCP query results. Notice the script uses two greater than (>) signs to append the results to the file's existing text. After you run the command, the contents of the lookupresults.txt file appear as follows:
Name=(NISER ) Address is (10.0.1.15)
You'll find Winscl a bit clunky, but useful. DNS will replace WINS over time, but until it does, the need to manage and understand WINS is as important as ever.
DNS Queries with Nslookup
The DNS database lookup is the easiest of the three database lookups to perform. Netlookup uses the Nslookup utility to query DNS about the computer name you specified when you called Netlookup. The code at callout F calls Nslookup and adds the results to the lookupresults.txt file.
Ping and Results
After Netlookup collects the network-service information, it pings the remote computer to verify that the IP address is accessible (if the user specified the optional -p switch on the command line). Callout G shows the syntax that provides the optional ping.
After the script completes the DHCP, WINS, and DNS queries, the line of code at callout H displays the results to your screen. You can make the lookupresults.txt file easier to read by adding a descriptive Echo statement before each section of code that queries one of the databases. The script's final lines of code clear the Netlookup files.
When you're trying to determine why you can't connect to a specific computer on your network, you want to do it quickly. Various command-line tools are available for querying DHCP, WINS, and DNS, and when you tie them together with a script such as Netlookup, you have a fast and easy way to acquire the information you need all at once.
Andrew Plackowski November 07, 2002