{"id":65403,"date":"2007-03-02T01:07:00","date_gmt":"2007-03-02T01:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/02\/how-can-i-change-the-first-two-octets-of-an-ip-address\/"},"modified":"2007-03-02T01:07:00","modified_gmt":"2007-03-02T01:07:00","slug":"how-can-i-change-the-first-two-octets-of-an-ip-address","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-first-two-octets-of-an-ip-address\/","title":{"rendered":"How Can I Change the First Two Octets of an IP Address?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>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.<BR><BR>&#8212; LD<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, LD. Before we answer your question we should note that we are well aware that \u201csuggestive\u201d photos of an <I>American Idol<\/I> 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 \u201csuggestive\u201d photos of the Scripting Guys to be found anywhere on the Internet. None.<\/P>\n<P>Not that we didn\u2019t try to post some, mind you. Interestingly enough, however, it turns out that there are some things that even the <I>Internet<\/I> won\u2019t accept!<\/P>\n<P>Now that we\u2019ve got that out of the way let\u2019s 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:<\/P><PRE class=\"codeSample\">10.10.1.2\n<\/PRE>\n<P>You\u2019d like to change these \u201c10.10\u201d IP addresses to \u201c192.168\u201d IP addresses. In other words, you\u2019d like to take an IP address like 10.10.1.2 and turn it into an IP address like this:<\/P><PRE class=\"codeSample\">192.168.1.2\n<\/PRE>\n<P>As you noted, the first two octets change; the last two octets remain the same.<\/P>\n<P>So can you do that using a script? You bet you can:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colNetAdapters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True&#8221;)<\/p>\n<p>arrSubnetMask = Array(&#8220;255.255.255.0&#8221;)<\/p>\n<p>For Each objNetAdapter in colNetAdapters\n    For Each strAddress in objNetAdapter.IPAddress\n        arrOctets = Split(strAddress, &#8220;.&#8221;)\n        If arrOctets(0) = &#8220;10&#8221; and arrOctets(1) = &#8220;10&#8221; Then\n            strNewAddress = &#8220;192.168.&#8221; &amp; arrOctets(2) &amp; &#8220;.&#8221; &amp; arrOctets(3)            \n            arrIPAddress = Array(strNewAddress)\n            errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)\n        End If\n    Next\nNext\n<\/PRE>\n<P>Let\u2019s 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 <I>bunch<\/I> of machines? Of course you can; that\u2019s what our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/templates\/default.mspx\"><B>Multiple\/Remote Computer Scripting Templates<\/B><\/A> are for.<\/P>\n<P>After connecting to the WMI service our next task is to retrieve a collection of all the network adapters installed on the computer. That\u2019s what this little chunk of code is for:<\/P><PRE class=\"codeSample\">Set colNetAdapters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True&#8221;)\n<\/PRE>\n<P>You might have noticed that our query includes the clause <B>Where IPEnabled = True<\/B>. Why did we include that clause? Well, the Windows operating system features all sorts of \u201cvirtual\u201d network adapters, items that get picked up by the <B>Win32_NetworkAdapterConfiguration<\/B> class. These aren\u2019t 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\u2019ve ever run a script that uses the Win32_NetworkAdapterConfiguration class you\u2019ve likely gotten back 10 or more items, even if your computer only has one network card installed. That\u2019s because of all these virtual adapters. By restricting returned data to network adapters where the <B>IPEnabled<\/B> property is True we filter out everything except \u201creal\u201d network cards.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">arrSubnetMask = Array(&#8220;255.255.255.0&#8221;)\n<\/PRE>\n<P>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.<\/P>\n<P>Speaking of assigning new IP addresses, here\u2019s 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:<\/P><PRE class=\"codeSample\">For Each strAddress in objNetAdapter.IPAddress\n<\/PRE>\n<P>Inside this second loop the first thing we do is use the <B>Split<\/B> function to split each IP address into an array:<\/P><PRE class=\"codeSample\">arrOctets = Split(strAddress, &#8220;.&#8221;)\n<\/PRE>\n<P>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 10.10.1.2 we end up with these four items in our array:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>10<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>10<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>1<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>2<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>At this point it\u2019s easy to determine whether or not we are dealing with a \u201c10.10\u201d address; all we have to do is check to see if the first item in the array (item 0) <I>and<\/I> the second item in the array (item 1) are both equal to 10:<\/P><PRE class=\"codeSample\">If arrOctets(0) = &#8220;10&#8221; and arrOctets(1) = &#8220;10&#8221; Then\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E6E\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Depending on your setup, you might not have to do this. We added this step to ensure that we don\u2019t automatically change <I>every<\/I> IP address assigned to an adapter; instead, we only change 10.10 addresses. If it\u2019s 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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:<\/P><PRE class=\"codeSample\">strNewAddress = &#8220;192.168.&#8221; &amp; arrOctets(2) &amp; &#8220;.&#8221; &amp; arrOctets(3)\n<\/PRE>\n<P>You can see what we\u2019re doing here. We\u2019re simply taking the string <B>192.168.<\/B> and tacking on: 1) the third element in our array (<B>1<\/B>); 2) a period; and 3) the fourth element in our array (<B>2<\/B>). That\u2019s going to make strNewAddress equal to <B>192.168.1.2<\/B>, which just happens to be the new IP address we want assigned to this network card.<\/P>\n<P>In order to make that assignment, we add this new address to an array named arrIPAddress, then call the <B>EnableStatic<\/B> method to assign the new address and subnet mask to the adapter:<\/P><PRE class=\"codeSample\">arrIPAddress = Array(strNewAddress)\nerrEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)\n<\/PRE>\n<P>And that\u2019s 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\u2019re done there we then loop around and do the same thing with the next adapter in the collection.<\/P>\n<P>We should note that all we\u2019ve 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\u2019t cover any of those other tasks in this column, but you can find information on practically <I>everything<\/I> related to network adapters by looking at Peter Costantini\u2019s epic masterpiece <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/networking\/default.mspx\"><B>Automating TCP\/IP Networking on Clients<\/B><\/A>.<\/P>\n<P>We should also clarify our earlier statement. The truth is that you won\u2019t find any suggestive photos of the Scripting Guys on the Internet because the Scripting Guys don\u2019t 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 <I>Hey, Scripting Guy!<\/I> column, is created by a computer running a trial version of Office 2025.<\/P>\n<P>But that shouldn\u2019t come as a surprise to most of you; we\u2019re sure that you\u2019ve suspected all along that no human being would <I>ever<\/I> write a daily scripting column like this one.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.&#8212; LD Hey, LD. Before we answer your question we should note that we are well aware that \u201csuggestive\u201d photos of an American Idol contestant have allegedly been uncovered on [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,37,3,5],"class_list":["post-65403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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.&#8212; LD Hey, LD. Before we answer your question we should note that we are well aware that \u201csuggestive\u201d photos of an American Idol contestant have allegedly been uncovered on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65403\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}