Enterprise Software

Find the master browser in a peer-to-peer workgroup using regular expressions

When working on a peer-to-peer workgroup, you know the importance of the master browser. In this Daily Drill Down, Greg Shultz employs the regular expressions feature in a VBScript to help you track down that elusive master browser.


As you know, when you run Windows in a peer-to-peer workgroup, one of the systems is responsible for compiling a list of all the systems in the workgroup that have resources to share and for displaying that list in Network Neighborhood/My Network Places. This particular system is called the master browser and is chosen for the role by winning the browser service election. Without getting into too much detail, when a browser service election is held, the system in the workgroup with the best set of predefined qualifications is elected as the master browser. These browser service elections are held whenever the status of the system currently acting as the master browser changes or is challenged. Various systems in a Windows workgroup can be elected to the master browser role at different times.

When troubleshooting network connection problems in a Windows workgroup, determining which system in the workgroup is currently acting as the master browser can make quick work of your troubleshooting expedition. However, since there’s no built-in network tool for identifying the master browser in a workgroup, this job becomes a manual procedure, going from system to system and changing the master browser options. This can be a very time-consuming and frustrating operation.

The Browstat and Browmon utilities
The Windows NT 4.0 Resource Kit comes with two utilities, Browstat and Browmon, that allow you to easily identify the master browser in a Windows NT/2000 domain. However, neither of these utilities will run in a peer-to-peer workgroup configuration, as they’re specifically designed for use in a Windows NT/2000 domain configuration.

If you’re running the TCP/IP protocol on your network, one of the pieces of information contained in a computer’s NetBIOS table and revealed when using the NBTSTAT utility is the identity of the master browser. While using the NBTSTAT utility does provide a way to track down the master browser, using this utility alone can also be time consuming and frustrating because it must be run from the command line and only displays the NetBIOS table of one computer at a time.

I’ve discovered a way to automate the NBTSTAT utility from within the Windows Script Host using VBScript to track down the computer acting as the master browser. I’ve employed this technique in a tool called the Find Master Browser Utility, which will quickly display the name of the computer acting as the master browser in a Windows workgroup.

As I show you how to create the Find Master Browser Utility in VBScript, I’ll introduce you to VBScript’s regular expressions feature and explain how this advanced search-and-replace tool makes extremely quick work of searching through a text string. (In a follow-up Daily Drill Down, I’ll take apart the script piece by piece and explain how it works for those of you who need more than just a quick fix for finding the master browser.)

Prerequisites
Before we get started, a few prerequisites must be satisfied to use the Find Master Browser Utility. First, you must have the Windows Script Host 5.5 package installed on the system that will run the Find Master Browser Utility. You can download this package from the Windows Script page of the Microsoft Developer Network site. Just follow the links to the Windows Script Host Download section. The Windows Script package includes Windows Script Host 5.6, VBScript 5.5, and JScript 5.5. Also, keep in mind that there are two versions of the package: one for Windows 2000 and one for Windows 95/98/NT.

Second, you must have the TCP/IP protocol installed on all the computers of your peer-to-peer workgroup because the NBTSTAT utility, which is the basis of the Find Master Browser Utility, is a TCP/IP-based tool.

General overview
The Find Master Browser Utility script is really pretty straightforward. It uses the Net View command to generate a list of all the computers in the peer-to-peer workgroup. It then plucks, one by one, the name of each computer from the list and runs the NBTSTAT utility with each computer’s name as a parameter.

The Find Master Browser Utility script then searches the output generated by the NBTSTAT utility for the master browser identification. The script will continue this process with each computer name until it locates the master browser identification. When the script locates this identification, it displays the name of that computer.

Technical overview
Now that you have a general idea of how the script finds the master browser in a peer-to-peer workgroup, let’s take a more technical look at how the script actually goes about locating this information. As I mentioned above, the Find Master Browser Utility uses the Net View command and the NBTSTAT utility to gather information about the peer-to-peer workgroup. Both of these tools are DOS-based, so the process of acquiring information from them involves shelling out to a command prompt, running the commands, and redirecting the output generated by the commands to text files. Once back in the script environment, the script needs to open these files and search through them to locate and extract certain items.

