File Coverage

blib/lib/Win32/Tracert/Parser.pm
Criterion Covered Total %
statement 56 77 72.7
branch 16 26 61.5
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 78 110 70.9


line stmt bran cond sub pod time code
1             package Win32::Tracert::Parser;
2             $Win32::Tracert::Parser::VERSION = '0.012'; # TRIAL
3 4     4   16 use strict;
  4         4  
  4         121  
4 4     4   13 use warnings;
  4         4  
  4         105  
5              
6 4     4   12 use Object::Tiny qw (input);
  4         5  
  4         16  
7 4     4   455 use Net::hostent;
  4         6  
  4         17  
8 4     4   202 use Socket;
  4         6  
  4         5166  
9              
10             # ABSTRACT: Parser object used by method B in Win32::Tracert
11              
12             sub to_parse{
13 4     4 0 3 my $self=shift;
14 4         69 my $tracert_outpout=$self->input;
15 4 50       31 die "Attending ARRAY REF and got something else ! \n" unless ref($tracert_outpout) eq "ARRAY";
16            
17 4         6 my $tracert_result={};
18 4         4 my $host_targeted;
19             my $ip_targeted;
20 0         0 my $witness_line;
21              
22 4         11 LINE:
23 4         5 foreach my $curline (@{$tracert_outpout}){
24             #remove empty line
25 135 100       241 next LINE if $curline =~ /^$/;
26 123 50       307 next LINE if "$curline" !~ /(\w|\d)+/;
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 100       418 if ($curline =~ /^\S+.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    50          
32 4         30 ($host_targeted,$ip_targeted)=(split(/\s/, $curline))[-2..-1];
33 4         31 $ip_targeted =~ s/(\[|\])//g;
34 4         10 chomp $ip_targeted;
35             #Data Structure initalization with first results
36 4         23 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOSTNAME' => "$host_targeted", 'HOPS' => []};
37 4         8 next LINE;
38             }
39             elsif($curline =~ /^\S+.*\s(?:\d{1,3}\.){3}\d{1,3}\s/){
40 0         0 $ip_targeted = $curline;
41 0         0 $ip_targeted =~ s/.*?((?:\d{1,3}\.){3}\d{1,3}).*$/$1/;
42 0         0 chomp $ip_targeted;
43             #Data Structure initalization with first results
44 0         0 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOPS' => []};
45 0         0 next LINE;
46             }
47            
48 119         86 my $hop_data;
49             #Working on HOPS to reach Target
50 119 100       356 if ($curline =~ /^\s+\d+(?:\s+(?:\<1|\d+)\sms){3}\s+.*$/) {
    100          
    50          
51 106         68 my $hop_ip;
52 106         88 my $hop_host="N/A";
53             #We split Hop result to create and feed our data structure
54 106         347 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 100       331 if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    50          
57 90         384 $hop_identity =~ s/(\[|\])//g;
58 90         174 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
59             }
60             elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
61 16         16 $hop_ip=$hop_identity;
62 16         38 $hop_ip =~ s/\s//g;
63             }
64             else{
65 0         0 die "Bad format $hop_identity\n";
66             }
67             #Cleaning IP data to be sure not to have carriage return
68 106         101 chomp $hop_ip;
69            
70             #We store our data across hashtable reference
71 106         270 $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         77 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
  106         179  
80 106         146 next LINE;
81             }
82             elsif ($curline =~ /^\s+\d+\s+(?:\*\s+){3}.*$/){
83 5         6 my $hop_ip='N/A';
84 5         7 my $hop_host='N/A';
85             #We split Hop result to create and feed our data structure
86 5         16 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         25 $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         10 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
  5         13  
97 5         8 next LINE;
98             }
99             elsif ($curline =~ /^\s+\d+\s+\*\s+(?:\s+(?:\<1|\d+)\sms){2}.*$/){
100 0         0 my $hop_ip="NA";
101 0         0 my $hop_host="NA";
102             #We split Hop result to create and feed our data structure
103 0         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 0       0 if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    0          
107 0         0 $hop_identity =~ s/(\[|\])//g;
108 0         0 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
109             }
110             elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
111 0         0 $hop_ip=$hop_identity;
112 0         0 $hop_ip =~ s/\s//g;
113             }
114             else{
115 0         0 die "Bad format $hop_identity\n";
116             }
117             #Cleaning IP data to be sure not to have carriage return
118 0         0 chomp $hop_ip;
119             #We store our data across hashtable reference
120 0         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         0 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
  0         0  
129 0         0 next LINE;
130             }
131             #Debugging purpose
132 8         14 $witness_line=$curline;
133             }
134 4 50       5 die "Try to trap Win32 specific case: $ip_targeted: $witness_line" if scalar(@{$tracert_result->{"$ip_targeted"}->{'HOPS'}}) < 1;
  4         17  
135 4         20 return $tracert_result;
136             }
137              
138             1;
139              
140             __END__