| 1 | # | ||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||
| 3 | # | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Rex::Commands::SimpleCheck - Simple tcp/alive checks | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||
| 10 | |||||||||||||
| 11 | With this module you can do simple tcp/alive checks. | ||||||||||||
| 12 | |||||||||||||
| 13 | Version <= 1.0: All these functions will not be reported. | ||||||||||||
| 14 | |||||||||||||
| 15 | All these functions are not idempotent. | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||
| 18 | |||||||||||||
| 19 | if(is_port_open($remote_host, $port)) { | ||||||||||||
| 20 | print "Port $port is open\n"; | ||||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 EXPORTED FUNCTIONS | ||||||||||||
| 24 | |||||||||||||
| 25 | =cut | ||||||||||||
| 26 | |||||||||||||
| 27 | package Rex::Commands::SimpleCheck; | ||||||||||||
| 28 | |||||||||||||
| 29 | 1 | ▶ | use v5.14.4; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | |||||||||||||
| 32 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||
| 33 | |||||||||||||
| 34 | 1 | ▶ | use IO::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | |||||||||||||
| 36 | 1 | require Rex::Exporter; | |||||||||||
| 37 | 1 | ▶ | use base qw(Rex::Exporter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | 1 | ▶ | use vars qw(@EXPORT); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | |||||||||||||
| 40 | 1 | @EXPORT = qw(is_port_open); | |||||||||||
| 41 | |||||||||||||
| 42 | =head2 is_port_open($ip, $port) | ||||||||||||
| 43 | |||||||||||||
| 44 | Check if something is listening on port $port of $ip. | ||||||||||||
| 45 | |||||||||||||
| 46 | =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub is_port_open { | ||||||||||
|
Subroutine
is_port_open: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | my ( $ip, $port, $type ) = @_; | |||||||||||
| 51 | |||||||||||||
| 52 | 0 | ▶ | $type ||= "tcp"; | ||||||||||
|
Condition0%0/2
$type ||= "tcp"
MC/DC0%0/1
$type ||= "tcp"
$type
Truth table0%0/2
A
A= $type
| |||||||||||||
| 53 | |||||||||||||
| 54 | 0 | my $socket = IO::Socket::INET->new( | |||||||||||
| 55 | PeerAddr => $ip, | ||||||||||||
| 56 | PeerPort => $port, | ||||||||||||
| 57 | Proto => $type, | ||||||||||||
| 58 | Timeout => 2, | ||||||||||||
| 59 | Type => SOCK_STREAM | ||||||||||||
| 60 | ); | ||||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | if ($socket) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | close $socket; | |||||||||||
| 64 | 0 | return 1; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 0 | return 0; | |||||||||||
| 68 | |||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 1 | 1; | |||||||||||