mirror of
https://gitlab.isc.org/isc-projects/dhcp
synced 2025-08-31 14:25:41 +00:00
[master] Enhance the script to show leases from the lease file
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
This commit is contained in:
6
RELNOTES
6
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
|
||||
|
@@ -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('', <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");
|
||||
}
|
||||
|
Reference in New Issue
Block a user