If you occasionally change the names and IP addresses of servers in your domain and some of your client machines use HOSTS files for DNS name resolution, you can use the script HostsFileCreator.bat to reflect those changes in the HOSTS files. Before I show you how to use this Windows NT shell script, let me explain how it works.
How HostsFileCreator.bat Works
HostsFileCreator.bat lets you either replace or modify existing HOSTS files. The script also lets you roll back to previous HOSTS files, if necessary. To run, HostsFileCreator.bat needs two input files and two batch tools.
The first input file that the script needs is servers.txt. This file lists the computers on which you need to change the HOSTS files. The second input file, hostsnew.txt or hostsappend.txt, provides the new information for the HOSTS files. If you want to replace the existing HOSTS files, the script needs hostsnew.txt. If you want to modify the existing HOSTS files, the script needs hostsappend.txt.
HostsFileCreator.bat needs the batch tools now.exe and choice.exe from the Microsoft Windows NT Server 4.0 Resource Kit. Now.exe provides a time-and-date stamp in the log file hosts-results.txt. Choice.exe lets you create a menu that contains the options for running the script.
When you run HostsFileCreator.bat, the script presents you with four options:
Option 1: Copy a new HOSTS file. If you choose this option, the script copies the new HOSTS file on the machines you specify in servers.txt. Instead of deleting the HOSTS file that you're replacing (i.e., the file called hosts), the script renames it to hosts.old. The script archives up to two previous HOSTS file versions (hosts.old and hosts.oldest). That way, you can use option 3 to revert to previous versions of the HOSTS file, if necessary.
Option 1 requires servers.txt and hostsnew.txt. You must place these input files in the locations that the %temp%\servers.txt and %temp%\hostsnew.txt variables specify, respectively. Otherwise, the script will display a warning message, then exit.
Option 2: Append the existing HOSTS file. If you choose this option, the script modifies the existing HOSTS file on the machines you specify in servers.txt. Before modifying the file, the script makes a copy of it and names it hosts.old. As in option 1, the script archives up to two previous HOSTS file versions so that you can revert to them, if necessary.
To modify the HOSTS file, the script first removes the entries that are no longer accurate. You specify those entries by including a string that the entries contain. For example, suppose you renamed the servers SeattlePDC, SeattleBDC, and SeattleFile to Wash01PDC, Wash02BDC, and Wash03File, respectively. You can specify Seattle as the string to remove the SeattlePDC, SeattleBDC, and SeattleFile entries from the HOSTS file. You can specify multiple strings for multiple searches.
After the script removes the inaccurate entries, the script appends the new entries you list in hostsappend.txt to the remaining entries in the file. If the append process creates any blank lines, the script removes those lines.
Option 2 requires servers.txt and hostsappend.txt. You must place these input files in the locations that the %temp%\servers.txt and %temp%\hostsappend.txt variables specify, respectively. Otherwise, the script will display a warning message, then exit.
Option 3: Reverse the last copy or append operation. If you choose this option, the script reverses the last operation of option 1 or 2. The script first renames the existing HOSTS file (i.e., hosts) to hosts.rev. Next, the script renames hosts.old to hosts. Finally, the script renames hosts.oldest to hosts.old. If you use option 3 twice consecutively, you can make host.oldest the existing HOSTS file.
Option 3 requires servers.txt. You must place this input file in the location that the %temp%\servers.txt variable specifies. Otherwise, the script will display a warning message, then exit.
Option 4: Quit. The script exits. No HOSTS file changes are made.
How to Use HostsFileCreator.bat
You can find HostsFileCreator.bat in the Code Library on the Win32 Scripting Journal Web site at http://www.win32scripting.com. The script includes comments to help you understand the code. Here are the steps to get the script working:
- Create the input file servers.txt. List the computers that require HOSTS file modifications following the format
- Create the input file hostsnew.txt or hostsappend.txt. Using the format that Figure 1 shows, include the IP address, fully qualified domain name, and server name for each machine you list in servers.txt.
- Configure the name and administrative share path to the \winnt directory. As callout A in Listing 1 shows, the script's default location for the computers is C$\winnt. If your computers are in this default location, modify the administrative share path that callout B in Listing 1 highlights. If your computers aren't in this default location, you need to modify the code at both callouts A and B.
If you have computers in default and nondefault locations, you can run the script in batches. First, include all the default-location computers in the servers.txt file, leave the NTDirPath variable set to the default location (i.e., C$\winnt), set the administrative share path, and run the script. Then, include all the nondefault-location computers in the servers.txt file, set the NTDirPath variable to the nondefault location (e.g., D$\winnt), set the administrative share path, and run the script.
If you have several \winnt directory locations but you're unsure of their location, you can use the code in Listing 2 to sort the computers you list in servers.txt. This code sorts the computers by the location of their \winnt directories.
- Run HostsFileCreator.bat from a server or workstation on which you've installed now.exe and choice.exe. Anyone running this script must have the appropriate permissions to access the administrative shares on the client computers.
- Test the script on one client before using the script to change the HOSTS file on all the clients. Verify the changes to the HOSTS file in hostsresults.txt. Figure 2 contains an example of the results you might find in hostsresults.txt. In this example output, the script failed to create a HOSTS file for Server3. Typical reasons for failure include an unavailable server, an incorrect \winnt directory path, and insufficient permissions.