How Can I Run a Script Against a Range of IP Addresses?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’d like to run a script against all the computers on a subnet. Is there a way to do that without having to hardcode all the IP addresses into the script?

— RB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RB. Based on your email, it sounds like you have a setup similar to this: you have a subnet with IP addresses ranging from 192.168.1.1 to 192.168.1.254. You’d like to create a script that will start with the first IP address, run some code against that computer, move on to the second address, run that same code, and continue in this same vein until you’ve hit each and every computer. Furthermore, you’d like to do that in as few lines of code as possible, and without having to hardcode a couple hundred IP addresses.

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

Pretty easy, huh?

Of course, you’re probably not all that interested in echoing a bunch of IP addresses; you’d like to run some WMI code instead. That’s fine:

On Error Resume Next

intStartingAddress = 1 intEndingAddress = 254 strSubnet = “192.168.1.”

For i = intStartingAddress to intEndingAddress strComputer = strSubnet & i

Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”) Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_OperatingSystem”) For Each objItem in ColItems Wscript.Echo strComputer & “: ” & objItem.Caption Next

Next

As you can see, we again set the value of the variable strComputer to an IP address. We then simply connect to the WMI service on the computer represented by that address. That’s an easy thing to do, because WMI can connect to computers using IP addresses as well as using computer names.

Now we’d like to add one more little wrinkle. You mentioned in your email that there are a few IP addresses you’d like to exclude, IP addresses that probably represent routers or something. Okey-doke. Here’s a modified script that uses a Select Case statement to exclude certain computers:

intEndingAddress = 254
strSubnet = “192.168.1.”

For i = intStartingAddress to intEndingAddress Select Case i Case 10 Case 50 Case 100

Case Else strComputer = strSubnet & i Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”) Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_OperatingSystem”) For Each objItem in ColItems Wscript.Echo strComputer & “: ” & objItem.Caption Next

End Select Next

Notice what happens when the value of i (the last octet in the IP address) is equal to 10, 50, or 100. That’s right: nothing happens. If a computer has an IP address of 192.168.1.10, 192.168.1.50, or 192.168.1.100, nothing happens; no WMI code will run, and the script will simply loop around. The WMI code will execute only for computers that have IP addresses other than those three. A simple but effective way to exclude specific IP addresses from the WMI portion of the script.

0 comments

Discussion is closed.

Feedback usabilla icon