Subscribe to Windows IT Pro
January 01, 1998 12:00 AM

Modify the Registry with Perl

Windows IT Pro
InstantDoc ID #3456
Rating: (1)

Walking Through the Script
The WINSer.pl script contains four key functional areas: a block that reads the command-line parameters, verifies them, and reads in the list of machine names if applicable (at callout A in Listing 1); an outer loop that traverses the computer name array and establishes the initial Registry connection (at C in Listing 1); an inner loop that steps through the adapters on a machine, updating the appropriate WINS addresses along the way (at D in Listing 1); and a subroutine for printing a Help message (at E in Listing 1).

The first two lines of WINSer.pl access the library files we need to work with the Registry. The next line tests the @ARGV command-line argument array for the number of arguments on the command line. Referencing the entire array in a scalar sense returns the number of elements the array contains. If the @ARGV array is empty (i.e., the user didn't specify any command-line arguments), WINSer.pl calls the &PrintHelp subroutine, which prints the Help information and then exits. Otherwise, WINSer.pl initializes $primary and $secondary with the first two arguments on the command line, the primary and secondary WINS server IP addresses.

After initializing $primary and $secondary, the script tests for the presence of a third command-line argument, $ARGV\[2]. In the absence of $ARGV\[2], control transfers to the last few lines of code at A in Listing 1, where the script assigns the local computer name to the @machines array of machine names. If $ARGV\[2] is present, the program compares the lowercase version of $ARGV\[2] to the string "remote" using Perl's eq operator. If the test is successful (i.e., the user is updating a remote machine), WINSer.pl assigns the fourth command-line argument, $ARGV\[3] (the name of the remote machine), to the @machines array. If the "remote" string test fails, the script tests $ARGV\[2] for the string "file". If the test is successful, the script attempts to open the file specified by $ARGV\[3]. If the file cannot be opened, the script exits with an appropriate error message. If the file is successfully opened, the script retrieves the file's contents, assigns them to the @machines array, and closes the file.

A Peek at Perl's Power
Let's examine the line of code at B in Listing 1 to get a closer look at Perl's power. This single line of code uses two standard Perl library functions, map and grep, to convert an input file (even a badly formatted one) into a neatly ordered array. Figure 1, page 209, depicts this filtering process.

WINSer.pl first reads the file that the FILELIST file handle points to. Perl's file-handle read operator (<FILELIST>) assigns each line of the input file to an element of the @machines array, as shown in the second box in Figure 1. The map function applies a code block ({s/\s//g; $_}), to each element in the array. This particular code block strips all blank characters (e.g., spaces, tabs, new lines) from a line. The third box in Figure 1 shows the result of the map filter.

WINSer.pl then passes the filtered array to the grep function, which returns elements of the array that match a specified regular expression (/\s/). In this case, grep returns all the nonblank lines from the array. The result is an ordered (and clean) list of machine names, as shown in the fourth box in Figure 1.

Connecting to the Registry
The foreach loop at C in Listing 1 traverses all the computer names in the @machines array. In each iteration of the loop, WINSer.pl stores the computer name in $machine and then attempts to connect to the HKEY_LOCAL_MACHINE hive on the computer $machine specifies. If the connection fails, WINSer.pl prints an error message, drops out of the loop iteration, and advances to the next element in @machines. If the connection is successful, RegConnectRegistry returns a handle to the connection in $key_LM. Next, the script attempts to open HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\Adapters. If the open fails, WINSer.pl prints an appropriate error message, closes the connection with a call to RegCloseKey, and moves to the next element in @machines. If the open succeeds, WINSer.pl executes the loop at D in Listing 1.

WINSer.pl uses a while loop to walk through the subkeys of the \Adapters key. Before entering the loop, the code initializes $i (the loop counter and index for the subkeys) to 0.

For each iteration, the code checks to see whether the adapter name contains the string NdisWan. If the adapter name includes this string, WINSer.pl skips the subkey and moves to the next subkey. (This part of the script ensures that WINSer.pl doesn't try to update RAS-related WINS information.) If NdisWan does not appear in the subkey's name, WINSer.pl attempts to open the adapter subkey. If the attempt is not successful, WINSer.pl performs appropriate error recovery. Otherwise, WINSer.pl sets the primary WINS address (or performs error recovery if the attempt fails). WINSer.pl follows the same procedure for the secondary WINS address, and then prints a message saying it successfully updated the entries. The while loop ends by closing the appropriate keys and moving to the next \Adapter subkey. After WINSer.pl visits all the subkeys, it closes the connection to the remote Registry and continues with the next computer name in @machines.

Help for Users
The block of code at E in Listing 1 is the PrintHelp subroutine. This simple subroutine uses Perl's "here document" string syntax. The <<END operator tells Perl to substitute the lines that follow (up to the line consisting of END) for <<END in the print statement. In other words, the subroutine prints out all the text lines up to END. This method is ideal for situations in which you need to print a large block of formatted text. The only thing to remember is that Perl performs variable interpolation and treats the backslash character (\) as the escape character for "here document" strings.

Beyond the Registry
Perl's ability to work with NT extends far beyond the Registry. Additional packages exist that provide programmatic access to the Security Account Manager (SAM) database, NT event logs, Open Database Connectivity (ODBC) databases, and the Internet. Future articles will explore some of these aspects of the language and present scripts built using other scripting languages.

Corrections to this Article:

  • The code for the Perl program, WINSer.pl, featured in Bob Wells' article, "Modify the Registry with Perl," contained errors.The corrected code is available from "Windows NT Magazine's" Web site at http://www.winntmag.com.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Feb 15, 2005

    I have tried. this code doesn't work

  • Scott
    9 years ago
    Nov 26, 2003

    This dosnt work. I wish sometimes that they would flog people that place stuff in news groups that DONT WORK! or the poster should have enough sense to TEST THERE SCRIPT OUT BEFORE writing about it.

    Have a great day!

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.