So is there a way to do this? Of course there is, and it?s actually easier than you think.
To start with, let?s show you how to loop through a range of IP addresses. This is sample code, so all it does is echo the name of each IP address. After we explain how this script works, then we?ll show you a more practical example:
On Error Resume Next
intStartingAddress = 1
intEndingAddress = 254
strSubnet = "192.168.1."
For i = intStartingAddress to intEndingAddress
strComputer = strSubnet & i
Wscript.Echo strComputer
Next
No, really, that?s the entire script. We start by assigning some variables: intStartingAddress gets assigned the value 1; intEndingAddress gets assigned the value 254; and strSubnet gets assigned the value 192.168.1. (note the period after the 1). As you might have guessed, these values will serve as the building blocks for constructing our IP addresses.
After assigning the variables, we create a For-Next loop that runs from 1 (intStartingAddress) to 254 (intEndingAddress). Why do we loop from 1 to 254? That?s easy: that?s your IP range. What if your IP range is 192.168.1.7 to 109.168.1.54? No problem: use the exact same loop, but just change the value of intStartingAddress to 7 and intEndingAddress to 54.
Inside our loop, we concatenate the string value 192.168.1. with the current value of our loop variable (i). The first time we run through the loop ? when i equals 1 ? we combine 192.168.1. and 1. And guess what? That gives us value of 192.168.1.1, which just happens to be our first IP address. The last time we run through the loop, we?ll combine 192.168.1. and 254, giving us the value of our last IP address, 192.168.1.254. Run this script, and you?ll get output like this:
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4