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