#!/usr/bin/perl -T -w # # $Id: get-current-dhcpd-leases,v 1.2 2003/10/07 02:32:59 suter Exp $ # Copyright (C) 2003 Mark Suter # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. use strict; use Getopt::Long; use Pod::Usage; use Time::Local qw(timegm); $ENV{'PATH'} = '/bin:/usr/bin'; my %opt = (man => 0, help => 0); GetOptions(\%opt, "man", "help") or pod2usage(0); $opt{man} and pod2usage(-exitval => 0, -verbose => 2); $opt{help} and pod2usage(0); $/ = "}\n"; ## Read in each stanza while(<>) { my ($ip, $year, $month, $day, $hour, $minute, $second) = m! lease \s+ ([\d.]+) \s+ { # "lease 192.0.2.1 {" [^\}]+ # ... ends \s+ \d+ \s+ (\d+)/(\d+)/(\d+) \s+ (\d+):(\d+):(\d+); # "ends 1 2001/01/01 00:00:01;" [^\}]+ # ... } # "}" !imsgcx or do { warn "Paragraph not parsed:\n$_\n"; next }; time() < timegm($second, $minute, $hour, $day, $month - 1, $year) and print "$ip\n"; } __END__ =head1 NAME get-current-dhcpd-leases - filter dhcpd.leases for current leases =head1 SYNOPSIS get-current-dhcpd-leases < /var/lib/dhcp/dhcpd.leases =head1 OPTIONS =over 8 =item B<--man> Print the manual page and exit. =item B<--help> Print a brief help message and exit. =back =head1 DESCRIPTION B is a simple filter for the dhcpd.leases file written by the ISC DHCP Daemon. It reads the timestamps and compares them to the current system time, outputing one line per current lease. =head1 EXIT CODES If B exits with a zero exit status and the correct output is on standard output. Nothing else is ever printed to standard output. B will exit with a non-zero exit status if there was a fatal error. Both fatal and non-fatal errors will cause output on standard error. =head1 AUTHOR Mark Suter EFE =head1 COPYRIGHT Copyright (C) 2003 Mark Suter EFE This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or from the following webpage. http://www.gnu.org/licenses/gpl.txt =cut