| 1 | package Net::Connection::Match::Command; | ||||||||||||||||||||||||||||
| 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 Proc::ProcessTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Net::Connection::Match::Command - Check if the process command matches fix regexp for a Net::Connection object. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.1.0 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use Net::Connection::Match::Command; | ||||||||||||||||||||||||||||
| 23 | use Net::Connection; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $connection_args={ | ||||||||||||||||||||||||||||
| 26 | foreign_host=>'10.0.0.1', | ||||||||||||||||||||||||||||
| 27 | foreign_port=>'22', | ||||||||||||||||||||||||||||
| 28 | local_host=>'10.0.0.2', | ||||||||||||||||||||||||||||
| 29 | local_port=>'12322', | ||||||||||||||||||||||||||||
| 30 | proto=>'tcp4', | ||||||||||||||||||||||||||||
| 31 | state=>'ESTABLISHED', | ||||||||||||||||||||||||||||
| 32 | pid=>0, | ||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $conn=Net::Connection->new( $connection_args ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my %args=( | ||||||||||||||||||||||||||||
| 38 | commands=>[ | ||||||||||||||||||||||||||||
| 39 | 'kernel', | ||||||||||||||||||||||||||||
| 40 | ], | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my $checker=Net::Connection::Match::Command->new( \%args ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | if ( $checker->match( $conn ) ){ | ||||||||||||||||||||||||||||
| 46 | print "It matches.\n"; | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 new | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | This intiates the object. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | The key in the hash ref commands is a array | ||||||||||||||||||||||||||||
| 56 | that holds regular expressions to match against | ||||||||||||||||||||||||||||
| 57 | the command line or fname(in case of blank command | ||||||||||||||||||||||||||||
| 58 | line). At least one value must be defined. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | If the new method fails, it dies. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my %args=( | ||||||||||||||||||||||||||||
| 63 | commands=>[ | ||||||||||||||||||||||||||||
| 64 | 'kernel', | ||||||||||||||||||||||||||||
| 65 | ], | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | my $checker=Net::Connection::Match::Command->new( \%args ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 5 | my %args; | |||||||||||||||||||||||||||
| 74 | 5 | ▶ | if ( defined( $_[1] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 4 | %args = %{ $_[1] }; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # run some basic checks to make sure we have the minimum stuff required to work | ||||||||||||||||||||||||||||
| 79 | 5 | ▶ | if ( !defined( $args{commands} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | die('No commands key specified in the argument hash'); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 4 | ▶ | if ( ref( $args{commands} ) ne 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | die('The commands key is not a array'); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 3 | ▶ | if ( !defined $args{commands}[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | die('Nothing defined in the commands array'); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | my $self = { commands => $args{commands}, }; | |||||||||||||||||||||||||||
| 90 | 2 | bless $self; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | return $self; | |||||||||||||||||||||||||||
| 93 | } ## end sub new | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 match | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Checks if a single Net::Connection object matches the stack. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | One argument is taken and that is a Net::Connection object. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | The returned value is a boolean. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | if ( $checker->match( $conn ) ){ | ||||||||||||||||||||||||||||
| 104 | print "The connection matches.\n"; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 10 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 10 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 111 | 10 | my $object = $_[1]; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 10 | ▶ | if ( !defined($object) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 1 | return 0; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 9 | ▶ | if ( ref($object) ne 'Net::Connection' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 1 | return 0; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 8 | my $conn_pid = $object->pid; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # don't bother proceeding, the object won't match ever | ||||||||||||||||||||||||||||
| 124 | # as it does not have a PID | ||||||||||||||||||||||||||||
| 125 | 8 | ▶ | if ( !defined($conn_pid) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 1 | return 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 7 | my $loop = 0; | |||||||||||||||||||||||||||
| 130 | 7 | my $command; | |||||||||||||||||||||||||||
| 131 | 7 | ▶ | if ( !defined( $object->proc ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | # go through each proc and look for a matching pid | ||||||||||||||||||||||||||||
| 133 | 3 | my $proctable = Proc::ProcessTable->new; | |||||||||||||||||||||||||||
| 134 | 3 | my $procs = $proctable->table; | |||||||||||||||||||||||||||
| 135 | 3 | my $proc_int = 0; | |||||||||||||||||||||||||||
| 136 | 3 | my $loop = 1; | |||||||||||||||||||||||||||
| 137 | 3 | ▶ | while ( $loop | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$loop and defined $procs->[$proc_int]
MC/DC100%2/2
$loop and defined $procs->[$proc_int]
$loop
defined $procs->[$proc_int]
Truth table100%3/3
A and B
A= $loopB= defined $procs->[$proc_int]
| |||||||||||||||||||||||||||||
| 138 | && defined( $procs->[$proc_int] ) ) | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 5 | ▶ | if ( $conn_pid eq $procs->[$proc_int]->{pid} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 2 | $command = $procs->[$proc_int]->cmndline; | |||||||||||||||||||||||||||
| 143 | # '' means it is a kernel process | ||||||||||||||||||||||||||||
| 144 | 2 | ▶ | if ( $command =~ /^$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 1 | $command = '[' . $procs->[$proc_int]->fname . ']'; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | # exit the loop as we found it | ||||||||||||||||||||||||||||
| 148 | 2 | $loop = 0; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 5 | $proc_int++; | |||||||||||||||||||||||||||
| 152 | } ## end while ( $loop && defined( $procs->[$proc_int]...)) | ||||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||
| 154 | 4 | $command = $object->proc; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # likely a dead connection that is handing around... | ||||||||||||||||||||||||||||
| 158 | # or disappeared since grabbing the connection list | ||||||||||||||||||||||||||||
| 159 | # and starting processing | ||||||||||||||||||||||||||||
| 160 | 7 | ▶ | if ( !defined($command) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 1 | return 0; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # check each command regex and see if any of them match | ||||||||||||||||||||||||||||
| 165 | 6 | foreach my $regex ( @{ $self->{commands} } ) { | |||||||||||||||||||||||||||
| 166 | 9 | ▶ | if ( $command =~ /$regex/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 4 | return 1; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2 | return 0; | |||||||||||||||||||||||||||
| 172 | } ## end sub match | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 BUGS | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 181 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 182 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | perldoc Net::Connection::Match | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | You can also look for information at: | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =over 4 | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | L<http://annocpan.org/dist/Net-Connection-Match> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | L<https://cpanratings.perl.org/d/Net-Connection-Match> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | L<https://metacpan.org/release/Net-Connection-Match> | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =back | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Copyright 2019 Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 225 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 226 | copy of the full license at: | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Any use, modification, and distribution of the Standard or Modified | ||||||||||||||||||||||||||||
| 231 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||||||||||||||||||||
| 232 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||||||||||||||||||||
| 233 | or distribute the Package, if you do not accept this license. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | If your Modified Version has been derived from a Modified Version made | ||||||||||||||||||||||||||||
| 236 | by someone other than you, you are nevertheless required to ensure that | ||||||||||||||||||||||||||||
| 237 | your Modified Version complies with the requirements of this license. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | This license does not grant you the right to use any trademark, service | ||||||||||||||||||||||||||||
| 240 | mark, tradename, or logo of the Copyright Holder. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||||||||||||||||||||
| 243 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||||||||||||||||||||
| 244 | otherwise transfer the Package with respect to any patent claims | ||||||||||||||||||||||||||||
| 245 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||||||||||||||||||||
| 246 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||||||||||||||||||||
| 247 | counterclaim) against any party alleging that the Package constitutes | ||||||||||||||||||||||||||||
| 248 | direct or contributory patent infringement, then this Artistic License | ||||||||||||||||||||||||||||
| 249 | to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||||||||||||||||||||
| 252 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||||||||||||||||||||
| 253 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||
| 254 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||||||||||||||||||||
| 255 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||||||||||||||||||||
| 256 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||||||||||||||||||||
| 257 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||||||||||||||||||||
| 258 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 2 | 1; # End of Net::Connection::Match | |||||||||||||||||||||||||||