File Coverage

blib/lib/Win32/Tracert/Parser.pm
Criterion Covered Total %
statement 51 70 72.8
branch 15 24 62.5
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 72 101 71.2


line stmt bran cond sub pod time code
1             package Win32::Tracert::Parser;
2             $Win32::Tracert::Parser::VERSION = '0.010';
3 3     3   12 use strict;
  3         3  
  3         95  
4 3     3   12 use warnings;
  3         3  
  3         85  
5              
6 3     3   11 use Object::Tiny qw (input);
  3         3  
  3         12  
7 3     3   392 use Net::hostent;
  3         3  
  3         19  
8 3     3   207 use Socket;
  3         4  
  3         4664  
9              
10             # ABSTRACT: Parser object used by method B in Win32::Tracert
11              
12             sub to_parse{
13 3     3 0 5 my $self=shift;
14 3         51 my $tracert_outpout=$self->input;
15 3 50       26 die "Attending ARRAY REF and got something else ! \n" unless ref($tracert_outpout) eq "ARRAY";
16            
17 3         5 my $tracert_result={};
18 3         3 my $host_targeted;
19             my $ip_targeted;
20              
21 3         7 LINE:
22 3         5 foreach my $curline (@{$tracert_outpout}){
23             #remove empty line
24 101 100       235 next LINE if $curline =~ /^$/;
25 92 50       292 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 92 100       392 if ($curline =~ /^\S+.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    50          
31 3         27 ($host_targeted,$ip_targeted)=(split(/\s/, $curline))[-2..-1];
32 3         26 $ip_targeted =~ s/(\[|\])//g;
33 3         10 chomp $ip_targeted;
34             #Data Structure initalization with first results
35 3         21 $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOSTNAME' => "$host_targeted", 'HOPS' => []};
36 3         6 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 89         70 my $hop_data;
48             #Working on HOPS to reach Target
49 89 100       337 if ($curline =~ /^\s+\d+(?:\s+(?:\<1|\d+)\sms){3}\s+.*$/) {
    100          
    50          
50 79         61 my $hop_ip;
51 79         64 my $hop_host="N/A";
52             #We split Hop result to create and feed our data structure
53 79         373 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 79 100       322 if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) {
    50          
56 67         395 $hop_identity =~ s/(\[|\])//g;
57 67         168 ($hop_host,$hop_ip)=split(/\s+/, $hop_identity);
58             }
59             elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){
60 12         13 $hop_ip=$hop_identity;
61 12         42 $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 79         104 chomp $hop_ip;
68            
69             #We store our data across hashtable reference
70 79         278 $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 79         176 push $tracert_result->{"$ip_targeted"}->{'HOPS'}, $hop_data;
79 79         136 next LINE;
80             }
81             elsif ($curline =~ /^\s+\d+\s+(?:\*\s+){3}.*$/){
82 4         6 my $hop_ip='N/A';
83 4         6 my $hop_host='N/A';
84             #We split Hop result to create and feed our data structure
85 4         16 my (undef, $hopnb, $p1_rt, $p2_rt, $p3_rt, $hop_identity) = split(/\s+/,$curline,6);
86             #We store our data across hashtable reference
87 4         26 $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 4         12 push $tracert_result->{"$ip_targeted"}->{'HOPS'}, $hop_data;
96 4         9 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;
128 0         0 next LINE;
129             }
130             }
131 3         17 return $tracert_result;
132             }
133              
134             1;
135              
136             __END__