]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
[master] Enhance the script to show leases from the lease file
authorShawn Routhier <sar@isc.org>
Tue, 26 Jul 2016 18:22:23 +0000 (11:22 -0700)
committerShawn Routhier <sar@isc.org>
Tue, 26 Jul 2016 18:22:23 +0000 (11:22 -0700)
Squashed commit of the following:

commit ed86441135b2dd001c1175abeaddc7fbfa5b3feb
Author: Shawn Routhier <sar@isc.org>
Date:   Tue Jul 19 20:31:42 2016 -0700

    [rt42113] Enhance the script to look at leases

RELNOTES
contrib/dhcp-lease-list.pl

index f0d822dab6ef5fa8fdb4a7e50480c44dd1fd88e6..2ddd24320526006bd7e0c92984a1a9d72075ea89 100644 (file)
--- a/RELNOTES
+++ b/RELNOTES
@@ -104,6 +104,12 @@ by Eric Young (eay@cryptsoft.com).
   of dhcpd, dhclient, and dhcrelay.
   [ISC-Bugs #42226]
 
+- Updates to contrib/dhcp-lease-list.pl to make it more friendly.
+  They are looking for the lease file in more places and skipping
+  the "processing complete" output when creating machine readable
+  output.
+  [ISC-Bugs #42113]
+
                        Changes since 4.3.4b1
 
 - None
index 3e6fd729491053cfde9872b1108c503a3ada2d2e..aa6372df085e670d6859f3d680ac513c359330df 100644 (file)
 #              optional, sar
 #
 # 2016-01-19 - updated to better trim the manu string and output the hostnames, sar
+#
+# 2016-01-18 - Mainly cosmetics. Eliminated spurious output in "parsable" mode.
+#              Provided for the various conventional lease file locations. (cbp)
 
 use strict;
 use warnings;
 use POSIX qw(strftime);
 
-my $LEASES = '/var/db/dhcpd.leases';
+my @LEASES = ('/var/db/dhcpd.leases', '/var/lib/dhcp/dhcpd.leases', '/var/lib/dhcp3/dhcpd.leases');
 my @all_leases;
 my @leases;
 
@@ -69,7 +72,16 @@ sub check_oui_file() {
 ## Read current leases file into array.
 sub read_dhcpd_leases() {
 
-    open(F, $LEASES) or die("Cannot open $LEASES: $!");
+    my $db;
+    for my $db_cand (@LEASES) {
+        if ( -r $db_cand) {
+    $db = $db_cand;
+    last;
+        }
+    }
+    die("Cannot find leases db") unless defined $db;
+    open(F, $db) or die("Cannot open $db: $!");
+    print("Reading leases from $db\n") if $opt_format eq 'human';
     my $content = join('', <F>);
     close(F);
     @all_leases = split(/lease/, $content);
@@ -86,7 +98,7 @@ sub process_leases() {
     my $gm_now = strftime("%Y/%m/%d %H:%M:%S", gmtime());
     my %tmp_leases; # for sorting and filtering
 
-    my $counter = 1;
+    my $counter = $opt_format eq 'human' ? 1 : 0;
 
     # parse entries
     foreach my $lease (@all_leases) {
@@ -95,14 +107,16 @@ sub process_leases() {
        # skip outdated lines
        next if ($opt_keep eq 'active'  and  $3 lt $gm_now);
 
-       my $percent = (($counter / $total_leases)*100);
-       printf "Processing: %2d%% complete\r", $percent;
-       ++$counter;
+       if ($counter) {
+           my $percent = (($counter / $total_leases)*100);
+           printf "Processing: %2d%% complete\r", $percent;
+           ++$counter;
+       }
 
-        my $hostname = "-NA-";
-        if ($6) {
-            $hostname = $6;
-        }
+       my $hostname = "-NA-";
+       if ($6) {
+           $hostname = $6;
+       }
 
        my $mac = $4;
        my $date_end = $3;
@@ -175,7 +189,9 @@ sub cli_processing() {
                " --last      prints the last (even if end<now) entry for every MAC\n".
                " --all       prints all entries i.e. more than one per MAC\n".
                " --lease     uses the next argument as the name of the lease file\n".
-               "             the default is /var/db/dhcpd.leases\n".
+               "             the default is to try /var/db/dhcpd.leases then\n".
+               "             /var/lib/dhcp/dhcpd.leases then\n".
+               "             /var/lib/dhcp3/dhcpd.leases\n".
                "\n");
            exit(0);
        } elsif ($arg eq '--parsable') {
@@ -185,7 +201,7 @@ sub cli_processing() {
        } elsif ($arg eq '--all') {
            $opt_keep = 'all';
        } elsif ($arg eq '--lease') {
-           $LEASES = shift(@ARGV);
+           unshift @LEASES, shift(@ARGV);
        } else {
            die("Unknown option $arg");
        }