How to find a list of structured records in a router log with Python

Question:

We receive daily some dumps from our routers. This has been configured by the vendor of the routers. Now I would like to find the real records in these logs and put them in tables.

Here is a sample of such a dump file.

PZMO120#
PZMO120# show interface description

                                              IF      IF      IF                                                                                    IPV6
                      AF                      ADMIN   OPER    TRACKER                                                                               ADMIN
VPN    INTERFACE      TYPE  IP ADDRESS        STATUS  STATUS  STATUS   DESC                                                                         STATUS
------------------------------------------------------------------------------------------------------------------------------------------------------------
0      ge0/0          ipv4  xx.yy.zz.r/pp     Up      Up      Up       ORCH=NETWORK - To INTERNET                                                   Up
0      ge0/1          ipv4  -                 Up      Up      NA       ORCH=NETWORK - TLOC Extension - B2B between Primary vEdge & Secondary vEdge  Up
0      ge0/2          ipv4  -                 Down    Down    NA       None                                                                         Up
65530  ge0/3          ipv4  -                 Up      Up      NA       ORCH=NETWORK - CUSTOMER LAN - Service VPN physical interface                 Up

PZMO120#
PZMO120#
PZMO120# show  vrrp

                                                                                                          PRIMARY                             TRACK   PREFIX
                GROUP                                            REAL      VRRP     OMP    ADVERTISEMENT  DOWN                                PREFIX  LIST
VPN  IF NAME    ID     VIRTUAL IP   VIRTUAL MAC        PRIORITY  PRIORITY  STATE    STATE  TIMER          TIMER    LAST STATE CHANGE TIME     LIST    STATE
--------------------------------------------------------------------------------------------------------------------------------------------------------------
1    ge0/3.510  10     zz.zz.zz.x   00:00:??:00:01:0a  150       150       primary  up     1              3        2022-12-06T19:32:06+00:00  -       -
2    ge0/3.511  11     yy.yy.yy.r   00:00:??:00:01:0b  150       150       primary  up     1              3        2022-12-06T19:32:06+00:00  -       -

PZMO120#
PZMO120# show  run vpn 1

I’m not a Python expert, but would like to create a system that recognized a predefined header, and knows that x lines further some real data is coming until the first blank line.

Asked By: Harry Leboeuf

||

Answers:

Finaly got something working, might not be the super code but might others give a head-start, to be refined.

def find_vrrp_table(routername, filecontent):
    result = ""
    regex_to_find = "(?s)(?<=VPN  IF NAME    ID     VIRTUAL IP  VIRTUAL MAC        PRIORITY  PRIORITY  STATE    STATE  TIMER          TIMER    LAST STATE CHANGE TIME     LIST    STATE)(.*?)(?=" + routername + ")"
    regex_result = re.search(regex_to_find, filecontent)
    if regex_result:
        result = regex_result.group()
    return result

and

def convert_interfaces_string_into_df(routername, interfaces_string):
    # Make a List with only records
    interfaces_list = interfaces_string.replace('r','').split('n')
    interfaces_list.pop(0)
    interfaces_list.pop(0)
    interfaces_list = list(filter(None, interfaces_list))
    # Convert Records into Pandas DataFrame
    colspecs = 0, 7, 22, 28, 46, 54, 62, 71, 148, 152
    df = pd.DataFrame([[row[i:k] for i, k in zip(colspecs[:-1], colspecs[1:])] for row in interfaces_list])
    df.columns = ['VPN', 'INTERFACE', 'AF_TYPE', 'IP_ADDRESS', 'ADMIN_STATUS', 'OPER_STATUS', 'TRACKER_STATUS', 'DESC', 'IPV6_ADMIN_STATUS']
    df['ROUTERNAME'] = routername
    df['LOG_DATE'] = dt.datetime.today().strftime("%m/%d/%Y")
    return spark.createDataFrame(df)
Answered By: Harry Leboeuf
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.