Net View command
In the case of the Net View command, the file created from the results of this command will contain a list of the computers in the peer-to-peer workgroup. The list will be displayed in the format shown in the output below. I need the script to search each line in the file for the characters \\, which signify that the text that immediately follows is the name of the computer. I then need the script to extract just the name of the computer.

Sample output of the Net View command
 
Server Name   Remark
————————————————————
\\EARTH       Test System 3
\\JUPITER     Test System 5
\\MARS        Test System 4
\\MERCURY     Test System 6
\\NEPTUNE     Test System 1
\\PLUTO       Celeron System
\\SATURN      Test System 7
\\SUN         Main System
\\URANUS      Test System 2
\\VENUS       Laptop System
The command completed successfully.


NBTSTAT command
In the case of the NBTSTAT command, the file created will contain a list of the contents of a particular computer’s NetBIOS table. The script must search each line in the file for the word MSBROWSE, which signifies that the system being tested is the master browser.

Sample output of the NBTSTAT command
 
Local Area Connection:
Node IpAddress: [192.168.1.101] Scope Id: []

NetBIOS Remote Machine Name Table

Name               Type         Status
——————————————————————-
SATURN         <6A>  UNIQUE      Registered
SATURN         <87>  UNIQUE      Registered
SATURN         <00>  UNIQUE      Registered
SOLAR SYSTEM   <00>  GROUP       Registered
SATURN         <20>  UNIQUE      Registered
SOLAR SYSTEM   <1E>  GROUP       Registered
SOLAR SYSTEM   <1D>  UNIQUE      Registered
..__MSBROWSE__.<01>  GROUP      Registered

MAC Address = 00-01-03-25-02-61


Regular expressions
Performing these types of searches and extractions is quite easy in VBScript using the regular expressions feature. If you’re familiar with the concept of using wildcard characters when searching for files on your hard disk, you already have a pretty good idea of what regular expressions are. Basically, regular expressions are a more powerful and flexible form of wildcards that you can use to search through strings of text.

Regular expressions are implemented in VBScript via a special object called RegExp. This object has several properties and methods that allow you to configure and conduct several types of searches. The Pattern property makes the searches possible. Basically, you assign the text string that you want to search for to the Pattern property. You then use one of the RegExp object’s methods to determine which type of search you want to conduct. In the Find Master Browser Utility script, I conduct two types of searches.

The first type of search simply looks through the text string for a particular pattern and then returns a Boolean value that indicates whether the pattern was found. This type of search is conducted with the Test method.

The second type of search goes through the text string, locates a particular pattern, and then extracts it from the text string. This type of search is conducted with the Execute method.

Once the Execute method extracts a particular pattern from the text string, it stores that pattern as a Match object in a Matches collection. Because a collection is basically an array, a Matches collection is an array of Match objects. The Match object has its own set of properties, one of which is the Value property, which will contain the extracted piece of text.

That may sound a bit confusing at first, but it’s actually very straightforward once you get past all the technicalities. Keep this overview in mind as I discuss the actual implementation of the regular expressions feature in the Find Master Browser Utility script.

If you really want to learn more about VBScript’s regular expressions feature, check out the VBScript Help system’s Language Reference, which contains a detailed explanation of all the facets of regular expressions. You’ll also find detailed information about the Match object and the Matches collection.

Creating the Find Master Browser Utility script
Creating the Find Master Browser Utility script is actually a simple task. To begin, launch Notepad or your scripting editor and type the Find Master Browser Utility script shown in Listing A. Be sure that you type all the commands exactly as shown. If you don’t, the script won’t work correctly. When you’ve finished, save the file as FindMB.vbs.

If typing in lines of code isn’t your cup of tea, don’t worry; the FindMB.vbs script is available for download here.

Conclusion
There you have it, a FindMB script that will chew through any peer-to-peer network configuration to locate your master browser machine. Now you know how to use the NBSTAT and Net View commands together in a script that will extract this specific information. I also introduced you to the VBScript regular expressions feature, which works just like the wildcard character in traditional hard disk file searches. In my follow-up Daily Drill Down, I’ll explain each section of code step-by-step to give you an idea of how this all works. If you need the script without the lesson, you can download the finished product by clicking here.

About Greg Shultz

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

Editor's Picks