Downloads
7993.zip

A trademark of competent WAN systems administrators is that end users rarely surprise them with news such as, "Did you know that we can't access our email in the Boston office?" To avoid such surprises from end users, you can use a script called PingWAN2Routers.cmd to check for WAN connectivity problems in your Windows NT domain.

In a properly designed NT domain, a WAN connects BDCs at remote sites. In a small WAN, you typically have a PDC at the home site and a BDC at each remote site (i.e., single-domain model). In a large WAN, you typically have a BDC for the selected master domain and a host of PDCs and BDCs for the local resource domains (multiple master-domain model). You can use PingWAN2Routers.cmd to check the connections in either type of WAN. You can use this NT shell script to check the WAN connections to any number of remote sites. You just need to understand how the script works, customize the script for your system, and test and execute the script.

Understanding How the Script Works
PingWAN2Routers.cmd tests the WAN connection to the domain controller at each remote site and displays a red warning window if the connection fails to ping. The script uses simulated arrays, the Ping command, and the Microsoft Windows NT Server 4.0 Resource Kit's Findstr utility to provide this testing and warning service.

NT shell scripting doesn't support arrays. However, you can use variables that follow a naming convention to simulate an array. Take, for example, the Server_1_, Server_2_, and Server_3_ variables in Listing 1. (Listing 1 contains an excerpt from PingWAN2Routers.cmd. You can find the entire script in the Code Library on the Win32 Scripting Journal Web site at http://www.win32scripting.com.) Together, these variables create three array entries (1, 2, and 3) in an array named Server. The code in Listing 1 sets the first array entry (Server_1_) to the server name of the domain controller in the first remote site, sets the second array entry (Server_2_) to the server name of the domain controller in the second remote site, and so on.

Similarly, the Router_1_, Router_2_, and Router_3_ variables create three array entries (1, 2, and 3) in an array named Router. The code sets these array entries to each remote site's router address. (For more information about how to create simulated arrays or how to use the NT shell commands that this article mentions, see Tim Hill, Windows NT Shell Scripting, Macmillan Technical Publishing, 1998.)

PingWAN2Routers.cmd uses the values in the array entries to name the text files that contain the output from the Ping command. The script first uses the Ping command to verify the connection to a remote domain controller. If this ping times out, the script initiates a long ping (2500ms). If the long ping also times out, the script pings the router. This pinging process lets the script test for three error conditions:

  • Lost connectivity because of heavy traffic, in which case the Ping command returns the message Request timed out
  • Lost connectivity because WINS or DNS is down, in which case the Ping command returns the message Bad IP Address
  • Lost connectivity because the router can't communicate with the destination router, in which case the Ping command returns the message Destination Host Unreachable

If a connection is successful, the Ping command returns a message similar to Reply from xxxxxx: bytes=32 time=188ms TTL=126.

The Ping command outputs its message to a text file, which the script names after the server or router it's pinging. The script stores the text files in an archive and keeps the archive up-to-date by deleting the oldest file before it creates a new file. The archive contains up to nine files.

The script uses the Findstr utility's Findstr command in a series of modules to search new text files for three key strings: Request, Bad, and Destination. (For information about how to use modules in NT shell scripting, see Dick Lewis, "Developing a Server Failure Notification System," parts 1 and 2, July and August 1999.) For example, as callout A in Listing 2 shows, the :TIMEOUT module uses the command

findstr /m /i "Request" *.txt

to look for the string Request in all the text files in the current directory. The /m switch tells the script to display only the filenames of the files containing the string Request, which is why the script names the output files after the servers and routers it's pinging. The /i switch specifies that the string matches are case-insensitive.

If the Findstr command finds the string Request, the script uses the && compound-command symbol to carry out several additional commands. You're probably familiar with the & compound-command symbol—the && symbol is a conditional version of the & symbol. If you use the & symbol between command A and command B, the script executes command A, then command B. If you use the && symbol between command A and command B, the script executes command A. However, the script executes command B only if command A successfully executes.

Thus, in the :TIMEOUT module, if the Findstr command successfully finds the string Request, the first && symbol tells the script to execute the Color command. If the Color command successfully sets the console window's background color to light red (which the letter c specifies) and the window's text color to black (which the zero denotes), the second && symbol tells the script to execute the Title command. If the Title command successfully changes the window's title to LOOK RIGHT NOW! Bad PING!, the third && symbol tells the script to execute the Goto command, which directs the script to the :LONGPING module.

