Search code examples
perllinescounting

How to count first and the last IP adderess from the following text that i have


I just want to print the first and the last IP from the following text file that I have

Traceroute: 10 1291134800 1291134792 1291134792 GoogleDNS 0.media.collegehumor.com 92.123.72.112
traceroute to 92.123.72.112 (92.123.72.112), 30 hops max, 60 byte packets
 1  * * *
 2  134.2.205.30  0.765 ms  1.125 ms  1.428 ms
 3  134.2.250.254  0.612 ms  0.701 ms  0.695 ms
 4  129.143.135.33  0.515 ms  0.516 ms  0.510 ms
 5  129.143.1.149  1.014 ms  1.012 ms  1.010 ms
 6  129.143.1.166  1.036 ms  0.821 ms  0.808 ms
 7  129.143.1.130  274.286 ms  274.265 ms *
 8  80.81.192.28  7.246 ms  6.913 ms  6.896 ms
 9  92.123.72.0  7.319 ms  7.311 ms  7.297 ms
Traceroute: 12 1291134800 1291134792 1291134792 LocalDNS 0.media.collegehumor.com      212.201.100.184
traceroute to 212.201.100.184 (212.201.100.184), 30 hops max, 60 byte packets
1  * * *
2  134.2.205.30  0.774 ms  1.121 ms  1.426 ms
3  134.2.250.254  0.635 ms  0.716 ms  0.709 ms
4  129.143.135.33  0.584 ms  0.583 ms  0.579 ms
5  129.143.1.149  0.951 ms  0.948 ms  0.942 ms
6  188.1.233.229  1.042 ms  0.969 ms  0.962 ms
7  188.1.145.77  4.665 ms  4.804 ms  4.826 ms
8  188.1.146.50  4.815 ms  4.890 ms  4.841 ms
9  188.1.145.73  7.071 ms  7.202 ms  7.241 ms
10  188.1.145.69  9.976 ms  10.001 ms  9.441 ms
11  212.201.100.184  9.289 ms  9.259 ms  9.276 ms

For example, if I have the above file then it should print the ones in which the first and the last does not match. So from the above i would be expecting something like: Destination to 92.123.72.112 but reached 92.123.72.0 And for the second traceroute it should not print anything as the destination 212.201.100.184 actually reached 212.201.100.184. So far I have written the following code..

  #!/usr/bin/perl -w
    use Regexp::Common qw/net/; 
 open my $in, '<', "Sample_01.txt" or die $!;
 open FILE, ">", "filename.txt" or die $!;
 while (my $line = <$in>){

         if($line =~ /^Traceroute: .* (\S+)/) {
               $traceroute = $1;
        print FILE "Destination to |$traceroute|";
        my ($ip) = $line =~ /(?: \d+ \s \s+) ($RE{net}{IPv4}) /msx+;
                if($traceroute eq $ip){

        print FILE "$ip|";
        print FILE "\n";
        }
        }
        else {

        }

Solution

  • Build a hash of arrays, where the keys to the hash are the source ips, and the array is the ip of each hop. Then when you're done, just compare the key to the last value in the hash.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    open my $in, '<', 'Sample_01.txt' or die $!;
    open FILE, '>', 'filename.txt' or die $!;
    
    my %ipToRoute;
    my $srcIp;
    while (my $line = <$in>) {
        next if ($line =~ /^Traceroute/);
    
        if (my ($ip) = $line =~ m/traceroute to ([^ ]+)/) {
            $srcIp = $ip;
        }
        else {
            $line =~ s/^ +//;
            my $dstIp = (split / +/, $line)[1];
            push @{$ipToRoute{$srcIp}}, $dstIp if $dstIp ne '*';
        }
    }
    
    foreach my $srcIp (keys %ipToRoute) {
        my $finalDst = @{$ipToRoute{$srcIp}}[-1];
        if ($srcIp ne $finalDst) {
            print FILE "Destination to $srcIp but reached $finalDst\n";
        }
    }