Remove building with NOCRYPTO option
[minix.git] / external / bsd / dhcp / dist / contrib / dhcp-lease-list.pl
blobf7e8c0c3fe7fe650335e2becda2da0b763c94c79
1 #!/usr/bin/perl
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
14 # vom
16 # 2013-04-22 - added option to choose lease file, made manufacture information
17 # optional, sar
18 use strict;
19 use warnings;
20 use POSIX qw(strftime);
22 my $LEASES = '/var/db/dhcpd.leases';
23 my @all_leases;
24 my @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';
28 my $oui;
30 my %data;
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($) {
39 my $manu = "-NA-";
41 if (defined $oui) {
42 $manu = join('-', ($_[0] =~ /^(..):(..):(..):/));
43 $manu = `grep -i '$manu' $oui | cut -f3`;
44 chomp($manu);
47 return $manu;
50 ## Read oui.txt or print warning.
51 sub check_oui_file() {
53 for my $oui_cand (@OUIS) {
54 if ( -r $oui_cand) {
55 $oui = $oui_cand;
56 last;
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>);
71 close(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) {
76 ++$total_leases;
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
86 my $counter = 1;
88 # parse entries
89 foreach my $lease (@all_leases) {
90 # skip invalid lines
91 next if not ($lease =~ /^\s+([\.\d]+)\s+{.*starts \d+ ([\/\d\ \:]+);.*ends \d+ ([\/\d\ \:]+);.*ethernet ([a-f0-9:]+);(.*client-hostname \"(\S+)\";)*/s);
92 # skip outdated lines
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;
97 ++$counter;
99 my $hostname = "-NA-";
100 if ($6) {
101 $hostname = $6;
104 my $mac = $4;
105 my $date_end = $3;
106 my %entry = (
107 'ip' => $1,
108 'date_begin' => $2,
109 'date_end' => $date_end,
110 'mac' => $mac,
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
126 if (%tmp_leases) {
127 foreach (sort keys %tmp_leases) {
128 my $h = $tmp_leases{$_};
129 push(@leases, $h);
133 # print "\n";
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");
143 foreach (@leases) {
144 if ($opt_format eq 'human') {
145 printf("%-19s%-16s%-15s%-20s%-20s\n",
146 $_->{'mac'}, # MAC
147 $_->{'ip'}, # IP address
148 $_->{'hostname'}, # hostname
149 $_->{'date_end'}, # Date
150 $_->{'manu'}); # manufactor name
151 } else {
152 printf("MAC %s IP %s HOSTNAME %s BEGIN %s END %s MANUFACTURER %s\n",
153 $_->{'mac'},
154 $_->{'ip'},
155 $_->{'hostname'},
156 $_->{'date_begin'},
157 $_->{'date_end'},
158 $_->{'manu'});
163 # Commandline Processing.
164 sub cli_processing() {
165 while (my $arg = shift(@ARGV)) {
166 if ($arg eq '--help') {
167 print(
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".
176 "\n");
177 exit(0);
178 } elsif ($arg eq '--parsable') {
179 $opt_format = 'parsable';
180 } elsif ($arg eq '--last') {
181 $opt_keep = 'last';
182 } elsif ($arg eq '--all') {
183 $opt_keep = 'all';
184 } elsif ($arg eq '--lease') {
185 $LEASES = shift(@ARGV);
186 } else {
187 die("Unknown option $arg");
193 # main()
195 cli_processing();
196 check_oui_file();
197 read_dhcpd_leases();
198 process_leases();
199 output_leases();