Windows XP Cookbook (Cookbooks)

Problem

You want to configure a static IP address or DHCP for a connection.

Solution

Using a graphical user interface

  1. From the Control Panel open the Network Connections applet.

  2. Double-click the connection you want to configure.

  3. Click the Properties button.

  4. Double-click Internet Protocol (TCP/IP).

  5. To enable DHCP, select Obtain an IP address automatically. To use a static address, select Use the following IP address. Then configure the IP address, subnet mask, and default gateway.

  6. Click OK until all windows are closed.

Using a command-line interface

The following command configures DHCP for a connection:

> netsh int ip set address name="<ConnectionName>" source=dhcp

Here is an example for configuring the connection named "Local Area Connection" to use DHCP:

> netsh int ip set address name="Local Area Connection" source=dhcp

This configures a connection with a static IP and default gateway:

> netsh int ip set address name="<ConnectionName>" source=static <IP> <Mask> <GateWayIP> <Metric>

This example configures a static IP address for "Local Area Connection":

> netsh int ip set address name="Local Area Connection" source=static 10.3.53.3 255.255.255.0 10.3.53.1 1

Using VBScript

' This code enables DHCP for the specified connection. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." strConnection = "Local Area Connection" ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colNA = objWMI.ExecQuery("select * " & _ " from Win32_NetworkAdapter " & _ " where NetConnectionID = '" & strConnection & "'" ) for each objNA in colNA set colNAConfig = objWMI.ExecQuery _ ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _ objNA.DeviceID & "'} " & _ " WHERE resultClass = win32_NetworkAdapterConfiguration ") for each objNAConfig in colNAConfig if objNAConfig.DHCPEnabled = True then WScript.Echo "DHCP already enabled for " & strConnection else intRC = objNAConfig.EnableDHCP( ) if intRC = 0 then WScript.Echo "DHCP Enabled for " & strConnection elseif intRC = 1 then WScript.Echo "You must reboot to start using DHCP for " & _ else WScript.Echo "There was an error enabling DHCP for " & _ strconnection & ": " & intRC end if end if next next ' This code configures an IP address, subnet mask and default gateway ' for the specified connection. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." strConnection = "Local Area Connection" strIP = Array("1.22.2.2") strMask = Array("255.255.255.0") strGatewayIP = Array("1.2.3.3") ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colNA = objWMI.ExecQuery("select * " & _ " from Win32_NetworkAdapter " & _ " where NetConnectionID = '" & strConnection & "'" ) for each objNA in colNA set colNAConfig = objWMI.ExecQuery _ ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _ objNA.DeviceID & "'} " & _ " WHERE resultClass = win32_NetworkAdapterConfiguration ") for each objNAConfig in colNAConfig intRC = objNAConfig.EnableStatic(strIP,strMask) intRC2 = objNAConfig.SetGateways(strGatewayIP) if intRC = 0 and intRC2 = 0 then WScript.Echo "IP address configured for " & strConnection elseif intRC = 1 or intRC2 = 1 then WScript.Echo "You must reboot for the changes to take effect for " & _ strConnection else WScript.Echo "There was an error configuring IP for " & _ strconnection & ": " & intRC & " and " & intRC2 end if next next

Discussion

If you use static IP addresses, any time you add a new computer to the network, you have to configure an IP address on that computer. However, there is no reason why you can't automate the process using either the netsh command we showed in the command line solution or with WMI. You still have to find an available IP address, which may not be easy to automate depending on your environment, but at least you can provision the IP address in an automated fashion.

Категории