blib/lib/MYDan/Util/Check/Port.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 88 CC 10 SCAR 27.4
CC
10
Cov
0%
new
20.0
check
20.0
_port
20.0
CRAP 15.5
1 package MYDan::Util::Check::Port;
2 use strict;
3 use warnings;
4  
5 $|++;
6  
7 sub new
8 {
9 my ( $class, $proto, @port ) = @_;
10  
11 die "not supported.\n" unless $proto && ( $proto eq 'tcp' || $proto eq 'udp' );
12 die "port undef\n" unless @port;
13  
14 map{ die "port $_ format error.\n" unless $_ =~ /^\d+$/ }@port;
15  
16 bless +{ proto => $proto, port => \@port }, ref $class || $class;
17 }
18  
19 sub check
20 {
21 my ( $this, %run, $error ) = @_;
22 my ( $proto, $port ) = @$this{qw( proto port )};
23  
24 my %listen = map{ $_ => 1 } $proto eq 'tcp' ? _tcpport() : _udpport();
25 map{ print "LISTEN $proto $_\n";} keys %listen if $run{debug};
26 map{
27 if( $listen{$_} )
28 {
29 print "$proto $_ listen\n";
30 }
31 else
32 {
33 print "[Error] $proto $_ not listen\n";
34 $error ++;
35 }
36 }@$port;
37  
38 return $error;
39 }
40  
41 sub _tcpport
42 {
43 return _port( `ss -t -l -n` );
44 }
45  
46 sub _udpport
47 {
48 return _port( `ss -u -a -n` );
49 }
50  
51 sub _port
52 {
53 my ( @x, @port )= @_;
54 chomp @x; shift @x;
55  
56 for ( @x )
57 {
58 my @fields = split /\s+/, $_;
59 my $fieldsLen = scalar @fields;
60  
61 die "format not supported.\n" if $fieldsLen != 4 && $fieldsLen != 5;
62  
63 my $index = $fieldsLen == 5 ? 3 : 2;
64 push ( @port, $1 ) if $fields[$index] =~ /:(\d+)$/;
65 }
66  
67 return @port;
68 }
69  
70 1;
71 __END__