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 LanX (Saint)
on May 24, 2021 at 16:30 UTC ( [id://11132975]=note: print w/replies, xml ) Need Help??


in reply to Cisco to Juniper - parser help

This should give you a clean start.

I suppose you'll be able to adjust the methods to your needs.

Especially ->__out__ where you can write the new format based on the already parsed data.

Based on the limited insight I have, I tried to keep it as generic as possible.

YMMV but HTH! :)

# https://perlmonks.org/index.pl?node_id=11132953 use strict; use warnings; use Data::Dump qw/pp dd/; my $obj = "CiscoService"; while (<DATA>) { chomp; my ($indent,$data) = /^(\s*)(.*)$/; my ($call, @args) = split/\s+/, $data; $call =~ s/^!$/__out__/; $call =~ s/-/_/g; if ( my $meth = $obj->can($call) ) { $obj = $meth->( $obj, length($indent),@args) } else { warn "<$call> not impemented"; } } package CiscoService; use Data::Dump qw/pp dd/; sub service { my ($self,$indent,@args) = @_; return bless {__head__ => \@args, sub=>{} },__PACKAGE__; } sub description { my ($self,$indent,@args) = @_; $self->{sub}{description} = \@args; return $self; } sub service_policy { my ($self,$indent,@args) = @_; $self->{sub}{"service-policy"} = \@args; return $self; } sub encapsulation { my ($self,$indent,@args) = @_; $self->{sub}{encapsulation} = \@args; return $self; } sub rewrite { my ($self,$indent,@args) = @_; $self->{sub}{rewrite} = \@args; return $self; } sub xconnect { my ($self,$indent,@args) = @_; $self->{sub}{xconnect} = {head =>\@args, sub=>{}}; return $self; } sub mtu { my ($self,$indent,@args) = @_; $self->{sub}{xconnect}{sub}{mtu} = \@args; return $self; } sub __out__ { my ($self,$indent,@args) = @_; pp $self; return "CiscoService"; } package main; __DATA__ service instance 20 ethernet description Internal site encapsulation dot1q 17 rewrite ingress tag pop 1 symmetric xconnect 192.168.51.20 838969735 encapsulation mpls mtu 9202 ! service instance 21 ethernet description Client 1 important encapsulation dot1q 18 rewrite ingress tag pop 1 symmetric xconnect 192.168.51.21 838969736 encapsulation mpls mtu 9202 ! service instance 22 ethernet description Client other 2 encapsulation dot1q 22 rewrite ingress tag pop 1 symmetric xconnect 192.168.56.22 838925386 encapsulation mpls mtu 9202 ! service instance 23 ethernet description Client name with fun stuff encapsulation dot1q 3200 second-dot1q 5 rewrite ingress tag pop 2 symmetric service-policy input CoS_222_333_23 xconnect 192.168.56.23 571125465 encapsulation mpls mtu 9202 ! service instance 24 ethernet description Client - not as important encapsulation dot1q 3200 second-dot1q 6 rewrite ingress tag pop 2 symmetric service-policy input 222_444_24 xconnect 192.168.56.24 571637077 encapsulation mpls mtu 9202 !

bless({ __head__ => ["instance", 20, "ethernet"], sub => { description => ["Internal", "site"], encapsulation => ["dot1q", 17], rewrite => ["ingress", "tag", "pop", 1, "symmetric"], xconnect => { head => ["192.168.51.20", 838969735, "encapsula +tion", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 21, "ethernet"], sub => { description => ["Client", 1, "important"], encapsulation => ["dot1q", 18], rewrite => ["ingress", "tag", "pop", 1, "symmetric"], xconnect => { head => ["192.168.51.21", 838969736, "encapsula +tion", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 22, "ethernet"], sub => { description => ["Client", "other", 2], encapsulation => ["dot1q", 22], rewrite => ["ingress", "tag", "pop", 1, "symmetric"], xconnect => { head => ["192.168.56.22", 838925386, "encapsula +tion", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 23, "ethernet"], sub => { "description" => ["Client", "name", "with", "fun", "stuff"], "encapsulation" => ["dot1q", 3200, "second-dot1q", 5], "rewrite" => ["ingress", "tag", "pop", 2, "symmetric"], "service-policy" => ["input", "CoS_222_333_23"], "xconnect" => { head => ["192.168.56.23", 571125465, "encaps +ulation", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService") bless({ __head__ => ["instance", 24, "ethernet"], sub => { "description" => ["Client", "-", "not", "as", "important"], "encapsulation" => ["dot1q", 3200, "second-dot1q", 6], "rewrite" => ["ingress", "tag", "pop", 2, "symmetric"], "service-policy" => ["input", "222_444_24"], "xconnect" => { head => ["192.168.56.24", 571637077, "encaps +ulation", "mpls"], sub => { mtu => [9202] }, }, }, }, "CiscoService")

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-29 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found