본문 바로가기
Python

IOS version 추출

by Ao1 2023. 2. 1.
import glob
import re
import os

DEFAULT_PATH = os.path.dirname(os.path.abspath(__file__))

SEARCH_TEXT = "Cisco IOS Software"
START_TEXT = "Version "
END_TEXT = " RELEASE"

Folder_NAME = str(input('Log 폴더 명 : '))
SEARCH_NAME = str(input('실행한 Command(sh ver | show ver) : '))

DATA = []
NAME = []

VERSION_LINE = re.compile(SEARCH_TEXT)
DEVICE_LINE = re.compile(SEARCH_NAME)

for i in glob.glob(DEFAULT_PATH + '/' + Folder_NAME + r'\*.log'):
    with open(i, 'r') as f:
        for x, y in enumerate(f.readlines(),1):
            version_data = VERSION_LINE.findall(y)
            device_name = DEVICE_LINE.findall(y)
            if version_data:
                START_NUM = y.find(START_TEXT)
                END_NUM = y.find(END_TEXT)
                DATA.append(y[START_NUM+8:END_NUM-1])
            if device_name:
                END_NUM = y.find(SEARCH_NAME)
                NAME.append(y[0:END_NUM-1])
             
csv_f = open(DEFAULT_PATH + "\IOS Version.csv", "w")
for i in range(len(NAME)):
    csv_f.write(NAME[i] + ',' + DATA[i] + '\n')

csv_f.close