File Coverage

blib/lib/Win32/Tracert/Parser.pm
Criterion Covered Total %
statement 53 73 72.6
branch 15 24 62.5
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 74 104 71.1


line stmt bran cond sub pod time code
1             package Win32::Tracert::Parser;
2             $Win32::Tracert::Parser::VERSION = '0.011';
3 4     4   21 use strict;
  4         8  
  4         218  
4 4     4   26 use warnings;
  4         8  
  4         168  
5              
6 4     4   22 use Object::Tiny qw (input);
  4         5  
  4         25  
7 4     4   640 use Net::hostent;
  4         7  
  4         32  
8 4     4   331 use Socket;
  4         7  
  4         7471  
9              
10             # ABSTRACT: Parser object used by method B in Win32::Tracert
11              
12             sub to_parse{
13 4     4 0 7 my $self=shift;
14 4         97 my $tracert_outpout=$self->input;
15 4 50       46 die "Attending ARRAY REF and got something else ! \n" unless ref($tracert_outpout) eq "ARRAY";
16            
17 4         8 my $tracert_result={};
18 4         7 my $host_targeted;
19             my $ip_targeted;
20              
21 4         11 LINE:
22 4         5 foreach my $curline (@{$tracert_outpout}){
23             #remove empty line
24 135 100       309 next LINE if $curline =~ /^$/;
25 123 50       393 next LINE if "$curline" !~ /(\w|\d)+/;
26            
27             #We looking for the target (NB: It is only sure with IP V4 Adress )
28             #If we have DNS solving we record hostname and IP Adress
29             #Else we keep only IP Adress
30 123 100       512 if ($curline =~ /^\S+.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    50          
31 4         34 ($host_targeted,$ip_targeted)=(split(/\s/, $curline))[-2..-1];
32 4         37 $ip_targeted =~ s/(\[|\])//g;
33 4         10 chomp $ip_targeted;
34             #Data Structure initalization with first results
35 4         25 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOSTNAME' => "$host_targeted", 'HOPS' => []};
36 4         8 next LINE;
37             }
38             elsif($curline =~ /^\S+.*\s(?:\d{1,3}\.){3}\d{1,3}\s/){
39 0         0 $ip_targeted = $curline;
40 0         0 $ip_targeted =~ s/.*?((?:\d{1,3}\.){3}\d{1,3}).*$/$1/;
41 0         0 chomp $ip_targeted;
42             #Data Structure initalization with first results
43 0         0 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOPS' => []};
44 0         0 next LINE;
45             }
46            
47 119         170 my $hop_data;
48             #Working on HOPS to reach Target
49 119 100       446 if ($curline =~ /^\s+\d+(?:\s+(?:\<1|\d+)\sms){3}\s+.*$/) {
    100          
    50          
50 106         78 my $hop_ip;
51 106         101 my $hop_host="N/A";
52             #We split Hop result to create and feed our data structure
53 106         486 my (undef, $hopnb, $p1_rt, $p1_ut, $p2_rt, $p2_ut, $p3_rt, $p3_ut, $hop_identity) = split(/\s+/,$curline,9);
54             #If we have hostname and IP Adress we keep all else we have only IP Adress to keep
55 106 100       420 if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    50          
56 90         553 $hop_identity =~ s/(\[|\])//g;
57 90         216 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
58             }
59             elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
60 16         17 $hop_ip=$hop_identity;
61 16         44 $hop_ip =~ s/\s//g;
62             }
63             else{
64 0         0 die "Bad format $hop_identity\n";
65             }
66             #Cleaning IP data to be sure not to have carriage return
67 106         129 chomp $hop_ip;
68            
69             #We store our data across hashtable reference
70 106         345 $hop_data={'HOPID' => $hopnb,
71             'HOSTNAME' => $hop_host,
72             'IPADRESS' => $hop_ip,
73             'PACKET1_RT' => $p1_rt,
74             'PACKET2_RT' => $p2_rt,
75             'PACKET3_RT' => $p3_rt,
76             };
77             #Each data record is store to table in ascending order
78 106         90 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
  106         258  
79 106         186 next LINE;
80             }
81             elsif ($curline =~ /^\s+\d+\s+(?:\*\s+){3}.*$/){
82 5         8 my $hop_ip='N/A';
83 5         8 my $hop_host='N/A';
84             #We split Hop result to create and feed our data structure
85 5         20 my (undef, $hopnb, $p1_rt, $p2_rt, $p3_rt, $hop_identity) = split(/\s+/,$curline,6);
86             #We store our data across hashtable reference
87 5         28 $hop_data={'HOPID' => $hopnb,
88             'HOSTNAME' => $hop_host,
89             'IPADRESS' => $hop_ip,
90             'PACKET1_RT' => $p1_rt,
91             'PACKET2_RT' => $p2_rt,
92             'PACKET3_RT' => $p3_rt,
93             };
94             #Each data record is store to table in ascending order
95 5         7 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
  5         21  
96 5         8 next LINE;
97             }
98             elsif ($curline =~ /^\s+\d+\s+\*\s+(?:\s+(?:\<1|\d+)\sms){2}.*$/){
99 0         0 my $hop_ip="NA";
100 0         0 my $hop_host="NA";
101             #We split Hop result to create and feed our data structure
102 0         0 my (undef, $hopnb, $p1_rt, $p2_rt, $p2_ut, $p3_rt, $p3_ut, $hop_identity) = split(/\s+/,$curline,6);
103            
104             #If we have hostname and IP Adress we keep all else we have only IP Adress to keep
105 0 0       0 if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    0          
106 0         0 $hop_identity =~ s/(\[|\])//g;
107 0         0 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
108             }
109             elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
110 0         0 $hop_ip=$hop_identity;
111 0         0 $hop_ip =~ s/\s//g;
112             }
113             else{
114 0         0 die "Bad format $hop_identity\n";
115             }
116             #Cleaning IP data to be sure not to have carriage return
117 0         0 chomp $hop_ip;
118             #We store our data across hashtable reference
119 0         0 $hop_data={'HOPID' => $hopnb,
120             'HOSTNAME' => $hop_host,
121             'IPADRESS' => $hop_ip,
122             'PACKET1_RT' => $p1_rt,
123             'PACKET2_RT' => $p2_rt,
124             'PACKET3_RT' => $p3_rt,
125             };
126             #Each data record is store to table in ascending order
127 0         0 push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data;
  0         0  
128 0         0 next LINE;
129             }
130             }
131 4         27 return $tracert_result;
132             }
133              
134             1;
135              
136             __END__