blib/lib/Net/DHCP/Windows/Netsh/Parse.pm

filter: Statementstmt 90.0% 90 / 100 Branchbran 70.8% 34 / 48 Conditioncond 57.1% 12 / 21 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 79.1% 156 / 197 CC 34 SCAR 25.7
CC
34
Cov
80%
parse
19.9
add_option
12.0
add_scope
9.4
CRAP 13.1
↑ Summary
1 package Net::DHCP::Windows::Netsh::Parse;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::DHCP::Windows::Netsh::Parse - Parses the output from 'netsh dhcp server dump'
11  
12 =head1 VERSION
13  
14 Version 0.1.0
15  
16 =cut
17  
18 2 our $VERSION = '0.1.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Net::DHCP::Windows::Netsh::Parse;
24  
25 my $parser=Net::DHCP::Windows::Netsh::Parse->new;
26
27 eval{
28 $parser->parse( $dump );
29 };
30 if ( $@ ){
31 print "It failed with... ".$@."\n";
32 }
33
34 # no white space
35 my $json=$parser->json(0);
36
37 # now with useful white space
38 $json=$parser->json(0);
39  
40 =head1 METHODS
41  
42 =head2 new
43  
44 This initiates the object.
45  
46 No arguments are taken.
47  
48 my $parser=Net::DHCP::Windows::Netsh::Parse->new;
49  
50 =cut
51  
52 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
53 1 my $self={
54 servers=>{},
55 };
56 1 bless $self;
57  
58 1 return $self;
59 }
60  
61 =head2 parse
62  
63 This parses a dump from netsh.
64  
65 Only one option is taken and that is a string.
66  
67 Nothing is returned. It will die if it fails to parse.
68  
69 eval{
70 $parser->parse( $dump );
71 };
72 if ( $@ ){
73 print "It failed with... ".$@."\n";
74 }
75  
76 =cut
77  
78 1 ▶sub parse{
Subroutine parse: called CC=18 SCAR=29.9
Pod documented
79 1 my $self=$_[0];
80 1 my $data=$_[1];
81  
82 1 ▶ if ( ! defined( $data ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 1 1
83 0 die( 'Nothing defined to parse' );
84 }
85  
86 # break it appart and grab only the relevant lines
87 # removing the pointless comments and blank lines
88 1 my @lines=grep( /^Dhcp\ Server/ , split( /\n/, $data ));
89  
90 # Don'y really care about lines matching like....
91 # Dhcp Server \\winboot Add Class "Default Routing and Remote Access Class" "User class for remote access clients" 525241532e4d6963726f736f6674 0 b
92 # Dhcp Server \\winboot Set DatabaseName "dhcp.mdb"
93 # Dhcp Server \\winboot Add Optiondef 36 "Ethernet Encapsulation" BYTE 0 comment="0=>client should use ENet V2; 1=> IEEE 802.3" 0
94 # Dhcp Server \\winboot v6 Add Class "Microsoft Windows Options" "Microsoft vendor-specific options for Windows Clients" 4d53465420352e30 1 b 311
95 # Dhcp Server \\winboot v6 Add Optiondef 21 "SIP Server Domain Name List " STRING 1 comment="Domain Name of SIP servers available to the client " ""
96 #
97 # set is case sensitive... we want stuff like...
98 # Dhcp Server \\winboot set optionvalue 15 STRING "foo.bar"
99 1 @lines=grep( !/^Dhcp\ Server\ [\\A-Za-z\.0-9]+\ Add\ Class/ , @lines );
100 1 @lines=grep( !/^Dhcp\ Server\ [\\A-Za-z\.0-9]+\ v6\ Add\ Class/ , @lines );
101 1 @lines=grep( !/^Dhcp\ Server\ [\\A-Za-z\.0-9]+\ Set/ , @lines );
102 1 @lines=grep( !/^Dhcp\ Server\ [\\A-Za-z\.0-9]+\ Add\ Optiondef/ , @lines );
103 1 @lines=grep( !/^Dhcp\ Server\ [\\A-Za-z\.0-9]+\ v6\ Add\ Optiondef/ , @lines );
104  
105 1 foreach my $line( @lines ){
106 # these will always be the same, just need to define something there
107 # garbage1=Dhcp garbage2=Server
108 57 my ( $garbage1, $garbage2, $server, $command, $the_rest)=split( /\ +/, $line, 5);
109  
110 57 ▶ if ( $command eq 'set' ){
Branch: 3 branches
BranchTrueFalseTotal
if ($command eq 'set') { } 5 52 57
elsif ($command eq 'add') { } 7 45 52
elsif ($command =~ /^[Ss]cope$/) { } 37 8 45
111 # Dhcp Server \\winboot set optionvalue 15 STRING "foo.bar"
112 # Dhcp Server \\winboot set optionvalue 6 IPADDRESS "10.202.97.1" "10.202.97.2"
113 # Dhcp Server \\winboot set optionvalue 66 STRING "10.93.192.10"
114 # Dhcp Server \\winboot set optionvalue 67 STRING "linux"
115 # Dhcp Server \\winboot set optionvalue 60 STRING "PXEClient"
116 5 my @the_rest=split(/\ +/, $the_rest);
117  
118 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($the_rest[0] eq "optionvalue" and $the_rest[1] =~ /^[0-9]+$/ and defined $the_rest[3]) 5 0 5
Condition33%2/6
$the_rest[0] eq "optionvalue" and $the_rest[1] =~ /^[0-9]+$/ and defined $the_rest[3]
!ll&&!rl&&r
005
$the_rest[0] eq "optionvalue" and $the_rest[1] =~ /^[0-9]+$/
!ll&&!rl&&r
005
MC/DC0%0/3
$the_rest[0] eq "optionvalue" and $the_rest[1] =~ /^[0-9]+$/ and defined $the_rest[3]
$the_rest[0] eq "optionvalue" $the_rest[1] =~ /^[0-9]+$/ defined $the_rest[3]
Truth table25%1/4
A and B and C
A= $the_rest[0] eq "optionvalue"B= $the_rest[1] =~ /^[0-9]+$/C= defined $the_rest[3]
ABCresult
0XX0
10X0
1100
1111
119 ( $the_rest[0] eq 'optionvalue' ) &&
120 ( $the_rest[1] =~ /^[0-9]+$/ ) &&
121 defined( $the_rest[3] )
122 ){
123  
124 5 my @values;
125 5 my $the_rest_location=3;
126 5 ▶ while(defined( $the_rest[$the_rest_location] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $the_rest[$the_rest_location]) 6 5 11
127 6 push(@values, $the_rest[$the_rest_location]);
128 6 $the_rest_location++;
129 }
130  
131 5 $self->add_option($server, 'default', $the_rest[1], \@values);
132 }
133 }elsif( $command eq 'add' ){
134 # Dhcp Server \\winboot add scope 10.40.10.0 255.255.254.0 "it.ord" ""
135 # Dhcp Server \\winboot add scope 10.31.129.248 255.255.255.248 "ipkvm.sjc" "The NEW ipkvm.sjc after 10.93.180.216/29 was swiped"
136 7 my @the_rest=split(/\ +/, $the_rest, 4);
137  
138 7 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($the_rest[0] eq "scope" and defined $the_rest[1] and defined $the_rest[2]) 7 0 7
Condition33%2/6
$the_rest[0] eq "scope" and defined $the_rest[1] and defined $the_rest[2]
!ll&&!rl&&r
007
$the_rest[0] eq "scope" and defined $the_rest[1]
!ll&&!rl&&r
007
MC/DC0%0/3
$the_rest[0] eq "scope" and defined $the_rest[1] and defined $the_rest[2]
$the_rest[0] eq "scope" defined $the_rest[1] defined $the_rest[2]
Truth table25%1/4
A and B and C
A= $the_rest[0] eq "scope"B= defined $the_rest[1]C= defined $the_rest[2]
ABCresult
0XX0
10X0
1100
1111
139 ( $the_rest[0] eq 'scope' ) &&
140 defined( $the_rest[1] ) &&
141 defined( $the_rest[2] )
142 ){
143 7 $self->add_scope($server, $the_rest[1], $the_rest[2], $the_rest[3]);
144 }
145 }elsif( $command =~ /^[Ss]cope$/ ){
146 # Dhcp Server \\winboot Scope 10.31.129.248 Add iprange 10.31.129.251 10.31.129.254
147 # Dhcp Server \\winboot Scope 10.31.110.0 set optionvalue 51 DWORD "1800"
148 # Dhcp Server \\winboot Scope 10.31.110.0 set optionvalue 3 IPADDRESS "10.31.110.1"
149 37 my @the_rest=split(/\ +/, $the_rest);
150  
151 37 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($the_rest[1] eq 'set' and $the_rest[2] eq 'optionvalue') { } 16 21 37
elsif ($the_rest[1] eq 'Add' and $the_rest[2] eq 'iprange') { } 7 28 35
Condition100%3/3
$the_rest[1] eq "set" and $the_rest[2] eq "optionvalue"
!ll&&!rl&&r
71416
MC/DC100%2/2
$the_rest[1] eq "set" and $the_rest[2] eq "optionvalue"
$the_rest[1] eq "set" $the_rest[2] eq "optionvalue"
Truth table100%3/3
A and B
A= $the_rest[1] eq "set"B= $the_rest[2] eq "optionvalue"
ABresult
0X0
100
111
152 ( $the_rest[1] eq 'set' ) &&
153 ( $the_rest[2] eq 'optionvalue' )
154 ){
155 16 my @values;
156  
157 16 my $the_rest_location=5;
158 16 ▶ while(defined( $the_rest[$the_rest_location] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $the_rest[$the_rest_location]) 20 16 36
159 20 push(@values, $the_rest[$the_rest_location]);
160 20 $the_rest_location++;
161 }
162  
163 16 $self->add_option($server, $the_rest[0], $the_rest[3], \@values);
164 ▶ }elsif(
Condition66%2/3
$the_rest[1] eq "Add" and $the_rest[2] eq "iprange"
!ll&&!rl&&r
1407
MC/DC50%1/2
$the_rest[1] eq "Add" and $the_rest[2] eq "iprange"
$the_rest[1] eq "Add" $the_rest[2] eq "iprange"
Truth table66%2/3
A and B
A= $the_rest[1] eq "Add"B= $the_rest[2] eq "iprange"
ABresult
0X0
100
111
165 ( $the_rest[1] eq 'Add' ) &&
166 ( $the_rest[2] eq 'iprange' )
167 ){
168 7 my @values=($the_rest[3].' '.$the_rest[4]);
169 7 $self->add_option($server, $the_rest[0], 'range', \@values);
170 }
171 }
172 }
173 }
174  
175 =head2 hash_ref
176  
177 This returns the current hash reference for the parsed data.
178  
179 my $hash_ref=$parser->hash_ref;
180  
181 =cut
182  
183 1 ▶sub hash_ref{
Subroutine hash_ref: called CC=1 SCAR=0.0
Pod documented
184 1 return $_[0]->{servers};
185 }
186  
187 =head2 json
188  
189 This returns the parsed data as JSON.
190  
191 One option is taken and that is either a 0/1 for
192 if it should be made nice and pretty.
193  
194 # no white space
195 my $json=$parser->json(0);
196
197 # now with useful white space
198 $json=$parser->json(0);
199  
200 =head1 DATA STRUCTURE
201  
202 The structure of it is as below for both the return
203 hash ref or JSON.
204  
205 $hostname=>{$scope}=>{
206 $options=>{
207 $option_id=>[]
208 },
209 mask=>subnet mask,
210 desc=>description,
211 }
212  
213 The $option_id will always be numeric, except for one special
214 case, which is range. That option contains a array of ranges
215 that the scope in question uses with in that subnet. Each item
216 the array represents one range. The format is as below for the
217 string.
218  
219 $start_ip $end_ip
220  
221 Hostname will always have \\ removed, so \\winboot
222 becomes just winboot.
223  
224 $scope is going to be the base address of the subnet.
225  
226 =cut
227  
228 1 ▶sub json{
Subroutine json: called CC=1 SCAR=0.0
Pod documented
229 1 my $self=$_[0];
230 1 my $pretty=$_[1];
231  
232 1 my $json=JSON->new;
233 1 $json->pretty( $pretty );
234  
235 1 return $json->encode( $self->{servers} );
236 }
237  
238 =head1 INTERNAL FUNCTIONS
239  
240 =head2 add_options
241  
242 This adds a option for a scope.
243  
244 $hostname = Hostname of the DHCP server.
245 $scope = scope name
246 $option = DHCP option integer
247 $values = array ref of values
248  
249 $parser->( $hostname, $scope, $option, \@values );
250  
251 =cut
252  
253 28 ▶sub add_option{
Subroutine add_option: called CC=11 SCAR=24.8
Pod undocumented
254 28 my $self=$_[0];
255 28 my $hostname=$_[1];
256 28 my $scope=$_[2];
257 28 my $option=$_[3];
258 28 my $values=$_[4];
259  
260 # make sure we have everything we need
261 # split up so we produce a more useful error
262 28 ▶ if ( !defined( $hostname ) ){
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $hostname) { } 0 28 28
elsif (not defined $scope) { } 0 28 28
elsif (not defined $option) { } 0 28 28
elsif (not defined $values->[0]) { } 0 28 28
263 0 die('No hostname specified');
264 }elsif( !defined( $scope ) ){
265 0 die('No scope specified');
266 }elsif( !defined( $option ) ){
267 0 die('No option specified');
268 }elsif( !defined( $values->[0] ) ){
269 0 die('No option specified');
270 }
271  
272 # skip over lines like this...
273 # Dhcp Server \\winboot Scope 10.40.10.0 set optionvalue 51 DWORD user="Default BOOTP Class" "1800"
274 28 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq 51 and $values->[0] =~ /^[Uu]/) 1 27 28
Condition100%3/3
$option eq 51 and $values->[0] =~ /^[Uu]/
!ll&&!rl&&r
2071
MC/DC100%2/2
$option eq 51 and $values->[0] =~ /^[Uu]/
$option eq 51 $values->[0] =~ /^[Uu]/
Truth table100%3/3
A and B
A= $option eq 51B= $values->[0] =~ /^[Uu]/
ABresult
0X0
100
111
275 ( $option eq '51' ) &&
276 ( $values->[0] =~ /^[Uu]/ )
277 ){
278 1 return 1;
279 }
280  
281 27 $hostname=~s/^\\+//;
282  
283 27 ▶ if ( ! defined( $self->{servers}{$hostname} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"servers"}{$hostname}) 1 26 27
284 1 $self->{servers}{$hostname}={};
285 }
286  
287 27 ▶ if ( ! defined( $self->{servers}{$hostname}{$scope} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"servers"}{$hostname}{$scope}) 1 26 27
288 1 $self->{servers}{$hostname}{$scope}={};
289 }
290  
291 27 ▶ if ( ! defined( $self->{servers}{$hostname}{$scope}{$option} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"servers"}{$hostname}{$scope}{$option}) 27 0 27
292 27 $self->{servers}{$hostname}{$scope}{$option}=[];
293 }
294  
295 # process each value
296 27 foreach my $value ( @{ $values } ){
297 # windows adds " to each of these
298 29 $value=~s/^\"//;
299 29 $value=~s/\"$//;
300 29 push( @{ $self->{servers}{$hostname}{$scope}{$option} }, $value );
301 }
302  
303 27 return 1;
304 }
305  
306 =head2 add_scope
307  
308 This adds a new scope.
309  
310 $hostname = Hostname of the DHCP server.
311 $scope = scope name
312 $mask = subnet mask for the scope
313 $desc = description
314  
315 $parser->( $hostname, $scope, $mask, $desc );
316  
317 =cut
318  
319 7 ▶sub add_scope{
Subroutine add_scope: called CC=7 SCAR=22.4
Pod documented
320 7 my $self=$_[0];
321 7 my $hostname=$_[1];
322 7 my $scope=$_[2];
323 7 my $mask=$_[3];
324 7 my $desc=$_[4];
325  
326 # make sure we have everything we need
327 # split up so we produce a more useful error
328 7 ▶ if ( !defined( $hostname ) ){
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $hostname) { } 0 7 7
elsif (not defined $scope) { } 0 7 7
elsif (not defined $mask) { } 0 7 7
elsif (not defined $desc) { } 0 7 7
329 0 die('No hostname specified');
330 }elsif( !defined( $scope ) ){
331 0 die('No scope specified');
332 }elsif( !defined( $mask ) ){
333 0 die('No subnet mask specified');
334 }elsif( !defined( $desc ) ){
335 0 die('No subnet description specified');
336 }
337  
338 7 $hostname=~s/^\\+//;
339  
340 7 ▶ if ( ! defined( $self->{servers}{$hostname} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"servers"}{$hostname}) 0 7 7
341 0 $self->{servers}{$hostname}={};
342 }
343  
344 7 ▶ if ( ! defined( $self->{servers}{$hostname}{$scope} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"servers"}{$hostname}{$scope}) 7 0 7
345 7 $self->{servers}{$hostname}{$scope}={};
346 }
347  
348 7 $self->{servers}{$hostname}{$scope}{mask}=$mask;
349 7 $self->{servers}{$hostname}{$scope}{desc}=$desc;
350  
351 7 return 1;
352 }
353  
354 =head1 AUTHOR
355  
356 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
357  
358 =head1 BUGS
359  
360 Please report any bugs or feature requests to C<bug-net-dhcp-windows-netsh-parse at rt.cpan.org>, or through
361 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-DHCP-Windows-Netsh-Parse>. I will be notified, and then you'll
362 automatically be notified of progress on your bug as I make changes.
363  
364  
365  
366  
367 =head1 SUPPORT
368  
369 You can find documentation for this module with the perldoc command.
370  
371 perldoc Net::DHCP::Windows::Netsh::Parse
372  
373  
374 You can also look for information at:
375  
376 =over 4
377  
378 =item * RT: CPAN's request tracker (report bugs here)
379  
380 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-DHCP-Windows-Netsh-Parse>
381  
382 =item * AnnoCPAN: Annotated CPAN documentation
383  
384 L<http://annocpan.org/dist/Net-DHCP-Windows-Netsh-Parse>
385  
386 =item * CPAN Ratings
387  
388 L<https://cpanratings.perl.org/d/Net-DHCP-Windows-Netsh-Parse>
389  
390 =item * Search CPAN
391  
392 L<https://metacpan.org/release/Net-DHCP-Windows-Netsh-Parse>
393  
394 =back
395  
396  
397 =head1 ACKNOWLEDGEMENTS
398  
399  
400 =head1 LICENSE AND COPYRIGHT
401  
402 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
403  
404 This is free software, licensed under:
405  
406 The Artistic License 2.0 (GPL Compatible)
407  
408  
409 =cut
410  
411 2 1; # End of Net::DHCP::Windows::Netsh::Parse
↑ Summary