Tracert/Parser.pm

filter: Statementstmt 74.6% 59 / 79 Branchbran 61.5% 16 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 72.3% 81 / 112 CC 15 SCAR 29.0
CC
15
Cov
71%
to_parse
23.9
BEGIN
1.0
BEGIN
1.0
CRAP 18.2
↑ Summary Tracert.pm »
1 package Win32::Tracert::Parser;
2 4 $Win32::Tracert::Parser::VERSION = '0.012'; # TRIAL
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 Object::Tiny qw (input);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Net::hostent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Parser object used by method B<to_trace> in Win32::Tracert
11  
12 4 ▶sub to_parse{
Subroutine to_parse: called CC=15 SCAR=31.7
Pod undocumented
13 4 my $self=shift;
14 4 my $tracert_outpout=$self->input;
15 4 ▶ die "Attending ARRAY REF and got something else ! \n" unless ref($tracert_outpout) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tracert_outpout eq "ARRAY" 0 4 4
16
17 4 my $tracert_result={};
18 4 my $host_targeted;
19 my $ip_targeted;
20 4 my $witness_line;
21  
22 LINE:
23 4 foreach my $curline (@{$tracert_outpout}){
24 #remove empty line
25 135 ▶ next LINE if $curline =~ /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $curline =~ /^$/ 12 123 135
26 123 ▶ next LINE if "$curline" !~ /(\w|\d)+/;
Branch: 1 branch
BranchTrueFalseTotal
unless "$curline" =~ /(\w|\d)+/ 0 123 123
27
28 #We looking for the target (NB: It is only sure with IP V4 Adress )
29 #If we have DNS solving we record hostname and IP Adress
30 #Else we keep only IP Adress
31 123 ▶ if ($curline =~ /^\S+.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($curline =~ /^\S+.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) { } 4 119 123
elsif ($curline =~ /^\S+.*\s(?:\d{1,3}\.){3}\d{1,3}\s/) { } 0 119 119
32 4 ($host_targeted,$ip_targeted)=(split(/\s/, $curline))[-2..-1];
33 4 $ip_targeted =~ s/(\[|\])//g;
34 4 chomp $ip_targeted;
35 #Data Structure initalization with first results
36 4 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOSTNAME' => "$host_targeted", 'HOPS' => []};
37 4 next LINE;
38 }
39 elsif($curline =~ /^\S+.*\s(?:\d{1,3}\.){3}\d{1,3}\s/){
40 0 $ip_targeted = $curline;
41 0 $ip_targeted =~ s/.*?((?:\d{1,3}\.){3}\d{1,3}).*$/$1/;
42 0 chomp $ip_targeted;
43 #Data Structure initalization with first results
44 0 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOPS' => []};
45 0 next LINE;
46 }
47
48 119 my $hop_data;
49 #Working on HOPS to reach Target
50 119 ▶ if ($curline =~ /^\s+\d+(?:\s+(?:\<1|\d+)\sms){3}\s+.*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($curline =~ /^\s+\d+(?:\s+(?:\<1|\d+)\sms){3}\s+.*$/) { } 106 13 119
elsif ($curline =~ /^\s+\d+\s+(?:\*\s+){3}.*$/) { } 5 8 13
elsif ($curline =~ /^\s+\d+\s+\*\s+(?:\s+(?:\<1|\d+)\sms){2}.*$/) { } 0 8 8
51 106 my $hop_ip;
52 106 my $hop_host="N/A";
53 #We split Hop result to create and feed our data structure
54 106 my (undef, $hopnb, $p1_rt, $p1_ut, $p2_rt, $p2_ut, $p3_rt, $p3_ut, $hop_identity) = split(/\s+/,$curline,9);
55 #If we have hostname and IP Adress we keep all else we have only IP Adress to keep
56 106 ▶ if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) { } 90 16 106
elsif ($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/) { } 16 0 16
57 90 $hop_identity =~ s/(\[|\])//g;
58 90 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
59 }
60 elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
61 16 $hop_ip=$hop_identity;
62 16 $hop_ip =~ s/\s//g;
63 }
64 else{
65 0 die "Bad format $hop_identity\n";
66 }
67 #Cleaning IP data to be sure not to have carriage return
68 106 chomp $hop_ip;
69
70 #We store our data across hashtable reference
71 106 $hop_data={'HOPID' => $hopnb,
72 'HOSTNAME' => $hop_host,
73 'IPADRESS' => $hop_ip,
74 'PACKET1_RT' => $p1_rt,
75 'PACKET2_RT' => $p2_rt,
76 'PACKET3_RT' => $p3_rt,
77 };
78 #Each data record is store to table in ascending order
79 106 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
80 106 next LINE;
81 }
82 elsif ($curline =~ /^\s+\d+\s+(?:\*\s+){3}.*$/){
83 5 my $hop_ip='N/A';
84 5 my $hop_host='N/A';
85 #We split Hop result to create and feed our data structure
86 5 my (undef, $hopnb, $p1_rt, $p2_rt, $p3_rt, $hop_identity) = split(/\s+/,$curline,6);
87 #We store our data across hashtable reference
88 5 $hop_data={'HOPID' => $hopnb,
89 'HOSTNAME' => $hop_host,
90 'IPADRESS' => $hop_ip,
91 'PACKET1_RT' => $p1_rt,
92 'PACKET2_RT' => $p2_rt,
93 'PACKET3_RT' => $p3_rt,
94 };
95 #Each data record is store to table in ascending order
96 5 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
97 5 next LINE;
98 }
99 elsif ($curline =~ /^\s+\d+\s+\*\s+(?:\s+(?:\<1|\d+)\sms){2}.*$/){
100 0 my $hop_ip="NA";
101 0 my $hop_host="NA";
102 #We split Hop result to create and feed our data structure
103 0 my (undef, $hopnb, $p1_rt, $p2_rt, $p2_ut, $p3_rt, $p3_ut, $hop_identity) = split(/\s+/,$curline,6);
104
105 #If we have hostname and IP Adress we keep all else we have only IP Adress to keep
106 0 ▶ if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) { } 0 0 0
elsif ($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/) { } 0 0 0
107 0 $hop_identity =~ s/(\[|\])//g;
108 0 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
109 }
110 elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
111 0 $hop_ip=$hop_identity;
112 0 $hop_ip =~ s/\s//g;
113 }
114 else{
115 0 die "Bad format $hop_identity\n";
116 }
117 #Cleaning IP data to be sure not to have carriage return
118 0 chomp $hop_ip;
119 #We store our data across hashtable reference
120 0 $hop_data={'HOPID' => $hopnb,
121 'HOSTNAME' => $hop_host,
122 'IPADRESS' => $hop_ip,
123 'PACKET1_RT' => $p1_rt,
124 'PACKET2_RT' => $p2_rt,
125 'PACKET3_RT' => $p3_rt,
126 };
127 #Each data record is store to table in ascending order
128 0 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
129 0 next LINE;
130 }
131 #Debugging purpose
132 8 $witness_line=$curline;
133 }
134 4 ▶ die "Try to trap Win32 specific case: $ip_targeted: $witness_line" if scalar(@{$tracert_result->{"$ip_targeted"}->{'HOPS'}}) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$$tracert_result{"$ip_targeted"}{"HOPS"};} < 1 0 4 4
135 4 return $tracert_result;
136 }
137  
138 4 1;
139  
140 __END__
↑ Summary Tracert.pm »