blib/lib/NetworkInfo/Discovery/NetBIOS.pm

filter: Statementstmt 87.7% 50 / 57 Branchbran 50.0% 6 / 12 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 72.9% 70 / 96 CC 14 SCAR 25.1
CC
14
Cov
74%
do_it
26.5
new
4.0
hosts
3.0
CRAP 12.3
↑ Summary
1 package NetworkInfo::Discovery::NetBIOS;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Net::NBName;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use NetworkInfo::Discovery::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶{ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 $VERSION = '0.05';
10 5 @ISA = qw(NetworkInfo::Discovery::Detect);
11 }
12  
13 =head1 NAME
14  
15 NetworkInfo::Discovery::NetBIOS - NetworkInfo::Discovery extension to find NetBIOS services
16  
17 =head1 VERSION
18  
19 Version 0.05
20  
21 =head1 SYNOPSIS
22  
23 use NetworkInfo::Discovery::NetBIOS;
24  
25 my $scanner = new NetworkInfo::Discovery::NetBIOS hosts => [ qw(192.168.0.0/24) ];
26 $scanner->do_it;
27
28 for my $host ($scanner->get_interfaces) {
29 printf "<%s> NetBios(node:%s zone:%s)\n", $host->{ip},
30 $host->{netbios}{node}, $host->{netbios}{zone};
31 }
32  
33  
34 =head1 DESCRIPTION
35  
36 This module is an extension to C<NetworkInfo::Discovery> which can find
37 hosts and services using the NetBIOS protocol.
38  
39 =head1 METHODS
40  
41 =over 4
42  
43 =item new()
44  
45 Create and return a new object.
46  
47 B<Options>
48  
49 =over 4
50  
51 =item *
52  
53 C<hosts> - expects a scalar or an arrayref of IP addresses in CIDR notation
54  
55 =back
56  
57 B<Example>
58  
59 # with a scalar argument
60 my $scanner = new NetworkInfo::Discovery::NetBIOS hosts => '192.168.0.0/24;
61  
62 # with an arrayref
63 my $scanner = new NetworkInfo::Discovery::NetBIOS hosts => [ qw(192.168.0.0/24) ];
64  
65 =cut
66  
67 6 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
68 6 my $class = shift;
69 6 my $self = $class->SUPER::new();
70 6 my %args = @_;
71
72 6 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
150
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
73 6 bless $self, $class;
74
75 # add private fields
76 6 $self->{_hosts_to_scan} = [];
77
78 # treat given arguments
79 6 for my $attr (keys %args) {
80 3 ▶ $self->$attr($args{$attr}) if $self->can($attr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attr) 3 0 3
81 }
82
83 6 return $self
84 }
85  
86 =item do_it()
87  
88 Run the scan.
89  
90 =cut
91  
92 1 ▶sub do_it {
Subroutine do_it: called CC=9 SCAR=32.8
Pod documented
93 1 my $self = shift;
94 1 my @hosts = ();
95 1 my $netbios = new Net::NBName;
96
97 1 for my $host (@{$self->{_hosts_to_scan}}) {
98 1 for my $ip (Net::Netmask->new($host)->enumerate) {
99 # trying to find status information on each IP address
100 1 my %host = ();
101 1 my $status = $netbios->node_status($ip);
102
103 1 ▶ if($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 1 1
104 0 $host{ip} = $ip;
105 0 $host{mac} = $status->mac_address;
106 0 $host{netbios} = {};
107
108 0 for my $rr ($status->names) {
109 0 ▶ $host{netbios}{node} = $rr->name if $rr->suffix == 0x00 and $rr->G eq 'UNIQUE';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->suffix == 0 and $rr->G eq "UNIQUE" 0 0 0
Condition0%0/3
$rr->suffix == 0 and $rr->G eq "UNIQUE"
!ll&&!rl&&r
000
MC/DC0%0/2
$rr->suffix == 0 and $rr->G eq "UNIQUE"
$rr->suffix == 0 $rr->G eq "UNIQUE"
Truth table0%0/3
A and B
A= $rr->suffix == 0B= $rr->G eq "UNIQUE"
ABresult
0X0
100
111
110 0 ▶ $host{netbios}{zone} = $rr->name if $rr->suffix == 0x00 and $rr->G eq 'GROUP';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->suffix == 0 and $rr->G eq "GROUP" 0 0 0
Condition0%0/3
$rr->suffix == 0 and $rr->G eq "GROUP"
!ll&&!rl&&r
000
MC/DC0%0/2
$rr->suffix == 0 and $rr->G eq "GROUP"
$rr->suffix == 0 $rr->G eq "GROUP"
Truth table0%0/3
A and B
A= $rr->suffix == 0B= $rr->G eq "GROUP"
ABresult
0X0
100
111
111 }
112
113 0 push @hosts, { %host };
114 }
115 }
116 }
117
118 # add found hosts
119 1 $self->add_interface(@hosts);
120
121 # return list of found hosts
122 1 return $self->get_interfaces
123 }
124  
125 =item hosts()
126  
127 Add hosts or networks to the scan list. Expects addresses in CIDR notation.
128  
129 B<Examples>
130  
131 $scanner->hosts('192.168.4.53'); # add one host
132 $scanner->hosts('192.168.5.48/29'); # add a subnet
133 $scanner->hosts(qw(192.168.6.0/30 10.0.0.3/28)); # add two subnets
134  
135 =cut
136  
137 9 ▶sub hosts {
Subroutine hosts: called CC=3 SCAR=11.0
Pod documented
138 9 my $self = shift;
139 9 ▶ if(ref $_[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 3 6 9
elsif (ref $_[0]) { } 4 2 6
140 3 push @{$self->{_hosts_to_scan}}, @{$_[0]}
141 } elsif(ref $_[0]) {
142 4 croak "fatal: Don't know how to deal with a ", lc(ref($_[0])), "ref."
143 } else {
144 2 push @{$self->{_hosts_to_scan}}, @_
145 }
146 }
147  
148 =back
149  
150 =head1 DIAGNOSTICS
151  
152 =over 4
153  
154 =item Don't know how to deal with a %sref.
155  
156 B<(F)> C<hosts()> was called with something it can't handle.
157  
158 =back
159  
160 =head1 SEE ALSO
161  
162 L<NetworkInfo::Discovery>, L<Net::NBName>
163  
164 =head1 AUTHOR
165  
166 Sébastien Aperghis-Tramoni, E<lt>sebastien@aperghis.netE<gt>
167  
168 =head1 BUGS
169  
170 Please report any bugs or feature requests to
171 C<bug-networkinfo-discovery-netbios@rt.cpan.org>, or through the web interface at
172 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
173 be notified of progress on your bug as I make changes.
174  
175 =head1 COPYRIGHT & LICENSE
176  
177 Copyright 2004 Sébastien Aperghis-Tramoni, All Rights Reserved.
178  
179 This program is free software; you can redistribute it and/or modify it
180 under the same terms as Perl itself.
181  
182 =cut
183  
184 5 1; # End of NetworkInfo::Discovery::NetBIOS
↑ Summary