Recipe 12.4. Renewing or Releasing a DHCP IP Address


Problem

You want to release or renew an IP address obtained via DHCP.

Solution

Using a graphical user interface

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

  2. Right-click the DHCP-enabled network connection you want to renew and select Repair. This will automatically attempt to renew the connection's IP address.

Using a command-line interface

The following commands renew and release a DHCP IP address, respectively:

> ipconfig /renew > ipconfig /release

With either of these commands you can specify a pattern to match if you want to affect only a subset of adapters. The following command would release the IP address for any adapter that had "Con" (e.g., Local Area Connection 1) in its name:

> ipconfig /renew *Con*

Using VBScript
' This code releases all DHCP IP addresses. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objAdapterConfig = objWMI.Get("Win32_NetworkAdapterConfiguration") intRC = objAdapterConfig.ReleaseDHCPLeaseAll( ) if intRC = 0 then    WScript.Echo "Released all DHCP IP addresses" elseif intRC = 1 then    WScript.Echo "You must reboot to release all DHCP IP addresses" else    WScript.Echo "There was an error releasing all DHCP IP addresses: " & intRC end if ' This code shows performs the same function as the previous example ' but it performs a query for all DHCP enabled IP addresses.  Use this ' if you don't want to release all IP addresses.  Modify the WQL statement ' based on the criteria you need. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colNetworkAdapters = objWMI.ExecQuery _     ("Select * From Win32_NetworkAdapterConfiguration Where DHCPEnabled = True") for each objNetworkConfig in colNetworkAdapters    intRC = objNetworkConfig.ReleaseDHCPLease( )    if intRC = 0 then       WScript.Echo "Released IP address for " & objNetworkConfig.Description    elseif intRC = 1 then       WScript.Echo "You must reboot to release the IP address for " & _                    objNetworkConfig.Description    else       WScript.Echo "There was an error releasing the IP address for " & _                    objNetworkConfig.Description & ": " & intRC    end if next ' This code renews all DHCP IP addresses. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objAdapterConfig = objWMI.Get("Win32_NetworkAdapterConfiguration") intRC = objAdapterConfig.RenewDHCPLeaseAll( ) if intRC = 0 then    WScript.Echo "Renewed all DHCP IP addresses" elseif intRC = 1 then    WScript.Echo "You must reboot to renew all DHCP IP addresses" else    WScript.Echo "There was an error renewing all DHCP IP addresses: " & intRC end if ' This code renews all adapters made by Intel that are installed on the system. ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colNAs = objWMI.ExecQuery("select * " & _                               " from Win32_NetworkAdapter    " & _                               " where manufacturer = 'Intel' " )  for each objNA in colNAs    set colSubNAConfig = objWMI.ExecQuery _       ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _         objNA.DeviceID & "'} " & _       " WHERE resultClass = win32_NetworkAdapterConfiguration ")    for each objNAConfig in colSubNAConfig       if objNAConfig.DHCPEnabled = True then          intRC = objNAConfig.RenewDHCPLease( )          if intRC = 0 then             WScript.Echo "Renewed IP address for " & objNA.Name          elseif intRC = 1 then             WScript.Echo "You must reboot to renew the IP address for " & _                    objNA.Name          else             WScript.Echo "There was an error renewing the IP address for " & _                    objNA.Name & ": " & intRC          end if       end if    next next

Discussion

Fortunately, the whole DHCP release/renew process is automatic and not something you have to do manually. When a client receives a DHCP lease for an IP address, it will automatically attempt to renew that address after 50% of the lease duration. The DHCP Server can grant the renewal, after which the client restarts its lease timer. If the server doesn't respond, the client tries again after 87.5% of the lease duration and then attempts to contact other DHCP Servers.

Even though this process is automatic, there may be times when you need to initiate it yourself especially if you've made network configuration changes. For example, let's say you configured a reservation on your DHCP Server for a particular host. If that host already has an IP address, you'll need to release the current lease and run the renew command to get the new address.



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

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