File Coverage

blib/lib/Egg/Plugin/Net/Scan.pm
Criterion Covered Total %
statement 18 50 36.0
branch 0 16 0.0
condition 0 20 0.0
subroutine 6 9 66.6
pod 1 1 100.0
total 25 96 26.0


line stmt bran cond sub pod time code
1             package Egg::Plugin::Net::Scan;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Scan.pm 363 2008-08-20 00:07:33Z lushe $
6             #
7 2     2   861 use strict;
  2         3  
  2         77  
8 2     2   11 use warnings;
  2         3  
  2         70  
9 2     2   10 use Carp qw/ croak /;
  2         4  
  2         139  
10 2     2   2259 use Socket;
  2         9363  
  2         2063  
11              
12             our $VERSION = '3.02';
13              
14             sub port_scan {
15 0     0 1   my $e= shift;
16 0   0       my $host= shift || croak q{ I want 'Host name' or 'IP address'. };
17 0   0       my $port= shift || croak q{ I want 'Port number'. };
18 0 0 0       my $attr= $_[1] ? {@_}: ($_[0] || {});
19              
20 0 0         if ($host!~/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
21 0   0       my $name= gethostbyname($host)
22             || return Egg::Plugin::Net::Scan::Result->new(q{ The Host doesn't have IP address. });
23 0           $host= join '.', unpack("C4", $name);
24             }
25 0   0       $attr->{timeout} ||= 1;
26 0   0       $attr->{protcol} ||= 'tcp';
27 0           my($protname, $alias ,$protnum)= getprotobyname($attr->{protcol});
28 0   0       my $connect= inet_aton($host)
29             || return Egg::Plugin::Net::Scan::Result->new(qq{ Cannot connect $host\:$port. });
30 0           eval {
31 0           my $main_alrm= alarm(0);
32             local($SIG{ALRM})= sub{
33 0     0     alarm($main_alrm);
34 0           die qq/No response $host\:$port./;
35 0           };
36 0           alarm($attr->{timeout});
37 0 0         if ($protname eq 'udp') {
38 0 0         socket(SOCK, PF_INET, SOCK_DGRAM, $protnum)
39             || die q/Socket creation fault/;
40             } else {
41 0 0         socket(SOCK, PF_INET, SOCK_STREAM, $protnum)
42             || die q/Socket creation fault/;
43             }
44 0 0         connect(SOCK, sockaddr_in($port, $connect))
45             || die qq/Cannot connect $host\:$port./;
46 0           select(SOCK);
47 0           local $|= 1;
48 0           select(STDOUT);
49 0           close(SOCK);
50 0           alarm($main_alrm);
51             };
52 0   0       my $err= $@ || 'is success';
53 0           Egg::Plugin::Net::Scan::Result->new($err);
54             }
55              
56             package Egg::Plugin::Net::Scan::Result;
57 2     2   15 use strict;
  2         3  
  2         62  
58 2     2   10 use base qw/ Class::Accessor::Fast /;
  2         4  
  2         2080  
59              
60             __PACKAGE__->mk_accessors(qw/ is_success no_response is_error /);
61              
62             sub new {
63 0     0     my $class = shift;
64 0   0       my $errstr= shift || 0;
65 0 0         my $param =
    0          
66             $errstr=~/^is success/ ? { is_success=> 1 }
67             : $errstr=~/^No response/ ? { is_block => 1 }
68             : { is_error=> $errstr };
69 0           bless $param, $class;
70             }
71              
72             1;
73              
74             __END__
75              
76             =head1 NAME
77              
78             Egg::Plugin::Net::Scan - Network host's port is checked.
79              
80             =head1 SYNOPSIS
81              
82             use Egg qw/ Net::Scan /;
83              
84             # If the port is effective, by the 25th mail is sent.
85             my $scan= $e->port_scan('192.168.1.1', 25, timeout => 3 );
86             if ( $scan->is_success ) {
87             $e->mail->send;
88             print " Mail was transmitted.";
89             } elsif ( $scan->is_block ) {
90             print " Mail server has stopped. ";
91             } else {
92             print " Error occurs: ". $scan->is_error;
93             }
94              
95             =head1 DESCRIPTION
96              
97             It is a plugin to check the operational condition of arbitrary host's port.
98              
99             * Because 'alarm' command is used, it operates in the platform that doesn't
100             correspond. A fatal error occurs when it is made to do.
101              
102             =head1 METHODS
103              
104             =head2 port_scan ( [TARGET_HOST], [TARGET_PORT], [OPTION] )
105              
106             The port scan is done and the result is returned
107             with the 'Egg::Plugin::Net::Scan::Result' object.
108              
109             When TARGET_HOST and TARGET_PORT are omitted, the exception is generated.
110              
111             The following options can be passed to OPTION.
112              
113             =over 4
114              
115             =item * timeout
116              
117             Time to wait for answer from port.
118              
119             * It is judged that it is blocked when there is no answer in this time.
120              
121             Default is '1'.
122              
123             =item * protcol
124              
125             Communication protocol.
126              
127             Default is 'tcp'.
128              
129             =back
130              
131             =head1 RESULT METHODS
132              
133             It is a method supported by Egg::Plugin::Net::Scan::Result.
134              
135             my $result= $e->port_cacan( ....... );
136              
137             =head2 new
138              
139             Constructor.
140              
141             =head2 is_success
142              
143             When the answer from the port is admitted, true is restored.
144              
145             =head2 is_block
146              
147             When the answer from the port doesn't come back within the second of
148             timeout, true is returned.
149              
150             =head2 is_error
151              
152             When some errors occur, the error message is returned.
153              
154             =head1 SEE ALSO
155              
156             L<Socket>,
157             L<Egg::Release>,
158              
159             =head1 AUTHOR
160              
161             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
166              
167             This library is free software; you can redistribute it and/or modify
168             it under the same terms as Perl itself, either Perl version 5.8.6 or,
169             at your option, any later version of Perl 5 you may have available.
170              
171             =cut
172