Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Changing IP on computers in a specific IP range

Posted on 2008-10-28
3
Medium Priority
?
351 Views
Last Modified: 2012-05-05
Hi,

I'm trying to use a script to change the IP and the gateway on target computers based on an IP range. Attached is the script I made out of copy/paste from Microsoft scripts

This is in a testing environment. The IP range is small: 192.168.9.100, 192.168.101, 192.168.9.102, I just want to see how to make it work

The result is:
1. 192.168.9.100 - could not be reached - this is good, there is no IP like this
2. 192.168.9.101 - the scripts get stuck
3. the script actually changes the IP from 192.168.9.101 to 10.16.9.101, but it never reaches to check 192.168.9.102 and so on, so the script dies, no message, just stuck
4. for some reason the gateway doesn't get changed either - no clue why

I'm running this script on a server with 2 NICs, one NIC in 192.168.9.x and one NIC in 10.16.9.x range, so I can ping the destination computer both before changing the IP and after changing the IP

unfortunately, besides copy/paste, I don't know much scripting, so I need your help

I need to run this soon, in a production environment with about 200 servers and workstations, and changing the IPs manually is only a last resort

Please let me know.

Thanks!
On Error Resume Next
 
intStartingAddress = 100
intEndingAddress = 102
strSubnet = "192.168.9."
 
For i = intStartingAddress to intEndingAddress
    strComputer = strSubnet & i
 
    Set objShell = CreateObject("WScript.Shell")
    strCommand = "%comspec% /c ping -n 2 -w 1000 " & strComputer & ""
    Set objExecObject = objShell.Exec(strCommand)
 
    Do While Not objExecObject.StdOut.AtEndOfStream
        strText = objExecObject.StdOut.ReadAll()
        If Instr(strText, "Reply") > 0 Then
        
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
			Set colNetAdapters = objWMIService.ExecQuery _
    			("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
 
			arrSubnetMask = Array("255.255.252.0")
			arrGateway = Array("10.16.9.1")
			arrGatewayMetric = Array(1)
			
		    For Each objNetCard in colNetCards
		        objNetCard.SetGateways arrGateway, arrGatewayMetric
    		Next
 
			For Each objNetAdapter in colNetAdapters
			    For Each strAddress in objNetAdapter.IPAddress
        			arrOctets = Split(strAddress, ".")
        			If arrOctets(0) = "192" and arrOctets(1) = "168" Then
            			strNewAddress = "10.16." & arrOctets(2) & "." & arrOctets(3)            
            			arrIPAddress = Array(strNewAddress)
            			errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
        			End If
    			Next
			Next
			
            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
 
        Else
            Wscript.Echo strComputer & " could not be reached."
        End If
    Loop
Next

Open in new window

0
Comment
Question by:sblanken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Jared Luker
ID: 22826428
Take off that on error resume next and see if it'll give you an error anywhere.
0
 

Author Comment

by:sblanken
ID: 22826726
ok, so after I took out the first line (On error resume next), I got an error in the Gateway section

   For Each objNetCard in colNetCards
              objNetCard.SetGateways arrGateway, arrGatewayMetric
   Next

it said: Object not a collection

So I changed Netcard with NetAdapter and this part worked, now the gateway part is OK

But it's still stuck in changing the IP part :(. That is it changes the IP but it doesn't go to the next IP in line :(

Thanks for the first hint though! :)
0
 

Accepted Solution

by:
sblanken earned 0 total points
ID: 22827640
so in the end I went with psexec and a local vbs, like this

the BATCH file


rem @echo off
rem @for /L %a in (100,1,102) do @set server_name=192.168.9.%a& x:\IT\Scripts\IP.bat > x:\IT\Scripts\IP.log

net use y: /d
net use m: /d

net use y: \\sf-corp-it-02\software
net use m: \\%server_name%\c$

set server_name
@ping 127.0.0.1 -n 2 -w 1000 > nul

copy /y y:\it\scripts\ip.vbs m:

Y:\Deployment\psexec.exe \\%server_name% -w c:\ cscript c:\ip.vbs

net use y: /d
net use m: /d


the VBS script

strComputer = "."

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

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

arrSubnetMask = Array("255.255.252.0")
arrGateway = Array("10.16.9.1")
arrGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
      objNetAdapter.SetGateways arrGateway, arrGatewayMetric
Next

For Each objNetAdapter in colNetAdapters
    For Each strAddress in objNetAdapter.IPAddress
        arrOctets = Split(strAddress, ".")
        If arrOctets(0) = "192" and arrOctets(1) = "168" Then
            strNewAddress = "10.16." & arrOctets(2) & "." & arrOctets(3)            
            arrIPAddress = Array(strNewAddress)
            errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
        End If
    Next
Next


And this does what I need. Takes some time cuz there is no ping control but it does the job


On the other hand the 'stuck' situation threw an error (after about 6 1/2 min) - i never waited that long in my previous attempts, but this time I was talking to someone and the error was

SWbemObjectEx: The remote procedure call failed.

SO I found this

http://www.windowsnetworking.com/articles_tutorials/Managing-Windows-Networks-Using-Scripts-Part7.html

and then I installed the latest updates from Microsoft on my testing server - hint from the article

Now the script doesn't get stuck, but it changes only the gateway not the IP. It doesn't throw any error, just doesn't change the IP. But I don't mind as long as I have the other method to change the IPs. I'll play with this some more, but for now I'm covered. I wanted to take some time to update this ticket for future reference for other users that need similar things. Thanks!

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question