Python - Parsing Junos Router Config to display data in a row

789 views Asked by At

I have the below Junos router config section. I am trying to parse the data to display the results below. I have tried a number of things but the problem that I am having is that i cannot seem to get a unique line for each interface (lo0.2 and xe-1/0/2.1210). I would be very grateful for help on understanding the best way to accomplish the desired output. What I am trying to do, is parse these router configs to display key data in rows in excel, this will include interface configuration items as well.

I have tried to cycle through the config and look for lines matching, but I can never get both interfaces. Just need some ideas on how to accomplish this.

for line in config:
    if line.startswith('set routing-instances'):
        if 'options router-id' in line:
            self.router_id = line.split()[5]
        elif 'interface' in line:
            self.interface = line.split()[5]
        elif 'vrf-import' in line:
            self.vrf_import_policy = line.split()[4]
        elif 'vrf-export' in line:
            self.vrf_export_policy = line.split()[4]

    set routing-instances redwood-ca routing-options router-id 10.10.10.1
    set routing-instances redwood-ca routing-options autonomous-system 620
    set routing-instances redwood-ca routing-options autonomous-system independent-domain no-attrset
    set routing-instances redwood-ca protocols bgp path-selection always-compare-med
    set routing-instances redwood-ca protocols bgp path-selection external-router-id
    set routing-instances redwood-ca protocols bgp family inet unicast
    set routing-instances redwood-ca protocols bgp hold-time 30
    set routing-instances redwood-ca protocols bgp log-updown
    set routing-instances redwood-ca instance-type vrf
    set routing-instances redwood-ca interface lo0.2
    set routing-instances redwood-ca interface xe-1/0/2.1210
    set routing-instances redwood-ca route-distinguisher 10.10.10.1:100
    set routing-instances redwood-ca vrf-import redwood-ca-vrf-import
    set routing-instances redwood-ca vrf-export redwood-ca-vrf-export
    set routing-instances redwood-ca vrf-table-label
    
 
    Desired Output
    vrf         router-id       interface           vrf-import              vrf-export
    redwood-ca  10.10.10.1      lo0.2               redwood-ca-vrf-import   redwood-ca-vrf-export
    redwood-ca  10.10.10.1      xe-1/0/2.1210       redwood-ca-vrf-import   redwood-ca-vrf-export

I appreciate any assistance you may be able to offer. Is there a standard/best practice way to convert vertical type data to horizontal rows?

Thanks all for your help in advance.

Mike

1

There are 1 answers

0
Jeyashree On

Can you make the interface as list and append the matches each time when it is found.

**interface = list()**
for line in config:
    if line.startswith('set routing-instances'):
        if 'options router-id' in line:
            router_id = line.split()[5]
        elif 'interface' in line:
            **interface.append(line.split()[4])**
        elif 'vrf-import' in line:
            vrf_import_policy = line.split()[4]
        elif 'vrf-export' in line:
            vrf_export_policy = line.split()[4]

print interface

Output - ['lo0.2', 'xe-1/0/2.1210']