Skip to content
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

F5 "else" statement (junos) #70

Closed
petzah opened this issue Dec 20, 2016 · 7 comments
Closed

F5 "else" statement (junos) #70

petzah opened this issue Dec 20, 2016 · 7 comments
Assignees

Comments

@petzah
Copy link

petzah commented Dec 20, 2016

if { something } {
do X
} else {
do Y
}

Traceback (most recent call last):
File "./test.py", line 4, in
parse = CiscoConfParse('file.conf', syntax='junos', debug=True, comment='#')
File "/usr/lib/pymodules/python2.7/ciscoconfparse/ciscoconfparse.py", line 225, in init
config = self.convert_braces_to_ios(rgx.split(text))
File "/usr/lib/pymodules/python2.7/ciscoconfparse/ciscoconfparse.py", line 351, in convert_braces_to_ios
line, line_offset = line_level(tmp.strip())
File "/usr/lib/pymodules/python2.7/ciscoconfparse/ciscoconfparse.py", line 345, in line_level
raise ValueError("Could not parse: '{0}'".format(input))
ValueError: Could not parse: '} else {'

@kthned
Copy link

kthned commented Aug 1, 2017

Hi
Did you get the solution for this ? I am having the similar issue while parsing F5.

@timothyfranson
Copy link

After fussing with it for the better part of today I came up with the below solution which finally allowed all my F5 parsing to run without issue. It's not the cleanest solution and I'm sure it can be improved but it works well enough to allow me to accomplish what I set out for. Python 3.5.2.

# Prep F5 config before instantiating the CiscoConfParse instance
import re

clean_conf = []
for line in bigip_conf:
    # the comment kwarg of CiscoConfParse does not work right
    if re.match(r'^(\s*#)', line):
        continue
    # kill empty '{ }' that break the parser
    new_line = line.replace(' { }', '')
    # remove one-line configs.  cause problems with parser
    # example of line removed:  'monitor min 1 of { /some/monitor /some/othermonitor }'
    if re.match(r'^(.+?{)(.+?}\s*)$', new_line):
        continue
    # separate '} else(if) {' lines so parser interprets them properly
    if re.match(r'\s*\}\s+else(if)?', new_line):
        new_line = re.sub(r'\}\s+elseif', '}\nelseif', new_line, count=1)
        new_line = re.sub(r'\}\s+else\s*{', '}\nelse {', new_line, count=1)
        new_line = new_line.split('\n', maxsplit=1)
        clean_conf.extend(new_line)
        continue
    clean_conf.append(new_line)

@mpenning
Copy link
Owner

Please try version 1.2.52 and see if this helps...

@timothyfranson
Copy link

timothyfranson commented Jan 26, 2018

Just gave it a shot, still runs into an issue. Lines like the below would be common in iRules.

File "/var/www/asdf/fetch.py", line 344, in fetch_f5_config
parse = CiscoConfParse(bigip_conf, syntax='junos', comment='#')
File "/var/www/asdf/venv/lib/python3.5/site-packages/ciscoconfparse/ciscoconfparse.py", line 188, in init
config = self.convert_braces_to_ios(config)
File "/var/www/asdf/venv/lib/python3.5/site-packages/ciscoconfparse/ciscoconfparse.py", line 402, in convert_braces_to_ios
line, line_offset = line_level(tmp.strip())
File "/var/www/asdf/venv/lib/python3.5/site-packages/ciscoconfparse/ciscoconfparse.py", line 396, in line_level
raise ValueError("Could not parse: '{0}'".format(input))
ValueError: Could not parse: '} elseif { $path starts_with "/abc" || $path starts_with "/abc/123" } {'

@mpenning
Copy link
Owner

I don't have access to an F5 anymore... can someone respond with a sanitized F5 config with some else and elseif conditions in it?

@timothyfranson
Copy link

Attached below. Very generically sanitized. Ctrl+f for 'if_elseif_rule' for a good example complete with nested if statements.

sanitized_bigip.conf.txt

@mpenning
Copy link
Owner

mpenning commented Feb 23, 2020

@timothyfranson , I finally rewrote the junos parser to support these kind of F5 configs... please test version 1.5.1 and tell me whether this works for you.

@mpenning mpenning self-assigned this Jul 14, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants