textfsm json template examples
Logan Rawlins
maveric at gotskill.net
Tue May 11 19:55:48 CEST 2021
Like many of you on this list i've seen many running into issues needing
json output from bird. For now i'm getting through this using textfsm and
then adding the headers to the output creating dict lists of routes or
other info from bird. This is probably the easiest method for parsing bird
output into json hopefully this is of use to some on this list.
<snip>
def _parse_bird_output(cls, template, text):
template='textfsm/' + template
with open(template) as f:
fsmtemplate = textfsm.TextFSM(f)
header = fsmtemplate.header
parsedtext = fsmtemplate.ParseText(text)
output = []
for item in parsedtext:
output.append(dict(zip(header,item)))
return json.dumps(output)
</snip>
<routedetail.textfsm>
Value Required Route
(\d+\.\d+\.\d+\.\d+\/\d+|([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}/\d{1,3})
Value Neighbor ((\w|\.)+)
Value Age (\d+-\d+-\d+ \d+:\d+:\d+)
Value NeiIP (\d+\.\d+\.\d+\.\d+)
Value OriginAS (\d+)
Value Filldown Table (.+)
Value BGP_origin (\w+)
Value BGP_aspath (.+)
Value BGP_nexthop (\d+\.\d+\.\d+\.\d+)
Value BGP_localpref (\d+)
Value BGP_community (.+)
Value BGP_med (\d+)
Start
^Table ${Table}:
^${Route}.*\[${Neighbor} ${Age} from ${NeiIP}\].*\[AS${OriginAS}.\]
^\s+BGP.origin: ${BGP_origin}
^\s+BGP.as_path: ${BGP_aspath}
^\s+BGP.next_hop: ${BGP_nexthop}
^\s+BGP.med: ${BGP_med}
^\s+BGP.local_pref: ${BGP_localpref} -> CommCheck
CommCheck
^\s+BGP.community: ${BGP_community} -> Record Start
# input buffer line doesn't have BGP.community in it, go on to the next
rule(s) but save
^\s*((?!BGP.community).)* -> Continue.Record
# check for the start of new table or route and move back to initial state
^Table ${Table}: -> Start
^${Route}.*\[${Neighbor} ${Age} from ${NeiIP}\].*\[AS${OriginAS}.\] ->
Start
</routedetail.textfsm>
<routes.textfsm>
Value Filldown Table (.+)
Value Required Route
(\d+\.\d+\.\d+\.\d+\/\d+|([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}/\d{1,3})
Value Neighbor ((\w|\.)+)
Value NeiIP (\d+\.\d+\.\d+\.\d+)
Value Age (\d+-\d+-\d+ \d+:\d+:\d+)
Value OriginAS (\d+)
Start
^BIRD \d+\.\d+\.\d+ ready. -> RTable
RTable
^Table ${Table}: -> Record
^${Route}.*\[${Neighbor} ${Age} from ${NeiIP}\].*\[AS${OriginAS}.\] ->
Record
^Table -> Start
</routes.textfsm>
<routecount.textfsm>
Value Table (\S+)
Value Routes (\d+)
Value Total (\d+)
Value Networks (\d+)
Value Tables (\d+)
Start
^${Routes} of ${Total} routes for ${Networks} networks in table ${Table}
-> Record
^Total: ${Routes} of ${Total} routes for ${Networks} networks in
${Tables} tables -> Record
</routecount.textfsm>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://trubka.network.cz/pipermail/bird-users/attachments/20210511/7116b911/attachment.htm>
More information about the Bird-users
mailing list