textfsm 사용법

dingdong·2022년 1월 14일
0
post-custom-banner

textfsm

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 이라는 라이브러리를 소개한다.



공식문서

textfsm github

textfsm 참고

설치 방법

pip install textfsm

1) 사용해보기 - 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

2) 몇가지 추가적으로 설치 필요

  • 아래의 코드를 실행하기 위해서는 다른 외부 라이브러리가 필요함.
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:~#

3) 사용해보기 - .template file

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

4) 사용해보기 - textfsm_exmple.py

  • dir2textfsm(path) : path str 을 받으면
    해당 path 의 파일을 읽어서
    TextFSM 을 통해서 parser 를 만들어서 반환해줍니다.
  • parser.ParseText(raw_data) : parser를 통해서 raw_data 를 parsing 합니다.
  • Dataframe 은 Table 형태로 데이터를 담아준다라고 생각
  • tablulate 를 통해서 Datafram 을 꾸며서 출력해준다.

코드📄

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
profile
자동화 개발
post-custom-banner

0개의 댓글