From: Shawn Routhier Date: Tue, 26 Jul 2016 18:22:23 +0000 (-0700) Subject: [master] Enhance the script to show leases from the lease file X-Git-Tag: v4_3_5b1~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=841d9a18add5ade4a6f04bc6f05ea2e632d58d97;p=thirdparty%2Fdhcp.git [master] Enhance the script to show leases from the lease file Squashed commit of the following: commit ed86441135b2dd001c1175abeaddc7fbfa5b3feb Author: Shawn Routhier Date: Tue Jul 19 20:31:42 2016 -0700 [rt42113] Enhance the script to look at leases --- diff --git a/RELNOTES b/RELNOTES index f0d822dab..2ddd24320 100644 --- 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 diff --git a/contrib/dhcp-lease-list.pl b/contrib/dhcp-lease-list.pl index 3e6fd7294..aa6372df0 100644 --- a/contrib/dhcp-lease-list.pl +++ b/contrib/dhcp-lease-list.pl @@ -17,12 +17,15 @@ # 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('', ); 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