DevOps

Creating a DHCP Server with vboxapi

While working on a project for work recently I stumbled across the need to create a network and associated DHCP server. The API documentation however only gave me the following reference to start the DHCP server.

void start (in wstring networkName, in wstring trunkName, in wstring trunkType)

trunkName and trunkType are not really documented in the IDHCPServer API page. Here are their documentation as I have discovered.

  • networkName: This is the IHostOnlyInterface.networkName property
  • trunkName: This should really be adapterName, as putting the IHostOnlyInterface.name property is correct
  • trunkType: This is one of the following TRUNKTYPE_* constants
    • These constants don’t exist in the VirtualBox_constants.py, but can be found in the NetworkServiceRunner.h
      • TRUNKTYPE_WHATEVER = ‘whatever’
      • TRUNKTYPE_NETFLT = ‘netflt’
      • TRUNKTYPE_NETADP = ‘netadp’
      • TRUNKTYPE_SRVNAT = ‘srvnat’

Putting this all together I ended up with code similar to the following

from vboxapi import VirtualBoxManager

# configuration
addresss     = '192.168.8.8'
netmask      = '255.255.255.0'
dhcp_from_ip = '192.168.8.100'
dhcp_to_ip   = '192.168.8.150'

## Configure a HostOnly Interface and the associated DHCP Server
vbm = VirtualBoxManager(None, None)
vb  = vbm.vbox

# create the network on the host
(progress, interface) = vb.host.createHostOnlyNetworkInterface()
interface.enableStaticIPConfig(address, netmask)

# create the dhcp server for this network
dhcpServer = vb.createDHCPServer(interface.networkName)
dhcpServer.setConfiguration(address, netmask, dhcp_from_ip, dhcp_to_ip)
dhcpServer.enabled = True
dhcpServer.start(interface.networkName, interface.name, 'netadp')

Leave a reply