3 # Shows current leases.
5 # THIS SCRIPT IS PUBLIC DOMAIN, NO RIGHTS RESERVED!
7 # I've removed the email addresses of Christian and vom to avoid
8 # putting them on spam lists. If either of you would like to have
9 # your email in here please send mail to the DHCP bugs list at ISC.
11 # 2008-07-13, Christian Hammers
13 # 2009-06-?? - added loading progress counter, pulls hostname, adjusted formatting
16 # 2013-04-22 - added option to choose lease file, made manufacture information
20 use POSIX
qw(strftime);
22 my $LEASES = '/var/db/dhcpd.leases';
26 my @OUIS = ('/usr/share/misc/oui.txt', '/usr/local/etc/oui.txt');
27 my $OUI_URL = 'http://standards.ieee.org/regauth/oui/oui.txt';
32 my $opt_format = 'human';
33 my $opt_keep = 'active';
35 our $total_leases = 0;
37 ## Return manufactorer name for specified MAC address (aa:bb:cc:dd:ee:ff).
38 sub get_manufactorer_for_mac
($) {
42 $manu = join('-', ($_[0] =~ /^(..):(..):(..):/));
43 $manu = `grep -i '$manu' $oui | cut -f3`;
50 ## Read oui.txt or print warning.
51 sub check_oui_file
() {
53 for my $oui_cand (@OUIS) {
60 if (not defined $oui) {
61 print(STDERR
"To get manufacturer names please download $OUI_URL ");
62 print(STDERR
"to /usr/local/etc/oui.txt\n");
66 ## Read current leases file into array.
67 sub read_dhcpd_leases
() {
69 open(F
, $LEASES) or die("Cannot open $LEASES: $!");
70 my $content = join('', <F
>);
72 @all_leases = split(/lease/, $content);
74 foreach my $lease (@all_leases) {
75 if ($lease =~ /^\s+([\.\d]+)\s+{.*starts \d+ ([\/\d\ \
:]+);.*ends \d
+ ([\
/\d\ \:]+);.*ethernet ([a-f0-9:]+);/s) {
81 ## Add manufactor name and sort out obsolet assignements.
82 sub process_leases
() {
83 my $gm_now = strftime
("%Y/%m/%d %H:%M:%S", gmtime());
84 my %tmp_leases; # for sorting and filtering
89 foreach my $lease (@all_leases) {
91 next if not ($lease =~ /^\s+([\.\d]+)\s+{.*starts \d+ ([\/\d\ \
:]+);.*ends \d
+ ([\
/\d\ \:]+);.*ethernet ([a-f0-9:]+);(.*client-hostname \"(\S+)\";)*/s);
93 next if ($opt_keep eq 'active' and $3 lt $gm_now);
95 my $percent = (($counter / $total_leases)*100);
96 printf "Processing: %2d%% complete\r", $percent;
99 my $hostname = "-NA-";
109 'date_end' => $date_end,
111 'hostname' => $hostname,
112 'manu' => get_manufactorer_for_mac
($mac),
115 $entry{'date_begin'} =~ s
#\/#-#g; # long live ISO 8601
116 $entry{'date_end'} =~ s
#\/#-#g;
118 if ($opt_keep eq 'all') {
119 push(@leases, \
%entry);
120 } elsif (not defined $tmp_leases{$mac} or $tmp_leases{$mac}{'date_end'} gt $date_end) {
121 $tmp_leases{$mac} = \
%entry;
125 # In case we used the hash to filtered
127 foreach (sort keys %tmp_leases) {
128 my $h = $tmp_leases{$_};
137 # Output all valid leases.
138 sub output_leases
() {
139 if ($opt_format eq 'human') {
140 printf "%-19s%-16s%-15s%-20s%-20s\n","MAC","IP","hostname","valid until","manufacturer";
141 print("===============================================================================================\n");
144 if ($opt_format eq 'human') {
145 printf("%-19s%-16s%-15s%-20s%-20s\n",
147 $_->{'ip'}, # IP address
148 $_->{'hostname'}, # hostname
149 $_->{'date_end'}, # Date
150 $_->{'manu'}); # manufactor name
152 printf("MAC %s IP %s HOSTNAME %s BEGIN %s END %s MANUFACTURER %s\n",
163 # Commandline Processing.
164 sub cli_processing
() {
165 while (my $arg = shift(@ARGV)) {
166 if ($arg eq '--help') {
168 "Prints active DHCP leases.\n\n".
169 "Usage: $0 [options]\n".
170 " --help shows this help\n".
171 " --parsable machine readable output with full dates\n".
172 " --last prints the last (even if end<now) entry for every MAC\n".
173 " --all prints all entries i.e. more than one per MAC\n".
174 " --lease uses the next argument as the name of the lease file\n".
175 " the default is /var/db/dhcpd.leases\n".
178 } elsif ($arg eq '--parsable') {
179 $opt_format = 'parsable';
180 } elsif ($arg eq '--last') {
182 } elsif ($arg eq '--all') {
184 } elsif ($arg eq '--lease') {
185 $LEASES = shift(@ARGV);
187 die("Unknown option $arg");