2 min readFeb 26, 2023
Python ile Cisco Router ve Switchlerden Model/Seri Numarası/IOS Versiyonu Alma
Merhaba,
Aşağıda paylaşmış olduğum python fonksiyonu ile cisco router ve switch cihazlarınızdan “show version“ komutunun çıktısını kullanarak cihazlarınıza ait model, seri numarası ve ios versiyon bilgisini kolaylıkla alabilirsiniz.
def Cisco_CPE_Info(sh_ver):
# Device type detect
if "PID" in sh_ver:
# If the detected device is a router
sh_ver_parsed = sh_ver.split("\n")
# Resetting the counters
line, count = 0, 0
# Getting the router ios version with regex
router_ios = re.findall(r"flash.*:(.*).bin", sh_ver, re.IGNORECASE)[0]
# Find router inventory info line
for i in sh_ver_parsed:
count += 1
if "PID" in i:
line = count+1
break
# Extract required data from inventory info line
return_data = list(filter(None, sh_ver_parsed[line].replace("*", "").replace("\r", "").replace("\t", "").split(" ")))
return [return_data[1], return_data[2], router_ios]
else:
# If the detected device is a switch
sh_ver_parsed = sh_ver.split("\n")
# Resetting the counters
line, count = 0, 0
# Getting the switch serial number with regex for switches with different IOS
switch_serial = re.findall(r"System\ [S|s]erial\ [N|n]umber\s+\:\s(.*)", sh_ver, re.IGNORECASE)[0]
# Find switch inventory info line
for i in sh_ver_parsed:
count += 1
if "SW Version" in i:
line = count+1
break
# Extract required data from inventory info line
return_data = list(filter(None, sh_ver_parsed[line].replace("*", "").replace("\r", "").replace("\t", "").split(" ")))
return [return_data[2], switch_serial, return_data[4]]
Örnek Çıktı
['ME-3400E-24TS-M', 'FCW1915H012', 'ME340x-METROACCESSK9-M']
['C9200L-24T-4G', 'JAE24190FBC', 'CAT9K_LITE_IOSXE']
['C9200L-24T-4G', 'JAE24190F4Q', 'CAT9K_LITE_IOSXE']
['C887VAG-4G-GA-K9', 'FGL203123EG', 'c800-universalk9-mz.SPA.155-3.M3']