New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Junos-style configs searchable #17

Closed
mpenning opened this Issue Jan 26, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@mpenning
Owner

mpenning commented Jan 26, 2015

Some people want ciscoconfparse to search Junos configurations... this bug tracks implementation of the feature request.

@mpenning mpenning self-assigned this Jan 26, 2015

@mpenning

This comment has been minimized.

Show comment
Hide comment
@mpenning

mpenning Jan 26, 2015

Owner

We now parse Junos configurations into Cisco IOS-style (I already hear the distant cries of "heretic")... nevertheless, if you want to see it in action, here's how I parse configs/sample_01.junos:

>>> from ciscoconfparse import CiscoConfParse
>>> parse = CiscoConfParse('configs/sample_01.junos', syntax='junos', comment='#!')
>>> for line in parse.ioscfg:
...     print line
... 
!# Last commit: 2015-06-28 13:00:59 CST by mpenning
system 
    host-name TEST01_EX
    domain-name pennington.net
    domain-search [ pennington.net lab.pennington.net ]
    location 
        country-code 001
        building HQ_005
        floor 1
    root-authentication 
        encrypted-password "$1$y7ArHxKU$zUbdeLfBirgkCsKiOJ5Qa0"
    name-server 
        172.16.3.222
    login 
        announcement "Test Lab Switch"
            class super-user
            authentication 
                encrypted-password "$1$y7ArHxKU$zUbdeLfBirgkCsKiOJ5Qa0"
    services 
        ssh 
            root-login allow
        telnet
        web-management 
            http
    syslog 
        user * 
            any emergency
        file messages 
            any notice
            authorization info
        file interactive-commands 
            interactive-commands any
    ntp 
        boot-server time.apple.com
        server 172.16.8.3
vlans 
    Management 
        vlan-id 1
        interface 
            ge-0/0/0.0
            ge-0/0/1.0
            ge-0/0/2.0
            ge-0/0/3.0
    VLAN_FOO 
        vlan-id 5
    vlan1 
        vlan-id 1
        l3-interface vlan.1
    vlan800 
        vlan-id 800
ethernet-switching-options 
    storm-control 
        interface all
interfaces 
    ge-0/0/0 
        unit 0 
            family ethernet-switching 
                port-mode access
                vlan 
                    members VLAN_FOO
    ge-0/0/1 
        unit 0 
            family ethernet-switching 
                port-mode trunk
                vlan 
                    members all
                native-vlan-id 1
    vlan 
        unit 0 
            family inet 
                address 172.16.15.5/22
routing-options 
    static 
        route 0.0.0.0/0 next-hop 172.16.12.1
        route 192.168.36.0/25 next-hop 172.16.12.1

This is a hack, to be sure... so I'm leaving the enhancement open until I get a few more details nailed down... in the mean time, if people want to experiment with searching Junos as IOS, it is here :-)

Owner

mpenning commented Jan 26, 2015

We now parse Junos configurations into Cisco IOS-style (I already hear the distant cries of "heretic")... nevertheless, if you want to see it in action, here's how I parse configs/sample_01.junos:

>>> from ciscoconfparse import CiscoConfParse
>>> parse = CiscoConfParse('configs/sample_01.junos', syntax='junos', comment='#!')
>>> for line in parse.ioscfg:
...     print line
... 
!# Last commit: 2015-06-28 13:00:59 CST by mpenning
system 
    host-name TEST01_EX
    domain-name pennington.net
    domain-search [ pennington.net lab.pennington.net ]
    location 
        country-code 001
        building HQ_005
        floor 1
    root-authentication 
        encrypted-password "$1$y7ArHxKU$zUbdeLfBirgkCsKiOJ5Qa0"
    name-server 
        172.16.3.222
    login 
        announcement "Test Lab Switch"
            class super-user
            authentication 
                encrypted-password "$1$y7ArHxKU$zUbdeLfBirgkCsKiOJ5Qa0"
    services 
        ssh 
            root-login allow
        telnet
        web-management 
            http
    syslog 
        user * 
            any emergency
        file messages 
            any notice
            authorization info
        file interactive-commands 
            interactive-commands any
    ntp 
        boot-server time.apple.com
        server 172.16.8.3
