1

I wonder if anybody could help me with this challenge.

I have a folder with the following files:

switch01.txt
switch02.txt

Each file has the following contents:

show running-config

Building configuration...

Current configuration : 23611 bytes
!
! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin
! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin


show version

Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Sat 19-Aug-17 09:28 by prod_rel_team

ROM: Bootstrap program is C3750 boot loader

show inventory

Interface              IP-Address      OK? Method Status                Protocol
Vlan1                  192.168.77.40   YES manual up                    up      
GigabitEthernet1/0/1   unassigned      YES unset  up                    up      
GigabitEthernet1/0/2   unassigned      YES unset  up                    up      
GigabitEthernet1/0/3   unassigned      YES unset  up                    up      

I want to split each file in the folder by the delimiter "show+whatever" and name each file by that delimiter. In this case, each file has 3 delimiters: show running-config, show version and show show inventory.

After splitting it should look like this:

Folder contents:

switch01$$$show running-config$$$.txt
switch01$$$show version$$$.txt
switch01$$$show inventory$$$.txt
switch02$$$show running-config$$$.txt
switch02$$$show version$$$.txt
switch02$$$show inventory$$$.txt

contents of the switch01$$$show running-config$$$.txt text file:

Building configuration...

Current configuration : 23611 bytes
!
! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin
! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin

contents of the switch01$$$show version$$$.txt text file:

Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Sat 19-Aug-17 09:28 by prod_rel_team

ROM: Bootstrap program is C3750 boot loader

contents of the switch01$$$show inventory$$$.txt text file:

Interface              IP-Address      OK? Method Status                Protocol
Vlan1                  192.168.77.40   YES manual up                    up      
GigabitEthernet1/0/1   unassigned      YES unset  up                    up      
GigabitEthernet1/0/2   unassigned      YES unset  up                    up      
GigabitEthernet1/0/3   unassigned      YES unset  up                    up      

and same for switch02.txt.

I pieced this code together which uses counters to uniquely name each file:

    if filename.endswith(".txt"):
        with open(filename,'r') as file:
            output = file.read()
            for command in output:
                splitter = re.split('show\s(?:running-config|version|inventory)',output, flags=re.IGNORECASE)
                counter=0
                for item in splitter:
                    with open(filename.replace(".txt","")+'$$$'+str(counter)+'$$$'+'.txt', 'w') as f:
                        f.write("%s\n" % item)
                        counter = counter + 1

Which makes the files look like this:

switch01$$$0$$$.txt
switch01$$$1$$$.txt
switch01$$$2$$$.txt
switch01$$$3$$$.txt

Also at this point, switch01$$$0$$$.txt is blank and unnecessary.

What needs to happen to name each file by the delimiter used in regex?

atahu
  • 11
  • 1
  • 2

1 Answers1

0

Using lark, you can convert your switch*.txt to json data and then you can process it the way you want.

% ls
s.lark      s.py        switch01.txt    switch02.txt
% cat s.lark
start : so+

so: action details+

action: /show [a-z-]+/
details: /.+/

%import common.WS
%ignore WS
% cat s.py
import json
from typing import List

import lark
from lark import Lark, Transformer

import sys


class TreeToJson(Transformer):

    def so(self, s):
        d = []
        for i in s[1:]:
            d.append(i)
        r = {"action": s[0]}

        r.update({"details": "\n".join(d)})
        return r


    def action(self, s):
        return s[0].value

    def details(self, s: List[lark.Token]):
        for i in s:
            return i


if __name__ == '__main__':

    with open(sys.argv[1]) as f:
        text = f.read()

    with open("s.lark", 'r') as grammar:
        parser = Lark(grammar, parser='lalr')
        tree = parser.parse(text)


        # uncomment this to see the tree in pretty view
        # print(tree.pretty(indent_str="    "))

        a: lark.Tree = TreeToJson().transform(tree)
        print(json.dumps(a.children, indent=4))
% python3 s.py switch01.txt
[
    {
        "action": "show running-config",
        "details": "Building configuration...\nCurrent configuration : 23611 bytes\n!\n! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin\n! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin"
    },
    {
        "action": "show version",
        "details": "Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2017 by Cisco Systems, Inc.\nCompiled Sat 19-Aug-17 09:28 by prod_rel_team\nROM: Bootstrap program is C3750 boot loader"
    },
    {
        "action": "show inventory",
        "details": "Interface              IP-Address      OK? Method Status                Protocol\nVlan1                  192.168.77.40   YES manual up                    up      \nGigabitEthernet1/0/1   unassigned      YES unset  up                    up      \nGigabitEthernet1/0/2   unassigned      YES unset  up                    up      \nGigabitEthernet1/0/3   unassigned      YES unset  up                    up     "
    }
]
% python3 s.py switch02.txt
[
    {
        "action": "show running-config",
        "details": "Building configuration...\nCurrent configuration : 23611 bytes\n!\n! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin\n! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin"
    },
    {
        "action": "show version",
        "details": "Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2017 by Cisco Systems, Inc.\nCompiled Sat 19-Aug-17 09:28 by prod_rel_team\nROM: Bootstrap program is C3750 boot loader"
    },
    {
        "action": "show inventory",
        "details": "Interface              IP-Address      OK? Method Status                Protocol\nVlan1                  192.168.77.40   YES manual up                    up      \nGigabitEthernet1/0/1   unassigned      YES unset  up                    up      \nGigabitEthernet1/0/2   unassigned      YES unset  up                    up      \nGigabitEthernet1/0/3   unassigned      YES unset  up                    up     "
    }
]
Suku
  • 3,820
  • 1
  • 21
  • 23