I wrote a little utility, IP Ranger.hta, that takes as input a starting IP address and an ending IP address and writes all the IP addresses within that range to a text file. Several IT staff members at my company had been asking me for this type of tool for a while because they didn't have a complete or accurate server list that they could use as input for some of my other administrative scripts. Initially, I thought of trying to build this functionality into the existing scripts but decided it would be easier to have a standalone HTML Application (HTA) that produced a text file of IP addresses. With this solution, the IT staff members just need to point the scripts to the IP address text file just as if the text file contained a list of server names. In addition, I didn't have to change a bunch of my scripts, which can be risky because it opens up the possibility of introducing errors.
As Figure 1 shows, the utility's UI is simple to use. You just enter the starting IP address and ending IP address, then click the button labeled IP Ranger. The HTA has a number of failsafe measures to ensure that you enter the correct IP addresses. If you don't enter a starting IP address or if you enter an invalid IP address, you'll get a message box that states, Invalid IP Range1. Similarly, if you don't enter an ending IP address or if you enter an invalid IP address, you'll receive the error message Invalid IP Range2. If you enter the same IP address in both the IP Range1 and IP Range2 text boxes, you'll get the error message, Same Values Entered. However, if you happen to enter the starting and ending IP addresses in the wrong text boxes, the HTA will swap the IP addresses for you, as callout A in Listing 1 shows. This listing contains the code for the EvaluateIP subroutine, which is the main subroutine in IP Ranger.hta.
The EvaluateIP subroutine uses an incrementing process to write each IP address to the C:\~IPRanger~.txt file. As callout B in Listing 1 shows, the subroutine first breaks down the beginning IP address into its four octets. Then, the subroutine uses a Do…Loop sta tement, which works as follows. First, the loop writes the IPAddress variable's value to the text file. The first time the loop runs, that variable will contain the beginning IP address. Next, the loop increments the beginning IP address's fourth (last) octet by 1. Two possible scenarios can occur:
- The new fourth octet is less than 255, in which case the loop assembles a full IP address that contains the new fourth octet. If that new IP address is not equal to the ending IP address, the loop begins again by writing the IPAddress variable's value (which now contains the new IP address) to the text file, adding 1 the new IP address's fourth octet, and so on.
- The new fourth octet is greater than 254, in which case the script adds 1 to the third octet and resets the fourth octet to 1. The loop then assembles a full IP address that contains the new third and fourth octets. If that new IP address is not equal to the ending IP address, the loop begins again by writing the IPAddress variable's value (which now contains the new IP address) to the text file, adding 1 the new IP address's fourth octet, and so on.
As long as the ending IP address isn't reached, the loop continues incrementing the third and fourth octets in this fashion until the third octet reaches the value of 255. When the third octet is greater than 254, the second octet is incremented by 1 and the third and fourth octet are reset to 1. The process then starts again with incrementing the second, third, and fourth octets until the second octet reaches 255. When the second octet is greater than 254, the first octet is incremented by 1 and the second, third, and fourth octets are reset to 1. The loop continues until the newly assembled IP address is equal to the ending IP address.
The HTA works on Windows Server 2003, Windows XP, and Windows 2000 with IPv4. I didn't write it for IPv6 because I haven't seen IPv6 in use anywhere. I really think IPv4 will be around for a while yet. (In case you were wondering about IPv5, this was an experimental non-IP real-time stream protocol that was never widely used.)
When you test IP Ranger.hta, you shouldn't try it with wide IP ranges or unrealistic IP ranges. You can easily create a VERY large file and find yourself waiting a long time for the script to complete. When the script does complete, you'll get a message stating so.
Share Your Scripting Experiences
Share your scripting discoveries, comments, solutions to problems, and experiences with products. Email your contributions to [email protected] Please include your full name and phone number. We edit submissions for style, grammar, and length. If we print your submission, you’ll get $100.