How Can I Change the First Two Octets of an IP Address?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the first 16 bits on an IP address? I want to keep the last 16 bits exactly the same.

— LD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, LD. Before we answer your question we should note that we are well aware that “suggestive” photos of an American Idol contestant have allegedly been uncovered on the Internet. Needless to say, we know many of you are concerned that a similar scandal might erupt around the Script Center and the Scripting Guys. We thought we should take a moment to assure you that you have nothing to worry about: there are absolutely no “suggestive” photos of the Scripting Guys to be found anywhere on the Internet. None.

Not that we didn’t try to post some, mind you. Interestingly enough, however, it turns out that there are some things that even the Internet won’t accept!

Now that we’ve got that out of the way let’s talk about changing the first 16 bits of an IP address. According to your email, LD, you have a bunch of servers that have IP addresses similar to this:

You’d like to change these “10.10” IP addresses to “192.168” IP addresses. In other words, you’d like to take an IP address like and turn it into an IP address like this:

As you noted, the first two octets change; the last two octets remain the same.

So can you do that using a script? You bet you can:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colNetAdapters = objWMIService.ExecQuery _ (“Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True”)

arrSubnetMask = Array(“”)

For Each objNetAdapter in colNetAdapters For Each strAddress in objNetAdapter.IPAddress arrOctets = Split(strAddress, “.”) If arrOctets(0) = “10” and arrOctets(1) = “10” Then strNewAddress = “192.168.” & arrOctets(2) & “.” & arrOctets(3) arrIPAddress = Array(strNewAddress) errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) End If Next Next

Let’s see if we can figure out how this all works. As you can see, we start out by connecting to the WMI service. In this sample script we connect to the WMI service on the local computer; however, simply by assigning a remote computer name to the variable strComputer you can just as easily run this script against a remote machine. And can you also adapt this script so that it runs against a bunch of machines? Of course you can; that’s what our Multiple/Remote Computer Scripting Templates are for.

After connecting to the WMI service our next task is to retrieve a collection of all the network adapters installed on the computer. That’s what this little chunk of code is for:

Set colNetAdapters = objWMIService.ExecQuery _
    (“Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True”)

You might have noticed that our query includes the clause Where IPEnabled = True. Why did we include that clause? Well, the Windows operating system features all sorts of “virtual” network adapters, items that get picked up by the Win32_NetworkAdapterConfiguration class. These aren’t real network adapters, at least not in the sense that they are tied to an actual network adapter card and are assigned an IP address. If you’ve ever run a script that uses the Win32_NetworkAdapterConfiguration class you’ve likely gotten back 10 or more items, even if your computer only has one network card installed. That’s because of all these virtual adapters. By restricting returned data to network adapters where the IPEnabled property is True we filter out everything except “real” network cards.

After we execute our query and get back a collection of all the network adapters on the computer we then take a slight detour, using this line of code the create an array named arrSubnetMask:

arrSubnetMask = Array(“”)

Why do we do that? Because when we assign a new static IP address to a computer we need to pass two parameters: the new IP address and the new subnet mask. And both of those values must be passed as arrays, even if we only have one such IP address or subnet mask.

Speaking of assigning new IP addresses, here’s how we do that. To begin with, we set up a For Each loop to walk through the collection of network adapters. Inside that loop we then set up a second For Each loop, this one designed to walk through all the IP addresses assigned to each network adapter:

For Each strAddress in objNetAdapter.IPAddress

Inside this second loop the first thing we do is use the Split function to split each IP address into an array:

arrOctets = Split(strAddress, “.”)

By splitting on the period we end up with an array (named arrOctets) consisting of four separate items, each item corresponding to an octet in the IP address. For example, with an IP address of we end up with these four items in our array:





At this point it’s easy to determine whether or not we are dealing with a “10.10” address; all we have to do is check to see if the first item in the array (item 0) and the second item in the array (item 1) are both equal to 10:

If arrOctets(0) = “10” and arrOctets(1) = “10” Then

Note. Depending on your setup, you might not have to do this. We added this step to ensure that we don’t automatically change every IP address assigned to an adapter; instead, we only change 10.10 addresses. If it’s OK to change every IP address, including addresses that might not be on the 10.10 subnet, then you can leave this If-Then statement out.

Now, what happens when we find a 10.10 address? Well, to begin with, we use this line of code to construct a new IP address:

strNewAddress = “192.168.” & arrOctets(2) & “.” & arrOctets(3)

You can see what we’re doing here. We’re simply taking the string 192.168. and tacking on: 1) the third element in our array (1); 2) a period; and 3) the fourth element in our array (2). That’s going to make strNewAddress equal to, which just happens to be the new IP address we want assigned to this network card.

In order to make that assignment, we add this new address to an array named arrIPAddress, then call the EnableStatic method to assign the new address and subnet mask to the adapter:

arrIPAddress = Array(strNewAddress)
errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)

And that’s all we have to do. We then loop around and repeat the process with the next IP address for the card (assuming there is another such address). When we’re done there we then loop around and do the same thing with the next adapter in the collection.

We should note that all we’ve done here is show you how to assign a new IP address (or addresses, as the case may be) to a computer. In switching IP addresses you might also need to do other tasks, such as changing DNS servers. We won’t cover any of those other tasks in this column, but you can find information on practically everything related to network adapters by looking at Peter Costantini’s epic masterpiece Automating TCP/IP Networking on Clients.

We should also clarify our earlier statement. The truth is that you won’t find any suggestive photos of the Scripting Guys on the Internet because the Scripting Guys don’t actually exist; instead, they are simply a marketing gimmick cooked up by Microsoft. As it turns out, all the material found in the Script Center, including the Hey, Scripting Guy! column, is created by a computer running a trial version of Office 2025.

But that shouldn’t come as a surprise to most of you; we’re sure that you’ve suspected all along that no human being would ever write a daily scripting column like this one.


Discussion is closed.

Feedback usabilla icon