in reply to Regex to match a Cisco ACL
use Modern::Perl; use Regexp::Common; my $cisco_protocol = qr { (?:ip|tcp|udp) | (?:object-group\s+[\S]+) }x; my $cisco_network = qr{ (?:host\s+[\S]+) | (?:$RE{net}{IPv4}\s+$RE{net}{IPv4}) | (?:object-group\s+[\S]+) | any }x; my $cisco_ports = qr{ (?:eq\s+\d+) | (?:range\s+\d+\s+\d+) }x; my $cisco_regex = qr{^ access-list \s+ (?<name>[\S]+) # name \s+ extended \s+ (?<action>(?:permit|deny)) # action \s+ (?<proto>$cisco_protocol) # protocol \s+ (?<source>$cisco_network) # source_network \s+ (?<destination>$cisco_network) # destination_network (?:\s+(?<ports>$cisco_ports))? # ports }x; while ( my $rule = <DATA> ) { chomp $rule; say "Parsing >$rule<"; if ( $rule =~ m/$cisco_regex/ ) { say "Name: $+{name}"; say "Action: $+{action}"; say "Protocol: $+{proto}"; say "Source: $+{source}"; say "Destination: $+{destination}"; say "Ports: $+{ports}" if defined $+{ports}; print "\n"; } else { say "No match\n"; } } __DATA__ access-list V420_IN extended permit object-group Symantec_Service_Grou +p object-group Symantec_Clients Symantec_Servers access-list V420_IN extended permit object-group Symantec_Service_Grou +p 10.148.0.0 255.254.0.0 host 10.149.16.40 access-list V420_IN extended permit object-group Symantec_Service_Grou +p any any access-list V420_IN extended permit tcp any any range 137 139 access-list V420_IN extended permit tcp any any eq 445
Output:
Parsing >access-list V420_IN extended permit object-group Symantec_Ser +vice_Group object-group Symantec_Clients Symantec_Servers< No match Parsing >access-list V420_IN extended permit object-group Symantec_Ser +vice_Group 10.148.0.0 255.254.0.0 host 10.149.16.40< Name: V420_IN Action: permit Protocol: object-group Symantec_Service_Group Source: 10.148.0.0 255.254.0.0 Destination: host 10.149.16.40 Parsing >access-list V420_IN extended permit object-group Symantec_Ser +vice_Group any any< Name: V420_IN Action: permit Protocol: object-group Symantec_Service_Group Source: any Destination: any Parsing >access-list V420_IN extended permit tcp any any range 137 139 +< Name: V420_IN Action: permit Protocol: tcp Source: any Destination: any Ports: range 137 139 Parsing >access-list V420_IN extended permit tcp any any eq 445< Name: V420_IN Action: permit Protocol: tcp Source: any Destination: any Ports: eq 445
As you see the first one does not match as I think the rule is not well formed. It seems to miss an object-group token.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex to match a Cisco ACL
by Anonymous Monk on May 22, 2011 at 17:21 UTC |