0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cisco-ios-configをtextfsmでパースする

Last updated at Posted at 2019-07-09

Cisco-iosの[show ip eigrp topology all-lins]の結果を、textfsm+ntc-templatesでパースする。

  • textfsmでパースエラーが出たので、デバック出力を追加したモジュールを作成
  • デフォルトのテンプレート[cisco_ios_show_ip_eigrp_topology.template]が未対応だったので、修正

ntc-templates
textfsm
Gist

textfsm

エラートレースを参考に、呼出前にテンプレート、入力ファイルの内容をSTDERR出力する。

    for line in lines:
      print >> sys.stderr, "DBG1:" , line
      self._CheckLine(line)
 
    for rule in self._cur_state:
      matched = self._CheckRule(rule, line)
      print >> sys.stderr, "DBG2:" , line , "->" , rule
      if matched:
        for value in matched.groupdict():
          self._AssignVar(matched, value)

        print >> sys.stderr, "DBG3:" , rule
        if self._Operations(rule):

diff結果

$ diff textfsm.py.org textfsm.py
1c1
< #!/usr/bin/python2.4
---
> 
853a854
>       print >> sys.stderr, "DBG1:" , line
872a874
>       print >> sys.stderr, "DBG2:" , line , "->" , rule
876a879
>         print >> sys.stderr, "DBG3:" , rule

ntc-templates

  • ADV_ROUTEがConected, Rstatic(nn/mm)に対応していなかたので、修正
  • P ip/maskl, xxx successors, FD is yyy, serno nnにも対応
# Value List ADV_ROUTER (\d+\.\d+\.\d+\.\d+\s+\(\d+\/\d+\))
Value List ADV_ROUTER ((\d+\.\d+\.\d+\.\d+\s+\(\d+\/\d+\))|\w+|(\w+\s+\(\d+\/\d+\)))

Start
  # Matches a route and captures if ${TAG} is use for the route and then moves to Gateway section
  ^${CODE}\s+${ROUTE}/${MASK},\s+${SUCCESSORS}\s+successors,\s+FD\s+is\s+${FD},\s+tag\s+is\s+${TAG} -> Gateway
  ## -- hiroshi : tag -> serno
  ^${CODE}\s+${ROUTE}/${MASK},\s+${SUCCESSORS}\s+successors,\s+FD\s+is\s+${FD},\s+serno\s+${TAG} -> Gateway

Gateway
  ## -- hiroshi
  # These are the same as above and capture the next set of routes
  ^${CODE}\s+${ROUTE}/${MASK},\s+${SUCCESSORS}\s+successors,\s+FD\s+is\s+${FD},\s+tag\s+is\s+${TAG}
  ^${CODE}\s+${ROUTE}/${MASK},\s+${SUCCESSORS}\s+successors,\s+FD\s+is\s+${FD},\s+serno\s+${TAG}
$ diff cisco_ios_show_ip_eigrp_topology.template.org cisco_ios_show_ip_eigrp_topology.template
9c9,13
< Value List ADV_ROUTER (\d+\.\d+\.\d+\.\d+|\w+)
---
> # 0.0.0.0 (fdval/adval)
> # Connected
> # Rstatic (fdval/adval)
> # Value List ADV_ROUTER (\d+\.\d+\.\d+\.\d+\s+\(\d+\/\d+\))
> Value List ADV_ROUTER ((\d+\.\d+\.\d+\.\d+\s+\(\d+\/\d+\))|\w+|(\w+\s+\(\d+\/\d+\)))
20a25,26
>   ## -- hiroshi : tag -> serno
>   ^${CODE}\s+${ROUTE}/${MASK},\s+${SUCCESSORS}\s+successors,\s+FD\s+is\s+${FD},\s+serno\s+${TAG} -> Gateway
29a36
>   ## -- hiroshi  
30a38
>   ^\s+via\s+${ADV_ROUTER}${OUT_INTERFACE}?
34a43,44
>   ## -- hiroshi
>   ^${CODE}\s+${ROUTE}/${MASK},\s+${SUCCESSORS}\s+successors,\s+FD\s+is\s+${FD},\s+serno\s+${TAG}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?