How to monitor DHCP server usage

We have published an article yesterday about how to install and configure DHCP server in Ubuntu 16.04 LTS server. In this article, we will see how to monitor DHCP server’s usage with a help of a simple script written by Mr.Ashutosh, one of our friend and regular reader of OSTechNix. This script analyzes the ‘/var/lib/dhcp/dhcpd.leases’ file in the DHCP server, and displays the details, such as what leases are active, who are using them etc.

Monitor DHCP server usage

I have tested this script in Ubuntu 16.04 LTS server. It worked just fine. Please note that the location of dhcpd.leases file might be different in some Linux distributions. You need to mention correct file name in the server. In Ubuntu 16.04 LTS server, you can find correct location of dhcpd.leases file under /var/lib/dhcp/ location. The author have mentioned /var/lib/dhcpd/dhcpd.leases in this script. It doesn’t work for me. I replaced the path with correct location i.e /var/lib/dhcp/dhcpd.leases in Ubuntu 16.04 systems.

Here I have included the complete script. Just coy and paste it to a text file and save it with .sh extension. For the purpose of this tutorial, I have it as


rm /tmp/showdhcp.log &
rm /tmp/tmp* &

cat /var/lib/dhcp/dhcpd.leases \
  | grep \
      -e '^lease ' \
      -e '^  binding state' \
      -e hardware \
      -e client-hostname \
      -e } >$tempFile.1

sed -e "s/^lease \(.*\) {$/\1~/" \
    -e "s/^  binding state \(.*\);$/\1~/" \
    -e "s/^  hardware ethernet \(.*\);$/\1~/" \
    -e "s/  client-hostname \(.*\);$/\1~/" \
    -e "s/^}//" \
    $tempFile.1 > $tempFile.2

sed -e ":'loop';/~$/N;s/~\n/,/;t'loop'" \
    -e "s/,$//" \
    -e "s/,/\t/g" \
    $tempFile.2 | sort > $tempFile.3

sed -e'$!N;/^\(.*\)\n\1$/!P;D' $tempFile.3 > $tempFile.4

print=$(printf "IP Addr\t\tStatus\t\tMAC\t\tHost Name\n")
cat $tempFile.4 > /tmp/showdhcp.log &
sed -i "1i $print" /tmp/showdhcp.log 
#rm -rvf /tmp/Non_Active_User*
#rm -rvf /tmp/Active_User*
#cp -rvf $tempFile.4 /tmp/showdhcp.log &
free=$(grep -o -w 'free' /tmp/showdhcp.log | wc -w)
active=$(grep -o -w 'active' /tmp/showdhcp.log | wc -w)
echo -e ""
echo -e "--------- Total Count- Active & Non Active -----------"
echo -e ""
echo -e "Total Active User :"       $free
echo -e "Total Non Active User :"   $active
echo -e "------------------------------------------------------"
#sed -n '/free/p' /tmp/showdhcp.log > /tmp/Non_Active_User_$(date +"%Y-%m-%d__%H%M%S").log
#sed -n '/active/p' /tmp/showdhcp.log > /tmp/Active_User_$(date +"%Y-%m-%d__%H%M%S").log
rm $tempFile.* 
#rm -rvf /tmp/showdhcp.log
#rm -rvf /tmp/tmp* &

Or, Download the script from the following location.

Once downloaded, make it as executable using as shown below.

sudo chmod +x

Now, run the following command to monitor DHCP server’s usage.


Sample output:

--------- Total Count- Active & Non Active -----------

Total Active User : 2
Total Non Active User : 1

ostechnix@ubuntuserver: ~_010

As you see in the above output, there are two active users, and one non-active user.

Alternatively, you can check the DHCP client details using the log file.

cat /tmp/showdhcp.log

Sample output:

IP Addr        Status      MAC     Host Name   free    xx:xx:xx:xx:xx:xx   active  xx:xx:xx:xx:xx:xx   "sk"   free    xx:xx:xx:xx:xx:xx

ostechnix@ubuntuserver: ~_011

For any further details and questions about this script, contact Mr.Ashutosh.

That’s all for today. Hope this script might useful for you.

Happy weekend! Cheers!!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

8 Responses

  1. Bill says:

    For campus-wide DHCP server monitoring, I use dhcpd-pools ( But for simple monitoring, this tool you’ve presented is cool! Thanks.

  2. Thomas Yobe says:

    Hi, last time I queried on the DNS issue, my post was deleted. So today I just want to report that copied this article to his blog without crediting you guys.

    • SK says:

      Thanks. I left a comment on the blog. I didn’t receive any query from you. Everyday, I receive a lot of comments. I couldn’t reply to them all. Sorry.

      • Thomas Yobe says:

        No Worries, I managed. I had a SERVERFAIL on my Secondary DNS server. However I have a query. I will email you.

        • SK says:

          Glad you fixed the problem on your own. BTW, I did a quick glance at the blog you mentioned earlier. He not only copy/paste from my blog but also from so many other blogs. 99% of the guides posted there are complete cop/paste from many blogs and websites.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.