| 1 | package Win32::Tracert::Parser; | ||||||||||||||||||
| 2 | 4 | $Win32::Tracert::Parser::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 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=14 SCAR=31.2
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
| |||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 4 | my $tracert_result={}; | |||||||||||||||||
| 18 | 4 | my $host_targeted; | |||||||||||||||||
| 19 | my $ip_targeted; | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | LINE: | ||||||||||||||||||
| 22 | 4 | foreach my $curline (@{$tracert_outpout}){ | |||||||||||||||||
| 23 | #remove empty line | ||||||||||||||||||
| 24 | 135 | ▶ | next LINE if $curline =~ /^$/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 25 | 123 | ▶ | next LINE if "$curline" !~ /(\w|\d)+/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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 | ▶ | if ($curline =~ /^\S+.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 31 | 4 | ($host_targeted,$ip_targeted)=(split(/\s/, $curline))[-2..-1]; | |||||||||||||||||
| 32 | 4 | $ip_targeted =~ s/(\[|\])//g; | |||||||||||||||||
| 33 | 4 | chomp $ip_targeted; | |||||||||||||||||
| 34 | #Data Structure initalization with first results | ||||||||||||||||||
| 35 | 4 | $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOSTNAME' => "$host_targeted", 'HOPS' => []}; | |||||||||||||||||
| 36 | 4 | next LINE; | |||||||||||||||||
| 37 | } | ||||||||||||||||||
| 38 | elsif($curline =~ /^\S+.*\s(?:\d{1,3}\.){3}\d{1,3}\s/){ | ||||||||||||||||||
| 39 | 0 | $ip_targeted = $curline; | |||||||||||||||||
| 40 | 0 | $ip_targeted =~ s/.*?((?:\d{1,3}\.){3}\d{1,3}).*$/$1/; | |||||||||||||||||
| 41 | 0 | chomp $ip_targeted; | |||||||||||||||||
| 42 | #Data Structure initalization with first results | ||||||||||||||||||
| 43 | 0 | $tracert_result->{"$ip_targeted"}={'IPADRESS' => "$ip_targeted", 'HOPS' => []}; | |||||||||||||||||
| 44 | 0 | next LINE; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 119 | my $hop_data; | |||||||||||||||||
| 48 | #Working on HOPS to reach Target | ||||||||||||||||||
| 49 | 119 | ▶ | if ($curline =~ /^\s+\d+(?:\s+(?:\<1|\d+)\sms){3}\s+.*$/) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 50 | 106 | my $hop_ip; | |||||||||||||||||
| 51 | 106 | my $hop_host="N/A"; | |||||||||||||||||
| 52 | #We split Hop result to create and feed our data structure | ||||||||||||||||||
| 53 | 106 | 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 | ▶ | if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 56 | 90 | $hop_identity =~ s/(\[|\])//g; | |||||||||||||||||
| 57 | 90 | ($hop_host,$hop_ip)=split(/\s+/, $hop_identity); | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){ | ||||||||||||||||||
| 60 | 16 | $hop_ip=$hop_identity; | |||||||||||||||||
| 61 | 16 | $hop_ip =~ s/\s//g; | |||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | else{ | ||||||||||||||||||
| 64 | 0 | die "Bad format $hop_identity\n"; | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | #Cleaning IP data to be sure not to have carriage return | ||||||||||||||||||
| 67 | 106 | chomp $hop_ip; | |||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | #We store our data across hashtable reference | ||||||||||||||||||
| 70 | 106 | $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 | push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data; | |||||||||||||||||
| 79 | 106 | next LINE; | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | elsif ($curline =~ /^\s+\d+\s+(?:\*\s+){3}.*$/){ | ||||||||||||||||||
| 82 | 5 | my $hop_ip='N/A'; | |||||||||||||||||
| 83 | 5 | my $hop_host='N/A'; | |||||||||||||||||
| 84 | #We split Hop result to create and feed our data structure | ||||||||||||||||||
| 85 | 5 | 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 | $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 | push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data; | |||||||||||||||||
| 96 | 5 | next LINE; | |||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | elsif ($curline =~ /^\s+\d+\s+\*\s+(?:\s+(?:\<1|\d+)\sms){2}.*$/){ | ||||||||||||||||||
| 99 | 0 | my $hop_ip="NA"; | |||||||||||||||||
| 100 | 0 | my $hop_host="NA"; | |||||||||||||||||
| 101 | #We split Hop result to create and feed our data structure | ||||||||||||||||||
| 102 | 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 | ▶ | if ($hop_identity =~ /.*\[(?:\d{1,3}\.){3}\d{1,3}\]/) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 106 | 0 | $hop_identity =~ s/(\[|\])//g; | |||||||||||||||||
| 107 | 0 | ($hop_host,$hop_ip)=split(/\s+/, $hop_identity); | |||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | elsif($hop_identity =~ /(?:\d{1,3}\.){3}\d{1,3}/){ | ||||||||||||||||||
| 110 | 0 | $hop_ip=$hop_identity; | |||||||||||||||||
| 111 | 0 | $hop_ip =~ s/\s//g; | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | else{ | ||||||||||||||||||
| 114 | 0 | die "Bad format $hop_identity\n"; | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | #Cleaning IP data to be sure not to have carriage return | ||||||||||||||||||
| 117 | 0 | chomp $hop_ip; | |||||||||||||||||
| 118 | #We store our data across hashtable reference | ||||||||||||||||||
| 119 | 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 | push @{$tracert_result->{"$ip_targeted"}->{'HOPS'}}, $hop_data; | |||||||||||||||||
| 128 | 0 | next LINE; | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | } | ||||||||||||||||||
| 131 | 4 | return $tracert_result; | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 4 | 1; | |||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||