Hack 42 Change a Network Adapter s IP Info

 < Day Day Up > 

Hack 42 Change a Network Adapter's IP Info

figs/beginner.gif figs/hack42.gif

Changing TCP/IP settings via the GUI is tedious at best. It's accomplished more easily with a little VB scripting magic .

Changing a machine's TCP/IP settings from the GUI usually involves a number of steps. This becomes tedious if you have to do it oftenfor example, if the machine is part of a testbed network where you test different deployment scenarios. Using the VBScript in this hack, you can quickly and frequently modify the network adapter information on a computer.

The Code

To use this script, type it into Notepad (with Word Wrap turned off) and save it with a .vbs extension as ChangeIP.vbs :

 Option Explicit Dim NetworkAdapter, AdapterConfiguration 'Objects Dim IPAddress, SubnetMask, Gateway, DNS 'String Arrays Dim RetVal 'Integers For Each NetworkAdapter In GetObject("winmgmts:").InstancesOf("Win32_NetworkAdapter") If NetworkAdapter.AdapterType = "Ethernet 802.3" Then For Each AdapterConfiguration In GetObject("winmgmts:").InstancesOf ("Win32_NetworkAdapterConfiguration") If UCase(AdapterConfiguration.ServiceName) = UCase(NetworkAdapter.ServiceName) Then IPAddress = Array("192.168.0.10") SubnetMask = Array("255.255.255.0") Gateway = Array("192.168.0.1") DNS = Array("35.8.2.41") RetVal = AdapterConfiguration.EnableStatic(IPAddress, SubnetMask) If Not RetVal = 0 Then WScript.Echo "Failure assigning IP/Subnetmask." End If RetVal = AdapterConfiguration.SetGateways(Gateway) If Not RetVal = 0 Then WScript.Echo "Failure assigning Gateway." End If RetVal = AdapterConfiguration.SetDnsServerSearchOrder(DNS) If Not RetVal = 0 Then WScript.Echo "Failure assinging DNS search order." End If End If Next End If Next 

Running the Hack

To run this hack, modify the IP information in the following lines, as required by your environment:

 IPAddress = Array("192.168.0.10") SubnetMask = Array("255.255.255.0") Gateway = Array("192.168.0.1") DNS = Array("35.8.2.41") 

For example, to change the IP address of a machine to 172.16.44.3 with subnet mask 255.255.0.0 and default gateway 172.16.44.1, replace those lines with these:

 IPAddress = Array("  172.16.44.3  ") SubnetMask = Array("  255.255.0.0  ") Gateway = Array("  172.16.44.1  ") 

Also, note this statement:

 If NetworkAdapter.AdapterType = "Ethernet 802.3" Then 

This is where the script checks the AdapterType , which in this script is listed as " Ethernet 802.3 ". You should modify this line if you have a different networking environment.

Once these changes have been made to the script, create a shortcut to the script and double-click on the shortcut to run the script.

Rod Trent

 < Day Day Up > 


Windows Server Hacks
Windows Server Hacks
ISBN: 0596006470
EAN: 2147483647
Year: 2004
Pages: 163
Authors: Mitch Tulloch

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net