Parsing CISCO's con/aux/vty configuration using TextFSM

50 views Asked by At

I am attempting to parse the configuration of the consoles (con/aux/vty) from a Cisco router using TextFSM, specifically, the output of the command show running-config | partial line. The issue arises because this portion of the configuration doesn't end the "line vtys" with " ! " as usual, which causes a problem when trying to detect the end of the record without missing that line as the beginning of the next one.

I was expecting to extract the following data in a structured format: "LINE_TYPE": con/aux/vty "LINE_NUM": number "ACCESS_CLASS": number "EXEC_TIMEOUT": number "LOGIN_AUTHENTICATION": string "STOPBITS": number

Example command output:

line con 0
 exec-timeout 0 0
 login authentication autCON
 stopbits 1
!
line aux 0
 stopbits 1
 login authentication autAUX
line vty 0 4
 exec-timeout 1 0
 login authentication autAAA
line vty 5 15
 exec-timeout 2 0
 login authentication autBBB
 password yourmom
line vty 16
 access-class 55 in vrf-also
 exec-timeout 3 0
 login authentication autCCC
!

I tested the following template (among hundreds of other trials!) but it gets misaligned. This can be observed in the LINE_TYPE and LOGIN_AUTHENTICATION lines of the result I have attached at the end:

TextFSM File

Value Required LINE_TYPE (\S+)
Value LINE_NUM (\S.*)
Value EXEC_TIMEOUT (\d+ \d+|\S+)
Value STOPBITS (\d+)
Value TRANSPORT (\S+)
Value AUTHORIZATION (\S+)
Value ACCOUNTING_CONNECTION (\S.*)
Value ACCOUNTING_EXEC (\S+)
Value LOGIN_AUTHENTICATION (\S+)
Value HISTORY_SIZE (\d+)
Value ACCESS_CLASS (\S+.*)

Start
  ^line ${LINE_TYPE} ${LINE_NUM} -> Continue.Record
  ^\s*exec-timeout ${EXEC_TIMEOUT}*
  ^\s*stopbits ${STOPBITS}*
  ^\s*transport input ${TRANSPORT}*
  ^\s*authorization exec ${AUTHORIZATION}*
  ^\s*accounting connection ${ACCOUNTING_CONNECTION}*
  ^\s*accounting exec ${ACCOUNTING_EXEC}*
  ^\s*login authentication ${LOGIN_AUTHENTICATION}*
  ^\s*history size ${HISTORY_SIZE}*
  ^\s*access-class ${ACCESS_CLASS}*
  ^\Z -> Record

Result

[
    {
        "ACCESS_CLASS": "",
        "ACCOUNTING_CONNECTION": "",
        "ACCOUNTING_EXEC": "",
        "AUTHORIZATION": "",
        "EXEC_TIMEOUT": "",
        "HISTORY_SIZE": "",
        "LINE_NUM": "0",
        "LINE_TYPE": "con",
        "LOGIN_AUTHENTICATION": "",
        "STOPBITS": "",
        "TRANSPORT": ""
    },
    {
        "ACCESS_CLASS": "",
        "ACCOUNTING_CONNECTION": "",
        "ACCOUNTING_EXEC": "",
        "AUTHORIZATION": "",
        "EXEC_TIMEOUT": "0 0",
        "HISTORY_SIZE": "",
        "LINE_NUM": "0",
        "LINE_TYPE": "aux",
        "LOGIN_AUTHENTICATION": "autCON",
        "STOPBITS": "1",
        "TRANSPORT": ""
    },
    {
        "ACCESS_CLASS": "",
        "ACCOUNTING_CONNECTION": "",
        "ACCOUNTING_EXEC": "",
        "AUTHORIZATION": "",
        "EXEC_TIMEOUT": "",
        "HISTORY_SIZE": "",
        "LINE_NUM": "0 4",
        "LINE_TYPE": "vty",
        "LOGIN_AUTHENTICATION": "autAUX",
        "STOPBITS": "1",
        "TRANSPORT": ""
    },
    {
        "ACCESS_CLASS": "",
        "ACCOUNTING_CONNECTION": "",
        "ACCOUNTING_EXEC": "",
        "AUTHORIZATION": "",
        "EXEC_TIMEOUT": "1 0",
        "HISTORY_SIZE": "",
        "LINE_NUM": "5 15",
        "LINE_TYPE": "vty",
        "LOGIN_AUTHENTICATION": "autAAA",
        "STOPBITS": "",
        "TRANSPORT": ""
    },
    {
        "ACCESS_CLASS": "",
        "ACCOUNTING_CONNECTION": "",
        "ACCOUNTING_EXEC": "",
        "AUTHORIZATION": "",
        "EXEC_TIMEOUT": "2 0",
        "HISTORY_SIZE": "",
        "LINE_NUM": "16",
        "LINE_TYPE": "vty",
        "LOGIN_AUTHENTICATION": "autBBB",
        "STOPBITS": "",
        "TRANSPORT": ""
    }
]

0

There are 0 answers