vlans 
    Management 
        vlan-id 1
        interface 
            ge-0/0/0.0
            ge-0/0/1.0
            ge-0/0/2.0
            ge-0/0/3.0
    VLAN_FOO 
        vlan-id 5
    vlan1 
        vlan-id 1
        l3-interface vlan.1
    vlan800 
        vlan-id 800
ethernet-switching-options 
    storm-control 
        interface all
interfaces 
    ge-0/0/0 
        unit 0 
            family ethernet-switching 
                port-mode access
                vlan 
                    members VLAN_FOO
    ge-0/0/1 
        unit 0 
            family ethernet-switching 
                port-mode trunk
                vlan 
                    members all
                native-vlan-id 1
    vlan 
        unit 0 
            family inet 
                address 172.16.15.5/22
routing-options 
    static 
        route 0.0.0.0/0 next-hop 172.16.12.1
        route 192.168.36.0/25 next-hop 172.16.12.1

This is a hack, to be sure... so I'm leaving the enhancement open until I get a few more details nailed down... in the mean time, if people want to experiment with searching Junos as IOS, it is here :-)

mpenning added a commit that referenced this issue Jan 26, 2015

@mpenning

This comment has been minimized.

Show comment
Hide comment
@mpenning

mpenning Jan 26, 2015

Owner

Note: Juniper configurations are quite hierarchical... finding an IOS interface object in python just requires parse.find_objects('^interface\sGigabitEthernet0/0/1').

To get the interface object for a Junos interface such as ge-0/0/1, you'd need to use something like parse.find_objects_w_parents(r'^\s*interface', r'^\s+ge-0/0/1'), which returns the object for ge-0/0/1 in a list. Then you can iterate through for children of ge-0/0/1 as you like.

However, if you don't require the interface object itself, you can still enforce Junos interface configuration policies in python via parse.find_objects_w_child(r'^\s+ge-0/0/1', r'port-mode\saccess'); that policy checks whether ge-0/0/1 is port-mode access.

Owner

mpenning commented Jan 26, 2015

Note: Juniper configurations are quite hierarchical... finding an IOS interface object in python just requires parse.find_objects('^interface\sGigabitEthernet0/0/1').

To get the interface object for a Junos interface such as ge-0/0/1, you'd need to use something like parse.find_objects_w_parents(r'^\s*interface', r'^\s+ge-0/0/1'), which returns the object for ge-0/0/1 in a list. Then you can iterate through for children of ge-0/0/1 as you like.

However, if you don't require the interface object itself, you can still enforce Junos interface configuration policies in python via parse.find_objects_w_child(r'^\s+ge-0/0/1', r'port-mode\saccess'); that policy checks whether ge-0/0/1 is port-mode access.

@mpenning mpenning closed this Jun 29, 2015

@kthned

This comment has been minimized.

Show comment
Hide comment
@kthned

kthned Aug 1, 2017

Hi Mike

I am trying to parse the F5 Networks configurations. Do you have a syntax ready for bigIP or any timeplan :)
currently i am getting following error on at

line 263, in __init__
    config = self.convert_braces_to_ios(rgx.split(text))
line 389, in convert_braces_to_ios
    line, line_offset = line_level(tmp.strip())
line 383, in line_level
    raise ValueError("Could not parse: '{0}'".format(input))
ValueError: Could not parse: '} else {'

Running the code like

from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse("/Users/python/lbj.junos", syntax='junos', comment='#')
for line in parse.ioscfg:
    print (line)

kthned commented Aug 1, 2017

Hi Mike

I am trying to parse the F5 Networks configurations. Do you have a syntax ready for bigIP or any timeplan :)
currently i am getting following error on at

line 263, in __init__
    config = self.convert_braces_to_ios(rgx.split(text))
line 389, in convert_braces_to_ios
    line, line_offset = line_level(tmp.strip())
line 383, in line_level
    raise ValueError("Could not parse: '{0}'".format(input))
ValueError: Could not parse: '} else {'

Running the code like

from ciscoconfparse import CiscoConfParse
parse = CiscoConfParse("/Users/python/lbj.junos", syntax='junos', comment='#')
for line in parse.ioscfg:
    print (line)
@mpenning

This comment has been minimized.

Show comment
Hide comment
@mpenning

mpenning Jan 26, 2018

Owner

@kthned, "} else {" should parse correctly in the latest versions... please let me know if you run into problems

Owner

mpenning commented Jan 26, 2018

@kthned, "} else {" should parse correctly in the latest versions... please let me know if you run into problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment