112014

Display DHCP Leases On-Screen Andrew Mallett | Posted November 2014

dhcpd leases on-screenThe ISC DHCP produces a log showing all leases given out to systems on the network. This script displays leases given out to known systems on screen in real time (well..in my case every minute, using cron).

You will need to be familiar with the output of ISC DCHP logs.

As a prerequisite the following script has DHCP recording to its own log file, in this case /sc/dhcplog/dhcpd.log. The log is rotated (archived and reset) every day.

#!/bin/sh

cd /sc/dhcplog

iphost=`diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 55-56`

if [ "$iphost" = "1" ]; then
echo `diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 3-14,41-87` "(ANDYXP)" >> /dev/pts/0

elif [ "$iphost" = "12" ]; then
echo `diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 3-14,41-87` "(PS3)" >> /dev/pts/0

....


else
echo `diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 3-14,41-87 > double.txt`
fi

sizediff=`diff --brief dhcpd.log dhcpd.last | cut -f5 -d" "`
if [ "$sizediff" = "differ" ]
then

cat double.txt >> /dev/pts/0
rm double.txt

else
echo
fi

rm dhcpd.last

cp dhcpd.log dhcpd.last

Line 2: The script first changes to the working DHCP log directory, /sc/dhcplog..

Line 3: It then creates a variable iphost, using the diff command. Diff indicates differences between two files; the current dhcpd.log and a copy of this file, dhcpd.last which was created when this script last ran (see the last line of the script) - a minute ago. If a lease has been given out, the dhcpd.log will be contain more entries than its copy, dhcpd.last

The script uses grep to find the DHCPACK line and uses cut -c 55-56 to grab the last two digits of the IP Address which was given out.

Lines 4 & 5: The if command tests to see if the iphost variable is "1" (which would be true if the IP Address given out was 192.168.0.1). If this is true, it uses diff to grep the DHCPACK line and cut to grab parts of the line (date, time, IP Address, MAC Address) plus the host's name (ANDYXP) and echoes (appends) this to screen (in this case /dev/pts/0) .

Lines 6 & 7: If the iphost variable does not equal '1' then elif is used to to see if it equals '12' (192.168.0.12). If this is true then the details plus the host name (PS3) are echoed to screen.

At this point (....) various subsequent elif lines can be added to test and echo the details of other systems. Ensure that the IP numbers it tests for are incremental (i.e. each number is larger than the previous one).


#!/bin/sh

cd /sc/dhcplog

iphost=`diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 55-56`

if [ "$iphost" = "1" ]; then
echo `diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 3-14,41-87` "(ANDYXP)" >> /dev/pts/0

elif [ "$iphost" = "12" ]; then
echo `diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 3-14,41-87` "(PS3)" >> /dev/pts/0

....


else
echo `diff dhcpd.log dhcpd.last | grep DHCPACK | cut -c 3-14,41-87 > double.txt`
fi

sizediff=`diff --brief dhcpd.log dhcpd.last | cut -f5 -d" "`
if [ "$sizediff" = "differ" ]
then

cat double.txt >> /dev/pts/0
rm double.txt

else
echo
fi

rm dhcpd.last

cp dhcpd.log dhcpd.last

In the second half of the script the else command kicks in (as part of the if/then/elif/else/fi loop) if it doesn't find a match for the previous IP Address numbers. This typically happens where there is a difference in size between dhcpd.log and dhcpd.last and there is more than one DHCPACK line, indicating more than one IP Address has been given out since the script last ran. In this case all new lines containing the DHCPACK statement are cut and output to a new, temporary file called double.txt.

The first loop is then ended with the fi command.

However it may be true that no IP leases have been given out since the script last ran. In this case the log files would still be identical in size. This can be tested using the diff command with the "--brief" switch..


diff --brief dhcpd.log dhcpd.last

Files dhcpd.log and dhcpd.last differ

Using diff we declare a variable sizediff by running the above command and cutting out all but the last word, "differ". The script then runs another if command to determine whether $sizediff equals the last word, differ.

Note that if the files are the same then diff will output nothing. So the script then tests for this..

If $sizediff = "differ" (indicating the log file has changed) then the contents of double.txt is echoed to screen using cat and then the file double.txt is deleted.

However if $sizediff doesn't equal "differ" (indicating the log file hasn't changed) the else command basically echoes nothing and the loop finishes with fi

Finally the dhcpd.last file is deleted and recreated by copying it from dhcpd.log.

Back

Comments (2)

Great way to keep an eye on Ip allocations in real time
#1 - Bludger - 12/02/2014 - 08:16
Screen
Echoing messages to screen - so simple Iove it.
#2 - Sundowner - 04/04/2015 - 22:27
Name
E-mail (Will not appear online)
Homepage
Title
Comment
;-) :-) :-D :-( :-o :-O B-) :oops: :-[] :-P
To prevent automated Bots form spamming, please enter the text you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
»
This comment form is powered by GentleSource Comment Script. It can be included in PHP or HTML files and allows visitors to leave comments on the website.

PHP Scripts   Temporary Email   Disposable Email