
,ch06.2677 Page 73 Thursday, April 8, 2004 11:56 AM Chapter 6 6 6. Machine-Specific Scripting So far, we have demonstrated the use of scripts that will run unattended on any number of workstations, quietly managing, reporting, and configuring as required by the administrator. These scripts even maintain themselves, noting when they become obsolete and replacing themselves when changes on a remote server sug- gest that they should do so. While there are many circumstances in which this functionality is more than sufficient, there is one major limitation: none of the techniques or scripts we have described is able to differentiate its behavior depending on the machine on which it is being run. This may not pose a prob- lem if the tasks being carried out are all routine and generally applicable, but it does impose severe restrictions if the purpose of scripting is to automate reconfig- uration of a machine or if you require certain tasks to be carried out only on spe- cific machines. In this chapter, we explain how to enhance the techniques already described by writing scripts whose actions are partially determined by the specific workstation on which they are running. First, we demonstrate the extraction and comparison of an identifier that can be found trivially within the registry. We then describe types of situations in which such an identifier falls short; the bulk of the chapter is devoted to describing various ways of extracting information that unambiguously and uniquely identifies a workstation. It is this latter type that we use exclusively in our scripts, for reasons which should soon become self-evident. Extracting the Machine Identifier Every Windows NT workstation has a name, an identity that is assigned to it when the operating system is installed. This name must be unique on a local area net- work because it is used for Windows networking; it normally (but not always) has 73 ,ch06.2677 Page 74 Thursday, April 8, 2004 11:56 AM 74 Chapter 6: Machine-Specific Scripting the same value as the hostname if IP networking is installed.* As far as we are con- cerned at the moment, the useful thing about this name is that it is incredibly easy to extract from a workstation. This means that it can provide us with a simple way of specifying script behavior that varies depending upon the individual worksta- tion on which it is being run. There are several methods available for retrieving the machine name from a work- station. Four obvious ones spring to mind: • Get it through the Network Control Panel. • Retrieve it from the registry. • Use network command-line utilities. • Read an environment variable. A brief description of each of these methods follows. From the Network Control Panel If you are sitting at a workstation, the quickest way to obtain the computer name is probably through the Network Control Panel. Simply open the panel (Start ➝ Settings ➝ Control Panel ➝ Network) and you will be presented with a dialog looking very much like the one in Figure 6-1. The computer name is presented clearly and unambiguously. Clearly, although this extraction method is trivial for someone sitting at a worksta- tion, it would not be of much use to a script. For this we have to turn to one of the other options. From the Registry It should come as no surprise to find that a workstation’s computer name is stored in the registry. A few minutes browsing with Regedt32 would reveal that the infor- mation is stored in the HKEY_LOCAL_MACHINE hive under the string value HKLM\ SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName. The fol- lowing short Perl scriptlet, computername.pl extracts the computer name of a workstation from the registry and prints it to standard output: #computername.pl use Win32::Registry; $key = 'SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName'; * The computer name, hostname, and significance for Windows networking (NetBIOS) will be discussed in much more detail in the next chapter. ,ch06.2677 Page 75 Thursday, April 8, 2004 11:56 AM Extracting the Machine Identifier 75 Figure 6-1. Network Control Panel Win32::Registry::RegOpenKeyEx (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle); Win32::Registry::RegQueryValueEx ($RegHandle, 'ComputerName', NULL, $type, $answer); Win32::Registry::RegCloseKey($RegHandle); print "The name of this NT workstation is $answer"; Running this scriptlet from the command line on a workstation called albatross will produce the following results: C:>computername.pl The name of this NT workstation is ALBATROSS Using net.exe Another straightforward way of finding out the name of a computer is to use a net command. Here, we will concentrate on one incantation of the command, net config workstation, which displays information about the workstation service. Typing this command at the command line produces a list of parameters, sepa- rated into four logical groups, which can loosely be described as concerning: • Identity • Networking hardware • Domain information • Communication time-outs ,ch06.2677 Page 76 Thursday, April 8, 2004 11:56 AM 76 Chapter 6: Machine-Specific Scripting The section we are interested in is the first one. The information displayed here looks very much like this: C:\>net config workstation Computer name \\ALBATROSS User name home As you can see, the computer name takes pride of place. The extraction of just the computer name from this output can be accomplished with the following frag- ment of Perl: @output = `net config workstation`; $output[0] =~ s/.*\\\\(.*)/$1/; print "The computer name is: $output[0]"; The code may look a little obfuscated thanks to the regular expression, but it is actually incredibly simple. The first line calls net config workstation and stores the output in an array. A regular expression then replaces the first line of the array with the value of the computer name (“find any number of any characters, fol- lowed by two backslashes, followed by any number of any more characters; replace everything with the bit after the backslashes”). Finally we prove the point by printing the computer name to standard output. From the Environment Short of using the GUI-based Network Control Panel, by far the simplest method for extracting the computer name from a workstation is to read the environment table. In common with virtually all operating systems in the world, Windows NT provides a set of so-called environment variables that store information about the machine and current user. These variables are listed if the set command is typed at the command prompt without any parameters. If you type set and read the list of variables, you will find that one of them is called computername; its value, unsurprisingly, is set to the name of the workstation. Extracting the value from an environment variable is totally trivial: on the Windows NT command line, any word sandwiched between two % symbols is treated as an environment variable and automatically “expanded” to the value of that variable. Therefore, on a work- station called albatross, the command: C:>echo This computer is called %computername% will produce the following output: This computer is called ALBATROSS Perl provides the special variable %ENV to read environment variables. The follow- ing scriplet reads the computer name from the environment and prints it to stan- dard output: ,ch06.2677 Page 77 Thursday, April 8, 2004 11:56 AM Extracting the Machine Identifier 77 #computername.pl print "The name of this NT workstation is $ENV{computername}"; We leave it to your imagination to work out what the output of this scriptlet may look like! Clearly this is a much neater, simpler way of retrieving that computer name than using the registry or parsing the output of a command-line network utility; it would be very difficult to justify using any other method unless the circumstances were rather exceptional. Given that it is so easy for a script to identify the name of the workstation on which it is running, it seems rather perverse to suggest—as we are just about to— that this is actually a rather unsatisfactory identifier. Although it and the extraction techniques discussed earlier are perfectly adequate for many maintenance and configuration tasks that require a script to behave differentially depending on the workstation on which it is running, there are certain situations in which reliance on computer name is not at all satisfactory. The reason for this is that while com- puter names are usually unique on a LAN, there is no guarantee that they are unique. In fact, in some situations, you can be extremely confident that they will not be unique, for example, if a set of workstations are disk-imaged clones or if the operating system has been installed automatically with a default set of parame- ters. Furthermore, there may be situations in which a workstation regularly changes its name or location, perhaps depending on who is using it. In any of these cases, a script that really needs to know on which workstation it is running will have to find a more robust method of identifying machines. Ideally, this method should be completely impervious to any amount of configuration and tweaking. Thankfully, every computer that contains an Ethernet card does have exactly what we need—a unique identifier encoded in hardware—which goes by the name of MAC address. Furthermore, virtually every operating system that supports network- ing provides a means of extracting this information relatively easily. Or at least for those that don’t, someone (the OS vendors themselves in the case of NT) pro- vides utilities to do it for you. The rest of this chapter is dedicated to describing various ways of retrieving a workstation’s MAC address; in the next chapter, we give an extended example of putting it to use by writing a configuration script that can totally change a workstation’s identity.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages18 Page
-
File Size-