File Coverage

blib/lib/App/pscan/Command/Discover.pm
Criterion Covered Total %
statement 19 39 48.7
branch 1 4 25.0
condition 1 3 33.3
subroutine 7 11 63.6
pod 0 4 0.0
total 28 61 45.9


line stmt bran cond sub pod time code
1             package App::pscan::Command::Discover;
2 1     1   1369 use warnings;
  1         3  
  1         28  
3 1     1   6 use strict;
  1         1  
  1         33  
4 1     1   5 use base qw( App::pscan::Scanner App::pscan::Command);
  1         2  
  1         140  
5              
6             BEGIN {
7 1 50 33 1   35 print "POE::Component::Client::Ping requires root privilege\n"
8             if $> and ( $^O ne 'VMS' );
9             }
10 1     1   6 use POE;
  1         1  
  1         7  
11 1     1   1531 use POE::Component::Client::Ping;
  1         4827  
  1         68  
12 1     1   12 use App::pscan::Utils;
  1         2  
  1         372  
13              
14             =head1 NAME
15              
16             App::pscan::Command::Discover - test the range of ips with Ping
17              
18             =head1 DESCRIPTION
19              
20             tcp scan of a given range of the format of Net::IP and a port range.
21             e.g.: 192.168.1.0/24
22             192.168.1.1
23             www.google.it
24              
25              
26             =cut
27              
28             sub options {
29 0     0 0   ( "verbose" => "verbose" );
30             }
31              
32             sub scan() {
33 0     0 0   my $self = shift;
34 0           info 'Ping for ' . $self->{'IP'}->ip();
35              
36 0           POE::Component::Client::Ping->spawn(
37             Alias => 'pinger', # The component's name will be "pinger".
38             Timeout => 15, # The default ping timeout.
39             );
40              
41             # Create a session that will use the pinger. Its parameters match
42             # event names with the functions that will handle them.
43 0           POE::Session->create(
44             inline_states => {
45             _start =>
46             \&client_start, # Call client_start() to handle "_start".
47             pong =>
48             \&client_got_pong, # Call client_got_pong() to handle "pong".
49             },
50             args => [ $self->{'IP'} ]
51             );
52              
53             # Start POE's main loop. It will only return when everything is done.
54 0           $poe_kernel->run();
55 0           exit;
56              
57             }
58              
59             sub client_start {
60 0     0 0   my ( $kernel, $session, $ip ) = @_[ KERNEL, SESSION, ARG0 ];
61 0           info "Starting to ping hosts";
62 0           do {
63             # info "Pinging ".$ip->ip()." at ", scalar(localtime);
64              
65             # "Pinger, do a ping and return the results as a pong event. The
66             # address to ping is $ping."
67 0           $kernel->post( pinger => ping => pong => $ip->ip );
68             } while ( ++$ip );
69             }
70              
71             # Handle a "pong" event (returned by the Ping component because we
72             # asked it to). Just display some information about the ping.
73             sub client_got_pong {
74 0     0 0   my ( $kernel, $session ) = @_[ KERNEL, SESSION ];
75              
76             # The original request is returned as the first parameter. It
77             # contains the address we wanted to ping, the total time to wait for
78             # a response, and the time the request was made.
79 0           my $request_packet = $_[ARG0];
80 0           my ( $request_address, $request_timeout, $request_time )
81 0           = @{$request_packet};
82              
83             # The response information is returned as the second parameter. It
84             # contains the response address (which may be different from the
85             # request address), the ping's round-trip time, and the time the
86             # reply was received.
87 0           my $response_packet = $_[ARG1];
88 0           my ( $response_address, $roundtrip_time, $reply_time )
89 0           = @{$response_packet};
90              
91             # It is impossible to know ahead of time how many ICMP ping
92             # responses will arrive for a particular address, so the component
93             # always waits PING_TIMEOUT seconds. An undefined response address
94             # signals that this waiting period has ended.
95 0 0         if ( defined $response_address ) {
96 0           info sprintf( "Pinged %-15.15s - Response from %-15.15s in %6.3fs",
97             $request_address, $response_address, $roundtrip_time );
98             }
99             else {
100             # notice "Time's up for responses from $request_address.";
101             }
102             }
103              
104             1;