Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Matching a cisco interface config

by FloydATC (Deacon)
on Jan 12, 2010 at 09:54 UTC ( #816854=perlquestion: print w/replies, xml ) Need Help??
FloydATC has asked for the wisdom of the Perl Monks concerning the following question:

I have a classic cisco config containing entries like these:
interface GigabitEthernet6/10 description TRUNK-KBV-SW001 switchport trunk encapsulation dot1q switchport trunk allowed vlan 1,30,31,45,156-158 switchport mode trunk switchport nonegotiate ! interface GigabitEthernet6/11 description Offices switchport access vlan 31 spanning-tree portfast ! interface GigabitEthernet6/12 !
I simply want to extract each interface config (from 'interface' to '!', inclusive) as a separate string, using a regular expression. I could rewrite this to extract each line manually but it just shouldn't be necessary.
#!/usr/bin/perl my @lines = <>; my $config = join('', @lines); my @interfaces = ( $config =~ m/interface.+?\n!\n/gi ); foreach my $interface (@interfaces) { print $interface . "\n"; }
This matches only the empty config, because . does not match \n

How can I make my regex match whatever lines, if any, appear between the 'interface' and '!' lines? I'm ashamed to say I've been beating my head against this for several hours and nothing seems to work...

-- Time flies when you don't know what you're doing

Replies are listed 'Best First'.
Re: Matching a cisco interface config
by BrowserUk (Pope) on Jan 12, 2010 at 09:59 UTC

    Try

    #! perl -slw use strict; $/ = "!\n"; ## set the delimiter while( <DATA> ) { print "'$_'"; } __DATA__ interface GigabitEthernet6/10 description TRUNK-KBV-SW001 switchport trunk encapsulation dot1q switchport trunk allowed vlan 1,30,31,45,156-158 switchport mode trunk switchport nonegotiate ! interface GigabitEthernet6/11 description Offices switchport access vlan 31 spanning-tree portfast ! interface GigabitEthernet6/12 !

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      This solution works, but in practice it would require some prepping of the config file, since I failed to mention it doesn't contain just the interfaces but a lot of other config settings as well. (As most Cisco configs do)

      I'll see how the regex approach below works in practice on a really big collection of configs and if I get into trouble with performance then this is probably the way to go. Thanks!

      -- Time flies when you don't know what you're doing

        Something like this?

        #! perl -slw use strict; $/ = "!\n"; while( <DATA> ) { next unless m[^interface]; print "'$_'"; } __DATA__ interface GigabitEthernet6/10 description TRUNK-KBV-SW001 switchport trunk encapsulation dot1q switchport trunk allowed vlan 1,30,31,45,156-158 switchport mode trunk switchport nonegotiate ! stuff with more stuff ! interface GigabitEthernet6/11 description Offices switchport access vlan 31 spanning-tree portfast ! thing with other things ! interface GigabitEthernet6/12 !
        Outputs:
        C:\test>junk 'interface GigabitEthernet6/10 description TRUNK-KBV-SW001 switchport trunk encapsulation dot1q switchport trunk allowed vlan 1,30,31,45,156-158 switchport mode trunk switchport nonegotiate ! ' 'interface GigabitEthernet6/11 description Offices switchport access vlan 31 spanning-tree portfast ! ' 'interface GigabitEthernet6/12 ! '

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Matching a cisco interface config
by rubasov (Friar) on Jan 12, 2010 at 11:55 UTC
    If you want to keep the regex based approach try the code below, and see 'perldoc perlre' for the meaning of /m and /s modifiers. (However I think BrowserUK's solution above is more lightweigth than this.)
    #! /usr/bin/perl use strict; use warnings; my $config = do { local $/; <DATA> }; my @interfaces = ( $config =~ m/^interface.+?\n!\n/msgi ); foreach my $interface (@interfaces) { print $interface, "--\n"; } __DATA__ interface GigabitEthernet6/10 description TRUNK-KBV-SW001 switchport trunk encapsulation dot1q switchport trunk allowed vlan 1,30,31,45,156-158 switchport mode trunk switchport nonegotiate ! interface GigabitEthernet6/11 description Offices switchport access vlan 31 spanning-tree portfast ! interface GigabitEthernet6/12 !
      I knew I was missing something, and the /s modifier was it, thank you!

      -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816854]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-11-22 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (326 votes). Check out past polls.

    Notices?