File Coverage

blib/lib/Win32/Tracert.pm
Criterion Covered Total %
statement 71 95 74.7
branch 19 32 59.3
condition 11 12 91.6
subroutine 14 17 82.3
pod 6 7 85.7
total 121 163 74.2


line stmt bran cond sub pod time code
1             package Win32::Tracert;
2             $Win32::Tracert::VERSION = '0.010';
3 3     3   2098 use strict;
  3         7  
  3         133  
4 3     3   15 use warnings;
  3         5  
  3         98  
5              
6 3     3   1922 use Net::hostent;
  3         13324  
  3         21  
7 3     3   2525 use Socket qw ();
  3         11492  
  3         119  
8 3     3   1262 use Object::Tiny qw (destination circuit);
  3         483  
  3         16  
9 3     3   1671 use Win32::Tracert::Parser;
  3         6  
  3         29  
10              
11             # ABSTRACT: Call Win32 tracert tool or parse Win32 tracert output
12              
13             my %tracert_result;
14              
15             #redefine constuctor
16             sub new {
17 8     8 0 28230 my $class = shift;
18 8         43 my $self = $class->SUPER::new( @_ );
19             # Extra checking and such
20 8 100 66     249 die "You must define [destination] attribute or [circuit] attribute" if ((! defined $self->destination) && (! defined $self->circuit));
21 7 100 100     253 die "constructor can't accept [circuit] and [destination] together" if ((defined $self->circuit) && (defined $self->destination));
22 6 100 100     198 die "Attribute [circuit] have to contain a Tracert result" if ((! defined $self->destination) && (scalar(@{$self->circuit}) == 0));
  4         80  
23 5 100 100     108 die "Attribute [destination] have to contain a hostname or IP address" if ((! defined $self->circuit) && ($self->destination eq ""));
24            
25 4         47 return $self;
26             }
27              
28              
29              
30             sub path {
31 7     7 1 8 my ($self,$value)=@_;
32 7 100       14 if (@_ == 2) {
33 3         8 $self->{path}=$value;
34             }
35            
36 7         13 return $self->{path};
37             }
38              
39             sub _destination_hostname{
40 1     1   2 my ($self,$value)=@_;
41 1 50       4 if (@_ == 2) {
42 1         2 $self->{_destination_hostname}=$value;
43             }
44            
45 1         2 return $self->{_destination_hostname};
46             }
47              
48             sub destination_hostname{
49 0     0 1 0 my $self=shift;
50 0         0 return $self->_destination_hostname;
51             }
52              
53             sub _destination_ip{
54 1     1   8 my ($self,$value)=@_;
55 1 50       5 if (@_ == 2) {
56 1         3 $self->{_destination_ip}=$value;
57             }
58            
59 1         3 return $self->{_destination_ip};
60             }
61              
62             sub destination_ip{
63 0     0 1 0 my $self=shift;
64 0         0 return $self->_destination_ip;
65             }
66              
67             sub _to_find{
68 0     0   0 my $self=shift;
69 0         0 my $hosttocheck=$self->destination;
70 0         0 my $iptocheck;
71            
72 0 0       0 if ($hosttocheck =~ /(?:\d{1,3}\.){3}\d{1,3}/) {
73 0         0 $iptocheck=$self->destination;
74             }
75             else{
76 0         0 my $h;
77             my $ipadress;
78            
79 0 0       0 if ($h = gethost($hosttocheck)){
80 0         0 $ipadress = inet_ntoa($h->addr);
81 0         0 $iptocheck=$ipadress;
82             }
83             else{
84 0         0 die "No such host: $hosttocheck\n";
85             }
86             }
87              
88             #Executing tracert call before to send its result to Parser
89 0         0 my @tracert_output=`tracert $hosttocheck`;
90              
91 0         0 return \@tracert_output;
92             }
93              
94             sub _get_target_ip{
95 3     3   4 my $self=shift;
96 3         5 my $hosttocheck=shift;
97 3         4 my $iptocheck;
98            
99 3 50       21 if ($hosttocheck =~ /(?:\d{1,3}\.){3}\d{1,3}/) {
100 3         5 $iptocheck=$hosttocheck;
101             }
102             else{
103 0         0 my $h;
104             my $ipadress;
105 0 0       0 if ($h = gethost($hosttocheck)){
106 0         0 $ipadress = inet_ntoa($h->addr);
107 0         0 $iptocheck=$ipadress;
108             }
109             else{
110 0         0 die "No such host: $hosttocheck\n";
111             }
112             }
113            
114 3         6 return $iptocheck;
115             }
116              
117             sub to_trace{
118 3     3 1 494 my $self=shift;
119            
120 3 50       76 my $result = defined $self->destination ? $self->_to_find : $self->circuit ;
121 3         80 my $path=Win32::Tracert::Parser->new(input => $result);
122            
123             #put returned result from [to_parse] method in current [path] attribute
124 3         22 $self->path($path->to_parse);
125            
126 3         25 return $self;
127             }
128              
129             sub found{
130 2     2 1 7 my $self=shift;
131 2         4 my $tracert_result=$self->path();
132            
133 2         2 my $hosttocheck;
134             my $iptocheck;
135            
136 2 50       54 if (defined $self->destination) {
137 0         0 $hosttocheck=$self->destination;
138             }
139             else{
140 2         17 ($hosttocheck)=keys %{$tracert_result};
  2         8  
141             }
142            
143 2         8 $iptocheck=$self->_get_target_ip($hosttocheck);
144            
145 2 50       8 if (exists $tracert_result->{"$iptocheck"}) {
146 2 100       14 if ("$iptocheck" eq $tracert_result->{"$iptocheck"}->{'HOPS'}->[-1]->{'IPADRESS'}) {
147             #If we find path to destination we can initialise following private writable attributes
148 1         7 $self->_destination_ip($tracert_result->{"$iptocheck"}->{'HOPS'}->[-1]->{'IPADRESS'});
149 1         7 $self->_destination_hostname($tracert_result->{"$iptocheck"}->{'HOPS'}->[-1]->{'HOSTNAME'});
150            
151 1         8 return $self;
152             }
153             else{
154             #route to target undetermined
155 1         5 return undef;
156             }
157             }
158             else{
159 0         0 die "No traceroute result for $hosttocheck\n";
160             }
161             }
162              
163             sub hops{
164 1     1 1 2 my $self=shift;
165 1         5 my $tracert_result=$self->path();
166 1         1 my $hosttocheck;
167             my $iptocheck;
168            
169 1 50       25 if (defined $self->destination) {
170 0         0 $hosttocheck=$self->destination;
171             }
172             else{
173 1         5 ($hosttocheck)=keys %{$tracert_result};
  1         3  
174             }
175 1         3 $iptocheck=$self->_get_target_ip($hosttocheck);
176              
177 1         2 return scalar(@{$tracert_result->{"$iptocheck"}->{'HOPS'}});
  1         7  
178             }
179              
180              
181             1;
182              
183             __END__