Friday, September 6, 2013

Windows DHCP Server - Clean ReservedIP Addresses

To clean all the reserved IP addresses on a Windows DHCP server (my-srvr):

1. Dump configuration of that server to origin.cfg by running the following command on windows:

netsh dhcp server \\my-srvr scope 172.40.0.0 dump > C:\origin.cfg


2. Read the config file, get the reserved ip part, remove "Add" with "delete", and then write them to a new config file names new.cfg. Using python to do all of those:


def create_del_ri_cfg(org_path, out_path):
    org_file = open(org_path, 'rb')
    cfg = org_file.read()
    org_file.close()

    ri_pos = cfg.index('#  Start Add ReservedIp')
    cfg = cfg[ri_pos:]
    cfg = cfg.replace('Add', 'delete')
    out_file = open(out_path, 'wb')
    out_file.write(cfg)
    out_file.close()



3. Execute the new config file to delete all reserved ip addresses of my-srvr:

netsh exec C:\new.cfg

(source: http://www.fujitsu.com/fts/products/computing/servers/primergy/os/microsoft/2008-r2-sp1/)