Just Enough Developed Infrastructure

Automating Dhcp Management With Omapi

I was looking for a way to automate the configuration of a dhcp server. I wanted to add, remove and modify hosts by scripting it. During that search I came across OMAPI - Object Management Application Programming Interface as a way to interface with the ISC dhcpd server .

Install dhcp3-server + omapi client (Ubuntu):

<% codify(:shell) do %>

To install the server package do:

$ sudo apt-get install dhcp3-server

Now go of and configure the server by changing the files in /etc/dhcp3/

This install omshell a omapi client:

$ sudo apt-get install dhcp3-common $ which omshell /usr/bin/omshell <% end %>

Configure omapi in /etc/dhcp3/dhcpd.conf

The first step is to create a key . We can use the dnssec-keygen command for that <% codify(:shell) do %> $ apt-get -y install bind9utils <% end %>

If you are executing this on a virtual machine, this never completes. This is because the random generation is not enough, so we need to specify the random generator as explained in http://www.pubbs.net/201005/bind/54065-dnssec-keygen-is-waiting-endless.html

<% codify(:shell) do %>

cd /etc/dhcp3

dnssec-keygen -r /dev/urandom -a HMAC-MD5 -b 512 -n HOST omapi_key


ls Komap*

Komapi_key.+157+02059.key Komapi_key.+157+02059.private more Komap* :::::::::::::: Komapi_key.+157+02059.key :::::::::::::: omapi_key. IN KEY 512 3 157 JaHI1mIAWR3RhiaT0Azau+cc1Bv+iD+L+8QgdPpVJW6vD0BTZHuv EpB2 vDP5rj33fScqoFb/HAWCzgwd8Udeag== :::::::::::::: Komapi_key.+157+02059.private :::::::::::::: Private-key-format: v1.3 Algorithm: 157 (HMAC_MD5) Key: JaHI1mIAWR3RhiaT0Azau+cc1Bv+iD+L+8QgdPpVJW6vD0BTZHuvEpB2vDP5rj33fScqoFb/HAW Czgwd8Udeag== Bits: AAA= Created: 20101208134727 Publish: 20101208134727 Activate: 20101208134727

<% end %>

What we require is the KEY

<% codify(:shell) do %>

KEY=cat /etc/dhcp3/Komapi_key*.private|grep ^Key|cut -d ' ' -f2-

echo $KEY

JaHI1mIAWR3RhiaT0Azau+cc1Bv+iD+L+8QgdPpVJW6vD0BTZHuvEpB2vDP5rj33fScqoFb/HAWCzgwd8Udeag== <% end %>

Now we can add the following snippet to the /etc/dhcp3/dchpd.conf

<% codify(:shell) do %> omapi-port 9991; key omapi_key { algorithm HMAC-MD5; secret "JaHI1mIAWR3RhiaT0Azau+cc1Bv+iD+L+8QgdPpVJW6vD0BTZHuvEpB2vDP5rj33fScqoFb/HAWCzgwd8Udeag=="; }; omapi-key omapi_key;

<% end %>

Creating entries using omshell

To create a new entry as described in http://manpages.ubuntu.com/manpages/lucid/man1/omshell.1.html

<% codify(:shell) do %>

#Creating a new entry in the file $ OMMAC="00:0c:29:c5:c0:cc" $ OMMIP="" $ OMNAME="myname" $ cat <

The entries will NOT be written to your /etc/dhcp3/dhcpd.conf, but in /var/lib/dhcp3/dhcpd.leases

Creating entries using java

By accident I stumbled upon a java implementation of the OMAPI protocol. It was first announced on the isc.org mailing by talamonso@gmx.net and it was the first version http://talamonso.net/omapi/omapi_v.0.1.zip.

Ramandeepk moved the project to sourceforge http://omapi-dhcp.sourceforge.net/ . I tried to checkout the source but somehow this always ended up being empty.

<% codify(:shell) do %> $ svn checkout http://omapi-dhcp.svn.sourceforge.net/svnroot/omapi-dhcp/ omapi-dhcp <% end %>

Therefore I created a omapi-dhcp github project with the same content, ready to be hacked away :)


The code comes with several examples

The example below registers a new host called albert with an IP addresses and the mac address specified:

<% codify(:java) do %> public static void main(final String[] args) { Connection c = Default.getC(); try { Host h = new Host(c); h.setName("albert"); h.setIPAddress(""); h.setHardwareAddress("00:00:00:00:00:11"); h.setHardwareType(1); Host remote = h.send(Message.CREATE); System.out.println(remote); } catch (OmapiException e) { System.err.println(e.getMessage()); } c.close(); } <% end %>