Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Cisco to Juniper - parser help

by Fletch (Bishop)
on May 24, 2021 at 11:38 UTC ( [id://11132955]=note: print w/replies, xml ) Need Help??


in reply to Cisco to Juniper - parser help

Couple of high level thoughts (attempted with minimal caffeination, so take with large conglomeration of NaCL and I may be whiffing on template syntax a bit):

This is almost complex enough you might want to look at using a proper parser (Parse::RecDescent, Marpa::R2) and build some sort of data structure to represent each "interface entry". Alternately maybe at least use "!\n" as your record separator and push the existing parsing logic off into its own sub (which then returns some sort of data structure . . .); doing that might help make sure you're not intermingling blocks and can (slightly more easily) start with a fresh parse state each one.

Also rather than a bunch of hardcoded print statements for something like this I'd use Template Toolkit (see also Template) and write something which takes the data produced by the parsing bits and then passes those to something which expands into the final output lines.

[% MACRO interface_metainfo( if_info ) BEGIN %] set interfaces ge-1/1/1 unit 23 description "[% if_info.description %] +" set interfaces ge-1/1/1 unit 23 encapsulation [% if_info.encapsulation + %] set interfaces ge-1/1/1 unit 23 vlan-tags [% FOR tag IN if_info.vlan_t +ags.keys %][% tag %] [% if_info.vlan_tags.$tag %][% END %] ## YADDA YADDA YADDA [% END %] [%- CLEAR -%] [% FOR if IN interface_list %] [% interface_metainfo( if ) %] [% protocol_block( if ) %] [% class_of_service_block( if ) %] [% END %]

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Cisco to Juniper - parser help
by jamescmatt (Novice) on May 24, 2021 at 11:43 UTC
    Thanks, I will definitely be looking into doing something like that once I can get the filter to align correctly to the unit.

      Aah another thing just noticed (after caffeine has put the mind in motion a bit) you've got an outer while loop reading your input FH handle as well as a nested foreach reading the same handle. You look to be effectively reading the first line and discarding it (after printing it to STDOUT), then reading the rest of the file. That's . . . probably not what you want and may be why you're out of sync.

      As I mentioned, if you change things to do record reads (see perlvar for $/) and then have a single while reading input records once that may help keep things straight(er).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Thanks again for pointing that out. I fixed it, and when I debug printing $cos to the CLI, it prints out perfectly, but then below when it should print to the file, it is empty(it prints everything to the file perfectly, except the line with $cos(both the debug line, and the actual print to file are empty here)). So trying to figure out why that is happening since logically I can't explain to myself what would be clearing my variable(I am probably missing something simple, but after working on this for over a week, I can barely think straight).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11132955]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found