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 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 =

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

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

netsh exec C:\new.cfg