Thursday, August 1, 2013

M$ DHCP Server - Reserved IPs synchronization between servers

Although the M$ DHCP Server does not allow you to synchronize the reserved IP(s) with other DHCP servers, but If you want, you can do it with a small trick:

1. Export the dhcp config of the master server by the command (in the command prompt windows):

netsh dhcp server \\master-server-name scope 172.90.0.0 > C:\master.cfg


The config file are something like this:




2. Creat config file (subserver.cfg) for other servers from the dumbed file above using python script:

+ Open the master.cfg file and extract the Reserved IP part of the configuration.
+ Replace the master-server-name part with subsever-name from the content.
+ Create a new file (subserver.cfg) with the new content.

3. Execute the created config file for the sub-servers:

netsh exec C:\subserver.cfg


In addition, you can make a batch file and create a task schedule in the master server to execute the batch file everyday (or even every hour) to synchronize the reserved IP(s) setting between DHCP servers.

Example:

+ dumpdhcp.bat:

netsh dhcp server \\master-server-name dump > C:\master_fullbackup.cfg
netsh dhcp server \\sub-server-name dump > C:\sub_fullbackup.cfg
netsh dhcp server \\master-server-name scope 172.90.0.0 dump > C:\master.cfg
C:\Python26\python C:\create_cfg.py
netsh exec C:\sub.cfg



 + create_cfg.py:

MASTER_PATH = 'C:\\master.cfg'
SUB_PATH = 'C:\\sub.cfg'


def create_ri_cfg(master_cfg_path, sub_cfg_path):
    fa = open(master_cfg_path, 'rb')
    tmp = ''
    while 'ReservedIp' not in tmp:
        tmp = tmp + fa.readline()
    ria_cfg = ''
    while 'ReservedIp' not in ria_cfg:
        ria_cfg = ria_cfg + fa.readline()
    fa.close()

    rib_cfg = ria_cfg.replace('MASTER-SERVER-NAME', 'SUB-SERVER-NAME')
    fb = open(sub_cfg_path, 'wb')
    fb.write(rib_cfg)
    fb.close()
 

if __name__ == "__main__":
    create_ri_cfg(MASTER_PATH, SUB_PATH)