Chris Knipe wrote:
Lo everyone,

Can someone please give me a regex to parse the following...



Flags: X - disabled, I - invalid, D - dynamic, J - rejected,
C - connect, S - static, r - rip, o - ospf, b - bgp
 #    DST-ADDRESS        G GATEWAY         DISTANCE INTERFACE
 0  S 0.0.0.0/0          r 198.19.0.2      1        SERVER-CORE
 1  S 196.0.0.0/8        r 165.146.192.1   1        National Gateway
 2 DC 198.19.1.0/24      r 0.0.0.0         0        WIRELESS-CORE
 3 Ib 198.19.0.0/24      u 0.0.0.0         200      (unknown)
 4 DC 198.19.0.0/24      r 0.0.0.0         0        SERVER-CORE
 5 DC 192.168.1.0/24     r 0.0.0.0         0        INTERNAL-CORE
 6 DC 165.146.192.1/32   r 0.0.0.0         0        National Gateway
[EMAIL PROTECTED] >


I want the regex to return the rule number for all route entries that is static (S Flag) on Interface "National Gateway". For added security, I'd like it if the regex will also only return true if the gateway address is part of 165.165.0.0/8 or 165.146.0.0/8

Basically, I need to use the rule number in another command to delete the route entry... So I presume I'll need to extract it via the regex

If it's a fixed width data format, its better (easier for you, faster for perl) to break up the string with C<substr>:


#!/usr/bin/perl

use strict;
use warnings;


while (defined( my $line = <DATA> )) { chomp( $line );

    my %data;
    $data{rule}      = substr( $line,  0,  2 );
    $data{flags}     = substr( $line,  3,  2 );
    $data{dest_ip}   = substr( $line,  6, 18 );
    $data{g}         = substr( $line, 25,  1 );
    $data{gateway}   = substr( $line, 27, 16 );
    $data{distance}  = substr( $line, 43,  8 );
    $data{interface} = substr( $line, 52     );

    # cleanup, stripping spaces
    $data{$_} =~ s/^\s+|\s+$//g
        for keys %data;

    # reformat or reinterpret data
    $data{flags} = [ split( //, $data{flags} ) ];

    use Data::Dumper;
    print Dumper( \%data );
}



__END__
 0  S 0.0.0.0/0          r 198.19.0.2      1        SERVER-CORE
 1  S 196.0.0.0/8        r 165.146.192.1   1        National Gateway
 2 DC 198.19.1.0/24      r 0.0.0.0         0        WIRELESS-CORE
 3 Ib 198.19.0.0/24      u 0.0.0.0         200      (unknown)
 4 DC 198.19.0.0/24      r 0.0.0.0         0        SERVER-CORE
 5 DC 192.168.1.0/24     r 0.0.0.0         0        INTERNAL-CORE
 6 DC 165.146.192.1/32   r 0.0.0.0         0        National Gateway


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to