#! perl use strict; use warnings; my $line1_re = qr! ^ Traceroute: \s+ .* \s+ (\S+) $ !x; my $line2_re = qr! ^ traceroute !x; my $destn_re = qr! ^ # start of line \s* # optional whitespace \d+ # sequence number: nnn \s+ # whitespace (?: # EITHER (?: \d{1,3} \. ){3} \d{1,3} # IP address: nnn.nnn.nnn.nnn | # OR \* \s+ \* \s+ \* # * * * ) !x; my (%ipToRoute, $source_IP, $in_tracing); while (my $line = ) { chomp $line; if ($line =~ /$line1_re/) { $source_IP = $1; $in_tracing = 1; } elsif ($in_tracing && $line !~ /$line2_re/) { if ($line =~ /$destn_re/) { $line =~ s/ ^ \s+ //x; my $destination_IP = (split /\s+/, $line)[1]; push @{ $ipToRoute{$source_IP} }, $destination_IP unless $destination_IP eq '*'; } else { $in_tracing = 0; } } } foreach (keys %ipToRoute) { my $final_destination = @{ $ipToRoute{$_} }[-1]; printf "Destination to %s but reached %s\n", $_, $final_destination unless $_ eq $final_destination; } __DATA__ Traceroute: 22 1291136399 1291136393 1291136393 LocalDNS home.china.com.cn 221.204.248.107 traceroute to 221.204.248.107 (221.204.248.107), 30 hops max, 60 byte packets 1 134.2.173.254 0.170 ms 0.191 ms 0.185 ms etc.