Skip to content

Instantly share code, notes, and snippets.

@ashutoshsmaurya
Created November 5, 2017 05:23
Show Gist options
  • Save ashutoshsmaurya/aeca495723e91228e187d52657d41a1d to your computer and use it in GitHub Desktop.
Save ashutoshsmaurya/aeca495723e91228e187d52657d41a1d to your computer and use it in GitHub Desktop.
Monitoring DHCP usage
#!/bin/bash
rm /tmp/showdhcp.log &
rm /tmp/tmp* &
tempFile=$(mktemp)
cat /var/lib/dhcpd/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* &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment