I've installed and configured a DHCP server on CentOS 6.5. I've also added a subnet to the dhcpd.conf file as follows:

subnet netmask {
  option domain-name-servers,;
  default-lease-time 600;
  max-lease-time 7200;
  range dynamic-bootp;
  option broadcast-address;
  option routers;
  option ip-forwarding off;

As you can see the DHCP server can assign only 20 IP addresses. Is it possible to make the DHCP server send an alert to the system administrator after it has assigned all 20 addresses using a shell script?


An option for you is counting number of lease declaration in dhcpd.leases:

dhcpd.leases(5) - Linux man page


dhcpd.leases - DHCP client lease database


the Lease Declaration

lease ip-address { statements... }

Each lease declaration includes the single IP address that has been leased to the
client. The statements within the braces define the duration of the lease and to
whom it is assigned.

So you just count number of line starting with lease to know number of ip address have been assigned:

COUNT=$(grep -c '^lease' /var/lib/dhcpd/dhcpd.leases)

if [[ $COUNT eq 20 ]]
    #do something here

This is not a direct solution but it would seem you could make use of the on commit facility within your DHCP configuration file. Here's an example from this articled titled: Execute a script when ISC DHCP hands out a new lease.

In the dhcpd.conf file you can create actions on various events such as when a lease is given out.

subnet netmask {
    option routers;

    on commit {
        set clip = binary-to-ascii(10, 8, ".", leased-address);
        set clhw = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
        execute("/usr/local/sbin/dhcpevent", "commit", clip, clhw, host-decl-name);

When the above script, dhcpevent, runs it's passed 4 arguments.

execute_statement argv[0] = /usr/local/sbin/dhcpevent
execute_statement argv[1] = commit
execute_statement argv[2] =
execute_statement argv[3] = 11:aa:bb:cc:dd:ee
execute_statement argv[4] = d1.jp

clipw & clhw are variables where, in this example, portions of other meta data has been parsed and stored prior to running the script. These variables are then passed along with other items to the event script.

You could parley this approach into a script where you could keep track of the numbers of IPs that have been leased out or perhaps you could interrogate the actual lease status file that the DHCP server keeps track of this info (/var/lib/dhcpd/dhcpd.leases), and then report if that files has a number of leases that exceeds your quota.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.