The script triggers the :LONGPING module when a ping times out. To achieve a long ping, the script uses the command

ping -w 2500 %%i

where the -w 2500 switch specifies a timeout interval of 2500ms and the %%i iterator variable contains the name of the server to ping. If the ping times out for the usual ping but doesn't time out for the long ping, the WAN connection is simply slow.

Customizing the Script
To use PingWAN2Routers.cmd, you need to customize the script to your domain and prepare your system. Follow these steps:

  1. Place a copy of the Findstr utility in your working directory or somewhere in the system path.
  2. In PingWAN2Routers.cmd, replace the specified server names (e.g., ruby) and router addresses (e.g., 10.25.1.1) with the server names and router addresses for the remote sites you want to ping. As Listing 1 shows, the script currently pings three remote sites. If you want to ping additional sites, you can add server and router statements following the syntax
    set Server_x_=ServerName
    set Router_x_=RouterAddress
    where x increments by 1 for each additional remote site. For example, x takes the value of 4 if you're adding another remote site for a total of four remote sites.

    If you're pinging more than three remote sites, you also need to change the following line of code in the script:

    for /l %%i in (1, 1, 3) do set
    /a x=%%i & call :ARRAY
    Change the value of 3 to match the number of remote sites you're pinging.
  3. In the script PingWAN2Routers.cmd, change the specified drive, if necessary. The script assumes you've installed NT on the default C drive. If you've installed NT on another drive, change the code C: (see Listing 1) to the appropriate drive letter.
  4. In PingWAN2Routers.cmd, replace the %scripts%\output\ping directory path (see Listing 1) with your system's directory path.
  5. Place PingWAN2Routers.cmd in a separate directory.
  6. Set a global environment variable for the location of your script (optional). For example, you can use a global environment variable such as %systemroot%\system32\scripts. Within that directory, you can add another directory called output (or something similar), then add subdirectories to the output directory as needed. That way, you're never more than two steps away from viewing the archived output from any script. You first double-click the output directory shortcut, then double-click the appropriate subdirectory.

Testing and Executing the Script
After you've prepared your system and tailored PingWAN2Routers.cmd for your domain, you can test the script and schedule it to run. To test PingWAN2Routers.cmd, put a fictitious server name in the script or disconnect your system from the LAN by pulling the cable. Run the script. A red console window tells you that the script is working. Check the output files for error messages.

The first few times you run the script in production mode, you'll receive error messages. Error messages occur because the script tries to rename or delete output text files that don't exist yet. These error messages will stop by the 10th time you run the script.

You can use the At command to schedule the script to run regularly. For example, suppose you want to execute PingWAN2Routers.cmd before you come into work each morning. You can use the command

at 06:45 /interactive
   /every:m,t,w,th,f
   "C:\winnt\system32\scripts   pingwan2routers.cmd"

to schedule the script to automatically execute every weekday at 6:45 a.m. The /interactive switch lets the script interact with your NT desktop when it executes.

You can create a shortcut that leaves the console window open after the script executes. In Windows Explorer, open the folder containing the script. Right-click the script, then select Copy. Find a place to put the shortcut (your desktop is a good place), right-click, and select Paste Shortcut. After you've made the shortcut, right-click it and select Properties. Select the Shortcut tab. The script's path and name appear in the Target field. In that field, insert cmd /k in front of the path. This command causes the console window to remain open after the script finishes executing. If you use this command, you can remove the Pause command at the end of PingWAN2Routers.cmd.

You can also customize the position and size of the console window so that you can see the window and read its output more easily. To customize the position of the window on your desktop, press alt+spacebar while in the Command Prompt window. Select Properties, then the Layout tab. Change the value in the Left field. For example, entering 130 in the Left field places the console window in the upper right corner of your desktop. To customize the window's size, change the values in the Width and Height fields to enlarge the size of the window. For example, entering 100 in the Width field and 250 in the Height field produces a large window that displays a lot of output that you can scroll through.

After you made the changes, a prompt will appear asking whether you want to either Apply properties to current window only or Modify shortcut which started this window. Select the latter to retain the layout every time you run the script.

You can even make the console window fill your entire desktop. Go to the shortcut's Options tab, and click Full Screen in the Display Options group.

Be Proactive, Not Reactive
If you don't want end users to surprise you with news about the WAN connection failures, use PingWAN2Routers.cmd. By adapting this script to your NT domain and using it to regularly check the status of your WAN connections, you can immediately find and fix any problems.