blib/lib/Net/Connection/ncnetstat.pm

filter: Statementstmt 26.5% 31 / 117 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 83.3% 10 / 12 Podpod 100% 2 / 2 totaltotal 20.8% 43 / 206 CC 33 SCAR 61.6
CC
33
Cov
17%
run
756.0
new
56.0
BEGIN
1.0
CRAP 473.0
↑ Summary
1 package Net::Connection::ncnetstat;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Net::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Net::Connection::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Net::Connection::Sort;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Net::Connection::lsof;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Text::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Net::Connection::ncnetstat - The backend for ncnetstat, the colorized and enhanced netstat like tool.
17  
18 =head1 VERSION
19  
20 Version 0.1.1
21  
22 =cut
23  
24 2 our $VERSION = '0.1.1';
25  
26  
27 =head1 SYNOPSIS
28  
29 use Net::Connection::ncnetstat;
30
31 # Net::Connection::Match filters
32 my @filters=(
33 {
34 type=>'States',
35 invert=>1,
36 args=>{
37 states=>['LISTEN']
38 }
39 }
40 );
41
42 my $ncnetstat=Net::Connection::ncnetstat->new(
43 {
44 ptr=>1,
45 command=>1,
46 command_long=>0,
47 sorter=>{
48 invert=>0,
49 type=>'host_lf',
50 },
51 match=>{
52 checks=>\@filters,
53 }
54 }
55 );
56
57 print $ncnetstat->run;
58  
59 =head1 METHODS
60  
61 =head2 new
62  
63 This initiates the object.
64  
65 my $ncnetstat=Net::Connection::ncnetstat->new( \%args );
66  
67 =head3 args hash ref
68  
69 =head4 command
70  
71 If set to true, it will show the command for the PID.
72  
73 =head4 command_long
74  
75 If set to true, the full command is shown.
76  
77 This requires command also being true.
78  
79 =head4 match
80  
81 This is the hash to pass to L<Net::Connection::Match>.
82  
83 By default this is undef and that module won't be used.
84  
85 =head4 sorter
86  
87 This is what is to be passed to L<Net::Connection::Sorter>.
88  
89 The default is as below.
90  
91 {
92 type=>'host_fl',
93 invert=>0,
94 }
95  
96 =cut
97  
98 sub new{
99 0 ▶ my %args;
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
100 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
101 0 %args= %{$_[1]};
102 };
103  
104 0 ▶ if (! defined( $args{sorter} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"sorter"}) 0 0 0
105 $args{sorter}={
106 0 type=>'host_fl',
107 invert=>0,
108 };
109 }
110  
111 my $self = {
112 invert=>0,
113 0 sorter=>Net::Connection::Sort->new( $args{sorter} ),
114 ptr=>1,
115 command=>0,
116 command_long=>0,
117 };
118 0 bless $self;
119  
120 0 ▶ if ( defined( $args{match} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"match"}) 0 0 0
121 0 $self->{match}=Net::Connection::Match->new( $args{match} );
122 }
123  
124 0 ▶ if ( defined( $args{ptr} )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ptr"}) 0 0 0
125 0 $self->{ptr}=$args{ptr};
126 }
127  
128 0 ▶ if ( defined( $args{command} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"command"}) 0 0 0
129 0 $self->{command}=$args{command};
130 }
131  
132 0 ▶ if ( defined( $args{command_long} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"command_long"}) 0 0 0
133 0 $self->{command_long}=$args{command_long};
134 }
135
136 0 return $self;
137 }
138  
139 =head2 run
140  
141 This runs it and returns a string.
142  
143 print $ncnetstat->run;
144  
145 =cut
146  
147 sub run{
148 0 ▶ my $self=$_[0];
Subroutine run: not called CC=27 SCAR=66.3
Pod documented
149
150 0 my @objects = &lsof_to_nc_objects;
151  
152 0 my @found;
153 0 ▶ if (defined( $self->{match} )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'match'}) { } 0 0 0
154 0 foreach my $conn (@objects){
155 0 ▶ if( $self->{match}->match( $conn ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"match"}->match($conn)) 0 0 0
156 0 push( @found, $conn );
157 }
158 }
159 }else{
160 0 @found=@objects;
161 }
162  
163 0 @found=$self->{sorter}->sorter( \@found );
164  
165 0 my @headers=(
166 color('underline white').'Proto'.color('reset'),
167 color('underline white').'User'.color('reset'),
168 color('underline white').'PID'.color('reset'),
169 color('underline white').'Local Host'.color('reset'),
170 color('underline white').'Port'.color('reset'),
171 color('underline white').'Remote Host'.color('reset'),
172 color('underline white').'Port'.color('reset'),
173 color('underline white').'State'.color('reset'),
174 );
175  
176 0 ▶ if ( $self->{command} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command"}) 0 0 0
177 0 push( @headers, color('underline white').'Command'.color('reset') )
178 }
179  
180 0 my $tb = Text::Table->new( @headers );
181  
182 # process table stuff if needed
183 0 my $ppt;
184 my $proctable;
185 0 my %cmd_cache;
186 0 ▶ if ( $self->{command} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command"}) 0 0 0
187 0 $ppt=Proc::ProcessTable->new;
188 0 $proctable=$ppt->table;
189 }
190  
191 0 my @td;
192 0 foreach my $conn ( @found ){
193 0 my @new_line=(
194 color('bright_yellow').$conn->proto.color('reset'),
195 );
196  
197 # handle adding the username or UID if we have one
198 0 ▶ if ( defined( $conn->username ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->username) { } 0 0 0
199 0 push( @new_line, color('bright_cyan').$conn->username.color('reset'));
200 }else{
201 0 ▶ if ( defined( $conn->uid ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->uid) { } 0 0 0
202 0 push( @new_line, color('bright_cyan').$conn->uid.color('reset'));
203 }else{
204 0 push( @new_line, '');
205 }
206 }
207  
208 # handle adding the PID if we have one
209 0 ▶ if ( defined( $conn->pid ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->pid) { } 0 0 0
210 0 push( @new_line, color('bright_red').$conn->pid.color('reset'));
211 0 $conn->pid;
212 }else{
213 0 push( @new_line, '');
214 }
215  
216 # Figure out what we are using for the local host
217 0 my $local;
218 0 ▶ if ( defined( $conn->local_ptr ) && $self->{ptr} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->local_ptr and $self->{'ptr'}) { } 0 0 0
Condition0%0/3
defined $conn->local_ptr and $self->{"ptr"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conn->local_ptr and $self->{"ptr"}
defined $conn->local_ptr $self->{"ptr"}
Truth table0%0/3
A and B
A= defined $conn->local_ptrB= $self->{"ptr"}
ABresult
0X0
100
111
219 0 $local=$conn->local_ptr;
220 }else{
221 0 $local=$conn->local_host;
222 }
223  
224 # Figure out what we are using for the foriegn host
225 0 my $foreign;
226 0 ▶ if ( defined( $conn->foreign_ptr ) && $self->{ptr} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->foreign_ptr and $self->{'ptr'}) { } 0 0 0
Condition0%0/3
defined $conn->foreign_ptr and $self->{"ptr"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conn->foreign_ptr and $self->{"ptr"}
defined $conn->foreign_ptr $self->{"ptr"}
Truth table0%0/3
A and B
A= defined $conn->foreign_ptrB= $self->{"ptr"}
ABresult
0X0
100
111
227 0 $foreign=$conn->foreign_ptr;
228 }else{
229 0 $foreign=$conn->foreign_host;
230 }
231  
232 # Figure out what we are using for the local port
233 0 my $lport;
234 0 ▶ if ( defined( $conn->local_port_name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->local_port_name) { } 0 0 0
235 0 $lport=$conn->local_port_name;
236 }else{
237 0 $lport=$conn->local_port;
238 }
239  
240 # Figure out what we are using for the foreign port
241 0 my $fport;
242 0 ▶ if ( defined( $conn->foreign_port_name ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conn->foreign_port_name) { } 0 0 0
243 0 $fport=$conn->foreign_port_name;
244 }else{
245 0 $fport=$conn->foreign_port;
246 }
247  
248 0 push(
249 @new_line,
250 color('bright_green').$local.color('reset'),
251 color('green').$lport.color('reset'),
252 color('bright_magenta').$foreign.color('reset'),
253 color('magenta').$fport.color('reset'),
254 color('bright_blue').$conn->state.color('reset'),
255 );
256  
257 # handle the command portion if needed
258 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (defined $conn->pid and $self->{'command'}) { } 0 0 0
elsif (not defined $conn->pid and $self->{'command'}) { } 0 0 0
Condition0%0/3
defined $conn->pid and $self->{"command"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conn->pid and $self->{"command"}
defined $conn->pid $self->{"command"}
Truth table0%0/3
A and B
A= defined $conn->pidB= $self->{"command"}
ABresult
0X0
100
111
259 defined( $conn->pid ) &&
260 $self->{command}
261 ){
262  
263 0 my $loop=1;
264 0 my $proc=0;
265 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proctable->[$proc] and $loop) 0 0 0
Condition0%0/3
defined $proctable->[$proc] and $loop
!ll&&!rl&&r
000
MC/DC0%0/2
defined $proctable->[$proc] and $loop
defined $proctable->[$proc] $loop
Truth table0%0/3
A and B
A= defined $proctable->[$proc]B= $loop
ABresult
0X0
100
111
266 defined( $proctable->[ $proc ] ) &&
267 $loop
268 ){
269 0 my $command;
270 0 ▶ if (defined( $cmd_cache{$conn->pid} ) ){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cmd_cache{$conn->pid}) { } 0 0 0
elsif ($proctable->[$proc]->pid eq $conn->pid) { } 0 0 0
271 0 push( @new_line, color('bright_red').$cmd_cache{$conn->pid}.color('reset') );
272 0 $loop=0;
273 }elsif( $proctable->[ $proc ]->pid eq $conn->pid ){
274 0 ▶ if ( $proctable->[ $proc ]->{'cmndline'} =~ /^$/ ){
Branch: 3 branches
BranchTrueFalseTotal
if ($proctable->[$proc]{'cmndline'} =~ /^$/) { } 0 0 0
elsif ($self->{'command_long'}) { } 0 0 0
elsif ($proctable->[$proc]{'cmndline'} =~ m[^/]) { } 0 0 0
275 #kernel process
276 0 $cmd_cache{$conn->pid}=color('bright_red').'['.$proctable->[ $proc ]->{'fname'}.']'.color('reset');
277 }elsif( $self->{command_long} ){
278 0 $cmd_cache{$conn->pid}=color('bright_red').$proctable->[ $proc ]->{'cmndline'}.color('reset');
279 }elsif( $proctable->[ $proc ]->{'cmndline'} =~ /^\//){
280 # something ran with a complete path
281 0 $cmd_cache{$conn->pid}=color('bright_red').$proctable->[ $proc ]->{'fname'}.color('reset');
282 }else{
283 # likely a thread or the like... such as dovecot/auth
284 # just trunkcat everything after the space
285 0 my $cmd=$proctable->[ $proc ]->{'cmndline'};
286 0 $cmd=~s/\ +.*//g;
287 0 $cmd_cache{$conn->pid}=color('bright_red').$cmd.color('reset');
288 }
289  
290 0 push( @new_line, $cmd_cache{$conn->pid} );
291 0 $loop=0;
292 }
293  
294 0 $proc++;
295 }
296  
297 ▶ }elsif(
Condition0%0/3
not defined $conn->pid and $self->{"command"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $conn->pid and $self->{"command"}
not defined $conn->pid $self->{"command"}
Truth table0%0/3
not(A) and B
A= not defined $conn->pidB= $self->{"command"}
ABresult
0X0
100
111
298 ( !defined( $conn->pid ) ) &&
299 $self->{command}
300 ){
301 0 push( @new_line, '');
302 }
303  
304 0 push( @td, \@new_line );
305 }
306  
307 0 $tb->load( @td );
308  
309 0 return $tb;
310 }
311  
312 =head1 TODO
313  
314 * Add support for more collection methods than L<Net::Connection::lsof>
315  
316 * Support color selection and column ordering.
317  
318 =head1 AUTHOR
319  
320 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
321  
322 =head1 BUGS
323  
324 Please report any bugs or feature requests to C<bug-net-connection-ncnetstat at rt.cpan.org>, or through
325 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-ncnetstat>. I will be notified, and then you'll
326 automatically be notified of progress on your bug as I make changes.
327  
328  
329  
330  
331 =head1 SUPPORT
332  
333 You can find documentation for this module with the perldoc command.
334  
335 perldoc Net::Connection::ncnetstat
336  
337  
338 You can also look for information at:
339  
340 =over 4
341  
342 =item * RT: CPAN's request tracker (report bugs here)
343  
344 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-ncnetstat>
345  
346 =item * AnnoCPAN: Annotated CPAN documentation
347  
348 L<http://annocpan.org/dist/Net-Connection-ncnetstat>
349  
350 =item * CPAN Ratings
351  
352 L<https://cpanratings.perl.org/d/Net-Connection-ncnetstat>
353  
354 =item * Search CPAN
355  
356 L<https://metacpan.org/release/Net-Connection-ncnetstat>
357  
358 =back
359  
360  
361 =head1 ACKNOWLEDGEMENTS
362  
363  
364 =head1 LICENSE AND COPYRIGHT
365  
366 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
367  
368 This is free software, licensed under:
369  
370 The Artistic License 2.0 (GPL Compatible)
371  
372  
373 =cut
374  
375 2 1; # End of Net::Connection::ncnetstat
↑ Summary