My boss recently asked me to document network information about all the devices with IP addresses within a class C subnet (i.e., 256 addresses). In the past, I obtained such information by manually pinging servers or by using the script PingReport.bat ("Real-World Scripting: Ping Report," July 2000). However, this time my boss asked me to document not only the IP addresses but also the computer names and media access control (MAC) addresses. I needed to output this information to a Comma Separated Values (CSV) file so that I could later import the data into a spreadsheet.
To programmatically collect this data, I adapted PingReport.bat. The new script, PingandArp.bat, uses the IPConfig, Ping, and Arp utilities. Specifically, the script uses
- The IPConfig /all command to display all the current TCP/IP network configuration values for the local machine
- The Ping –a command to ping each machine and resolve its IP address into a computer name
- The Arp –a command to retrieve the MAC address for the IP address
The Arp –a command will fail if the system has already removed the MAC address from the Address Resolution Protocol (ARP) cache. Thus, I placed the Arp –a command immediately after the Ping –a command to ensure that the ARP cache would still contain the MAC address.
Figure 1 and Figure 2, page 9, show the pseudocode for PingReport.bat and PingandArp.bat, respectively. By comparing the two figures, you can see how I adapted PingReport.bat into PingandArp.bat. (If you're unfamiliar with pseudocode, see "Getting Started in NT Shell Scripting, Part 1," March 2000, and "Getting Started in NT Shell Scripting, Part 2," April 2000.)
I tested PingandArp.bat on Windows NT Workstation with Service Pack 6 (SP6), NT Server 4.0 with SP6, Windows 2000 Professional, and Win2K Advanced Server. You can find this script in the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com). After you download PingandArp.bat, you need to make several configurations:
- In the line
Set First_3_Octets=10.0.0replace 10.0.0 with the first three octets of the IP address range you're testing.
- In the lines
Set Starting_Number=1 Set Ending_Number=254replace 1 with the starting point and 254 with the ending point of the IP address test range. Valid numbers are 1 through 254.
- In the line
Set Output_Location=C:\pingres.csvreplace C:\pingres.csv with your CSV file's pathname.
- In the line
SET Show_Progress=YESspecify whether you want to see status messages about the script's progress on the console. If you do, leave the YES value. If you don't, specify any other value (e.g., NO).
Be sure to test PingandArp.bat's operation and suitability in a nonproduction environment before deployment. Figure 3 shows sample results from running this script. As you can see, the required data—the IP and MAC addresses and the computer name—is present.