Creating DHCP reservations at the command line

by Ed Fisher on 2008-07-02

in Infrastructure

So we are in the process of migrating an entire network from one ip.addr range to another, and need to create a large number (>100) of reservations across various scopes at each location. While the GUI DHCP Admin tool is adequate to the task, I didn’t want to have to do that one at a time, especially since I was given an Excel spreadsheet with the required information to create the reservations. I figured there had to be a tool like dnscmd.exe for DHCP. After doing some looking, I only found that there was an old NT ResKit tool dhcpcmd.exe, but I had no interest in using that on my 2008 domain.
I did a little poking around with netsh and figured out a way to do this.

  1. Create a CSV file with no header. The four columns should include only the following…
    scope subnet,ip.addr reservation,mac address (no separator characters),name

    For example, if you wanted to reserve 1.2.3.4/24 for a client named workstation1, with a mac.addr of 00-11-22-33-44-55, your file would look like this…

    1.2.3.0,1.2.3.4,001122334455,workstation1

  2. The following command line will do the trick. If using Vista or 2008, run this from an Administrative cmd prompt.

    for /f “tokens=1-4 delims=,” %G IN (list.txt) do netsh dhcp server \\w.x.y.z scope %G add reservedip %H %I %J

To break this down, “tokens=1-4″ means to use all four columns. You could also have said “tokens=*.”
“Delims=,” just means that the delimiting character in the file is a comma.
“%G” sets the first variable…you could have used any letter or number you want as long as you stay in order for the rest.
“list.txt” is of course the name of the CSV file you are going to use.
“netsh dhcp server \\w.x.y.z” executes the command against the dhcp service of server w.x.y.z (you could use an FQDN.)
“scope %G” means to use the scope defined in column A
“add reservedip” means exactly what it says, and that is populated with the values in columns B, C, and D as variables %H %I and %J….ip.addr for mac.addr and name.
Hopefully this will help someone else out…if not, I expect I will need to refer to this again many times over the next few months!

keywords “create dhcp reservations” script “command line” dhcpcmd

You might also enjoy:

  1. Creating DNS records at the command line
  2. Creating an ISO in ESX from a mounted CD/DVD
  3. Scripting AD group creation using dsadd
  4. Disabling DHCP Authorisation checking in Windows

{ 5 comments… read them below or add one }

Yossi 2008-08-14 at 17:51

Thane You ,
very nice script , it was help me to move 150 address to reservation
Yossi

Reply

Ed Fisher 2008-08-14 at 19:54

You’re very welcome…glad it helped!

Reply

kirel 2008-10-13 at 16:19

Thank you greatly for this, Ed.

For reference, this also worked perfectly on my Windows 2003 server.

Reply

Ed Fisher 2008-10-13 at 16:26

Excellent! I’m glad it helped.

Reply

nws 2009-07-16 at 11:48

Fantastic, saved me about an hour of work today (Server 2k3). Much appreciated!

Reply

Leave a Comment

CommentLuv Enabled

Previous post:

Next post: