Python 을 이용하여 데이터를 Parsing 해주는 라이브러리중 하나이다.
아래의 데이터는
사람이 보았을때는 테이블 형태여서
어떤 부분(행, 열)이 어떤걸 의미하는지 알 수 있다.
SWITCH# show mac
================================================================================
vid port mac addr permission status in use
================================================================================
1 eth0/1 22:22:33:33:44:aa OK static 0.00
Python 에서 변수에 저장이되면, 단지 문자열이다.
>>> raw = '''
SWITCH# show mac
================================================================================
vid port mac addr permission status in use
================================================================================
1 eth0/1 22:22:33:33:44:aa OK static 0.00
'''
>>> raw
'\nSWITCH# show mac\n================================================================================\n vid port mac addr permission status in use\n================================================================================\n 1 eth0/122:22:33:33:44:aa OK static 0.00\n\n'
>>>
textfsm 사용을 한다면, 아래와 같은 데이터(테이블)를 얻을 수 있다.
VLAN PORTS MAC PERMISSION STATUS IN_USE 0 1 eth0/1 22:22:33:33:44:aa OK static 0.00
문자들이 의미있는 데이터로 parsing 할 수 있는 것이 필요하다.
그래서 Parser 라이브러리 중에 textfsm 이라는 라이브러리를 소개한다.
pip install textfsm
SWITCH# show mac
================================================================================
vid port mac addr permission status in use
================================================================================
1 eth0/1 22:22:33:33:44:aa OK static 0.00
root@VirtualBox:~# cat requirements.txt
tabulate
pandas
textfsm
root@VirtualBox:~# pip install -r requirements.txt
Requirement already satisfied: tabulate in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 1)) (0.8.9)
Requirement already satisfied: pandas in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 2)) (1.3.5)
Requirement already satisfied: textfsm in /usr/local/lib/python3.8/dist-packages (from -r requirements.txt (line 3)) (1.1.2)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/lib/python3/dist-packages (from pandas->-r requirements.txt (line 2)) (2.7.3)
Requirement already satisfied: numpy>=1.17.3; platform_machine != "aarch64" and platform_machine != "arm64" and python_version < "3.10" in /usr/local/lib/python3.8/dist-packages (from pandas->-r requirements.txt (line 2)) (1.22.0)
Requirement already satisfied: pytz>=2017.3 in /usr/lib/python3/dist-packages (from pandas->-r requirements.txt (line 2)) (2019.3)
Requirement already satisfied: future in /usr/lib/python3/dist-packages (from textfsm->-r requirements.txt (line 3)) (0.18.2)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from textfsm->-r requirements.txt (line 3)) (1.14.0)
root@VirtualBox:~#
Value VLAN (\d+)
Value PORTS (\S+)
Value MAC ((\S{2}(\:))(\S{2}(\:))(\S{2}(\:))(\S{2}(\:))(\S{2}(\:))(\S{2}))
Value PERMISSION (\S+)
Value STATUS (\S+)
Value IN_USE (\S+)
Start
^(\s+)?${VLAN}\s+${PORTS}\s+${MAC}\s+${PERMISSION}\s+${STATUS}\s+${IN_USE} -> Record
EOF
코드📄
import pdb
import unittest
from io import StringIO
from tabulate import tabulate
from textfsm import TextFSM
from pandas import DataFrame
def dir2textfsm(path):
parser = None
try:
with open(path) as f:
data = f.read()
parser = TextFSM(StringIO(data))
except IOError:
print("No such TextFSM file : {}".format(path))
return parser
class TextfsmExample(unittest.TestCase):
def test_01(self):
parser = dir2textfsm('textfsm_exmple.template')
raw_data = """SWITCH# show mac
================================================================================
vid port mac addr permission status in use
================================================================================
1 eth0/1 22:22:33:33:44:aa OK static 0.00
"""
print(raw_data)
res = DataFrame(parser.ParseText(raw_data), columns=parser.header)
res_str = str(tabulate(res, headers="keys", tablefmt="grid"))
print(res_str)
실행💻
root@VirtualBox:~# python3 -m unittest textfsm_exmple.py
출력💻
SWITCH# show mac
================================================================================
vid port mac addr permission status in use
================================================================================
1 eth0/1 22:22:33:33:44:aa OK static 0.00
+----+--------+---------+-------------------+--------------+----------+----------+
| | VLAN | PORTS | MAC | PERMISSION | STATUS | IN_USE |
+====+========+=========+===================+==============+==========+==========+
| 0 | 1 | eth0/1 | 22:22:33:33:44:aa | OK | static | 0 |
+----+--------+---------+-------------------+--------------+----------+----------+
.
----------------------------------------------------------------------
Ran 1 test in 0.004s
OK