Info.pm

filter: Statementstmt 89.1% 66 / 74 Branchbran 63.4% 33 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 79.7% 110 / 138 CC 27 SCAR 23.2
CC
27
Cov
79%
fetch_lease
14.2
new
13.1
fetch_subnet
9.2
CRAP 10.2
↑ Summary Info/Obj.pm »
1 package Net::DHCP::Info;
2  
3 =head1 NAME
4  
5 Net::DHCP::Info - Fast dhcpd.leases and dhcpd.conf parser
6  
7 =head1 NOTE
8  
9 =head2 DEPRECATED
10  
11 This module is not very flexible, and not maintained.
12 Use L<Net::ISC::DHCPd> instead.
13  
14 =head1 VERSION
15  
16 Version 0.11
17  
18 =head1 SYNOPSIS
19  
20 use Net::DHCP::Info;
21  
22 my $conf = Net::DHCP::Info->new($path_to_dhcpd_conf);
23 my $lease = Net::DHCP::Info->new($path_to_dhcpd_leases);
24  
25 while(my $net = $conf->fetch_subnet) {
26 # .. do stuff with $net
27 }
28  
29 while(my $lease = $lease->fetch_lease) {
30 # .. do stuff with $lease
31 }
32  
33 =cut
34  
35 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use Net::DHCP::Info::Obj;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 5 our $VERSION = '0.12';
40 5 our $FIND_NET = qr{^([^s]*)subnet\s+([\d\.]+)\s+netmask\s+([\d\.]+)};
41 5 our $ROUTERS = qr{^\W*option\s+routers\s+([\d\.\s]+)};
42 5 our $RANGE = qr{^\W*range\s+([\d\.]+)\s*([\d\.]*)};
43 5 our $FIND_LEASE = qr{^lease\s([\d\.]+)};
44 5 our $STARTS = qr{^\W+starts\s\d+\s(.+)};
45 5 our $ENDS = qr{^\W+ends\s\d+\s(.+)};
46 5 our $HW_ETHERNET = qr{^\W+hardware\sethernet\s(.+)};
47 5 our $REMOTE_ID = qr{option\sagent.remote-id\s(.+)};
48 5 our $BINDING = qr{^\W+binding\sstate\s(.+)};
49 5 our $HOSTNAME = qr{^\W+client-hostname\s\"([^"]+)\"};
50 5 our $CIRCUIT_ID = qr{^\W+option\sagent.circuit-id\s(.+)};
51 5 our $END = qr{^\}$};
52  
53 5 my %stash;
54  
55 =head2 METHODS
56  
57 =head2 new($file)
58  
59 Object constructor. Takes one argument; a filename to parse. This can be
60 either a dhcpd.conf or dhcpd.leases file.
61  
62 =cut
63  
64 3 ▶sub new {
Subroutine new: called CC=5 SCAR=25.7
Pod documented
65 3 my $class = shift;
66 3 my $file = shift;
67  
68 3 ▶ if(ref $file eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 3 0 3
69 3 return bless $file, $class;
70 }
71 else {
72 0 ▶ return 'file does not exist' unless(-f $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
73 0 ▶ return 'file is not readable' unless(-r $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
74 0 ▶ open(my $self, '<', $file) or return $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $self, "<", $file or return $! 0 0 0
75 0 return bless $self, $class;
76 }
77 }
78  
79 =head2 fetch_subnet
80  
81 This method returns an object of the C<Net::DHCP::Info::Obj> class.
82  
83 =cut
84  
85 1 ▶sub fetch_subnet {
Subroutine fetch_subnet: called CC=9 SCAR=22.2
Pod documented
86 1 my $self = shift;
87 1 my $tab = "";
88 1 my $net;
89  
90 FIND_NET:
91 1 ▶ while(readline $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self)) 2 0 2
92 2 ▶ if(my($tab, @ip) = /$FIND_NET/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = /$Net::DHCP::Info::FIND_NET/mx) 1 1 2
93 1 $net = Net::DHCP::Info::Obj->new(@ip);
94 1 last FIND_NET;
95 }
96 }
97  
98 READ_NET:
99 1 ▶ while(readline $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self)) 4 0 4
100  
101 4 s/\;//mx;
102  
103 4 ▶ if(/$ROUTERS/mx) {
Branch: 3 branches
BranchTrueFalseTotal
if (/$Net::DHCP::Info::ROUTERS/mx) { } 1 3 4
elsif (my(@net) = /$Net::DHCP::Info::RANGE/mx) { } 1 2 3
elsif (/$tab\}/mx) { } 1 1 2
104 1 $net->routers([ split /\s+/mx, $1 ]);
105 }
106 elsif(my @net = /$RANGE/mx) {
107 1 ▶ $net[1] = $net[0] unless($net[1]);
Branch: 1 branch
BranchTrueFalseTotal
unless $net[1] 0 1 1
108 1 $net->add_range(\@net);
109 }
110 elsif(/$tab\}/mx) {
111 1 last READ_NET;
112 }
113 }
114  
115 1 ▶ return ref $net ? $net : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $net ? : 1 0 1
116 }
117  
118 =head2 fetch_lease
119  
120 This method returns an object of the C<Net::DHCP::Info::Obj> class.
121  
122 =cut
123  
124 2 ▶sub fetch_lease {
Subroutine fetch_lease: called CC=13 SCAR=26.5
Pod documented
125 2 my $self = shift;
126 2 my $lease;
127  
128 FIND_LEASE:
129 2 ▶ while(readline $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self)) 2 0 2
130 2 ▶ if(my($ip) = /$FIND_LEASE/mx) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ip) = /$Net::DHCP::Info::FIND_LEASE/mx) 2 0 2
131 2 $lease = Net::DHCP::Info::Obj->new($ip);
132 2 last FIND_LEASE;
133 }
134 }
135  
136 READ_LEASE:
137 2 ▶ while(readline $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $self)) 14 0 14
138  
139 14 s/\;//mx;
140  
141 14 ▶ if(/$STARTS/mx) {
Branch: 8 branches
BranchTrueFalseTotal
if (/$Net::DHCP::Info::STARTS/mx) { } 2 12 14
elsif (/$Net::DHCP::Info::ENDS/mx) { } 2 10 12
elsif (/$Net::DHCP::Info::HW_ETHERNET/mx) { } 2 8 10
elsif (/$Net::DHCP::Info::REMOTE_ID/mx) { } 0 8 8
elsif (/$Net::DHCP::Info::CIRCUIT_ID/mx) { } 0 8 8
elsif (/$Net::DHCP::Info::BINDING/mx) { } 2 6 8
elsif (/$Net::DHCP::Info::HOSTNAME/mx) { } 2 4 6
elsif (/$Net::DHCP::Info::END/mx) { } 2 2 4
142 2 $lease->starts($1);
143 }
144 elsif(/$ENDS/mx) {
145 2 $lease->ends($1);
146 }
147 elsif(/$HW_ETHERNET/mx) {
148 2 $lease->mac( fixmac($1) );
149 }
150 elsif(/$REMOTE_ID/mx) {
151 0 $lease->remote_id( fixmac($1) );
152 }
153 elsif(/$CIRCUIT_ID/mx) {
154 0 $lease->circuit_id( fixmac($1) );
155 }
156 elsif(/$BINDING/mx) {
157 2 $lease->binding($1);
158 }
159 elsif(/$HOSTNAME/mx) {
160 2 $lease->hostname($1);
161 }
162 elsif(/$END/mx) {
163 2 last READ_LEASE;
164 }
165 }
166  
167 2 ▶ return ref $lease ? $lease : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $lease ? : 2 0 2
168 }
169  
170 =head1 FUNCTIONS
171  
172 =head2 fixmac
173  
174 Takes a mac in various formats as an argument, and returns it as a 12 char
175 string.
176  
177 =cut
178  
179 2 ▶sub fixmac {
Subroutine fixmac: called CC=3 SCAR=11.2
Pod documented
180 2 my $mac = shift;
181  
182 2 ▶ $mac = unpack('H*', $mac) if($mac =~ /[\x00-\x1f]/mx);
Branch: 1 branch
BranchTrueFalseTotal
if $mac =~ /[\x00-\x1f]/mx 0 2 2
183 2 $mac =~ y/a-fA-F0-9\://cd;
184 $mac = join "", map {
185 2 my $i = 2 - length($_);
186 12 ▶ ($i < 0) ? $_ : ("0" x $i) .$_;
Branch: 1 branch
BranchTrueFalseTotal
$i < 0 ? : 0 12 12
187 } split /:/mx, $mac;
188  
189 2 return $mac;
190 }
191  
192 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
193 0 my $self = shift;
194 0 delete $stash{$self};
195 }
196  
197 =head1 AUTHOR
198  
199 Jan Henning Thorsen, C<< <pm at flodhest.net> >>
200  
201 =head1 COPYRIGHT & LICENSE
202  
203 Copyright 2007 Jan Henning Thorsen, all rights reserved.
204  
205 This program is free software; you can redistribute it and/or modify it
206 under the same terms as Perl itself.
207  
208 =cut
209  
210 5 1;
↑ Summary Info/Obj.pm »