Home Command line utilities How to monitor DHCP server usage

How to monitor DHCP server usage

By sk

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 monitor_dhcp.sh.


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 monitor_dhcp.sh

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


Bill August 7, 2016 - 7:40 pm

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

SK August 8, 2016 - 11:24 am

You’re welcome.

Thomas Yobe December 18, 2017 - 8:46 pm

Bill, that link is dead.

Thomas Yobe December 18, 2017 - 7:36 pm

Hi, last time I queried on the DNS issue, my post was deleted. So today I just want to report that http://ravindrayadava.blogspot.com/2016/08/ubuntu-1604-install-monitor-dhcp-server.html copied this article to his blog without crediting you guys.

SK December 18, 2017 - 8:30 pm

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 December 18, 2017 - 8:44 pm

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

SK December 19, 2017 - 11:04 am

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.

Thomas Yobe December 19, 2017 - 12:23 pm

It’s a shame really. Wanting to get credit on other peoples work is not good at all.

Alessandro February 1, 2019 - 7:41 pm

Thank you so much for this script! It’s really useful for us.
I had to correct the path of dhcpd lease file like you did and another thing i suggest to check for you.
In the script i downloaded the screen output of the script as exchanged the active and non active users and i had to change the text to match the correct ones.

Keep up the good work!


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More