File Coverage

lib/Rex/Commands/SimpleCheck.pm
Criterion Covered Total %
statement 14 21 66.6
branch 0 2 0.0
condition 0 2 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 20 32 62.5


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
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     1   14 use v5.12.5;
  1         4  
30 1     1   6 use warnings;
  1         3  
  1         40  
31              
32             our $VERSION = '1.14.3'; # VERSION
33              
34 1     1   9 use IO::Socket;
  1         1119  
  1         3  
35              
36             require Rex::Exporter;
37 1     1   456 use base qw(Rex::Exporter);
  1         2  
  1         73  
38 1     1   6 use vars qw(@EXPORT);
  1         3  
  1         170  
39              
40             @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             sub is_port_open {
49              
50 0     0 1   my ( $ip, $port, $type ) = @_;
51              
52 0   0       $type ||= "tcp";
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 0         if ($socket) {
63 0           close $socket;
64 0           return 1;
65             }
66              
67 0           return 0;
68              
69             }
70              
71             1;