[Home] [Blog] [Contact] - [Talks] [Bio] [Customers]
twitter linkedin youtube github rss

Patrick Debois

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)

# 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

Configure omapi in /etc/dhcp3/dhcpd.conf

The first step is to create a key . We can use the dnssec-keygen command for that

$ apt-get -y install bind9utils

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

# cd /etc/dhcp3
# dnssec-keygen -r /dev/urandom  -a HMAC-MD5 -b 512 -n HOST omapi_key
Komapi_key.+157+02059
# 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

What we require is the KEY

# KEY=`cat /etc/dhcp3/Komapi_key*.private|grep ^Key|cut -d ' ' -f2-`
# echo $KEY
JaHI1mIAWR3RhiaT0Azau+cc1Bv+iD+L+8QgdPpVJW6vD0BTZHuvEpB2vDP5rj33fScqoFb/HAWCzgwd8Udeag==

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

omapi-port 9991;
        key omapi_key {
        algorithm HMAC-MD5;
        secret "JaHI1mIAWR3RhiaT0Azau+cc1Bv+iD+L+8QgdPpVJW6vD0BTZHuvEpB2vDP5rj33fScqoFb/HAWCzgwd8Udeag==";
};
omapi-key omapi_key;

Creating entries using omshell

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

#Creating a new entry in the file
$ OMMAC="00:0c:29:c5:c0:cc"
$ OMMIP="192.168.4.150"
$ OMNAME="myname"
$ cat <<EOF |omshell
port 9991
key omapi_key "$KEY"
server $OMIP
connect
new "host"
set name="$OMNAME"
set hardware-address=$OMMAC
set hardware-type=1
create
EOF

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.

$ svn checkout  http://omapi-dhcp.svn.sourceforge.net/svnroot/omapi-dhcp/ omapi-dhcp

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

https://github.com/jedi4ever/omapi-dhcp

The code comes with several examples

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

public static void main(final String[] args) {
Connection c = Default.getC();
try {
	Host h = new Host(c);
	h.setName("albert");
	h.setIPAddress("1.2.3.4");
	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();
}