In this post we will parse the output of show version to fetch most common details required from it. We will try to learn step by step how to capture information like version, hostname, hardware model, uptime etc. You can use the concept to fetch any other detail you would like from any other command output.
Sample output of “show version”.
I have snipped the output only to show relevant information.
rtr-039-ce01#show version Cisco IOS Software, C3900 Software (C3900-UNIVERSALK9-M), Version 15.1(4)M8, RELEASE SOFTWARE (fc2) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2014 by Cisco Systems, Inc. Compiled Fri 07-Mar-14 12:27 by prod_rel_team ROM: System Bootstrap, Version 15.0(1r)M16, RELEASE SOFTWARE (fc1) rtr-039-ce01 uptime is 1 year, 9 weeks, 1 day, 16 hours, 10 minutes System returned to ROM by power-on System restarted at 20:22:14 GMT Sat Dec 7 2019 System image file is "flash:c3900-universalk9-mz.SPA.151-4.M8.bin" Last reload type: Normal Reload . . . Cisco CISCO3925-CHASSIS (revision 1.0) with C3900-SPE100/K9 with 999424K/49152K bytes of memory. Processor board ID FTX1707AHVT 3 Gigabit Ethernet interfaces 1 terminal line DRAM configuration is 72 bits wide with parity enabled. 255K bytes of non-volatile configuration memory. 250880K bytes of ATA System CompactFlash 0 (Read/Write) . . . .
Let’s parse the output of show version
Base Code
import os
import re
for file in os.listdir('input'):
with open('input/' + file, 'r') as f:
data = f.read()
Extract Hostname
print(re.findall('^(\S+)#show', data, re.M)[0])
Extract Version
version = re.findall('Cisco .+? Version (\S+)', data, re.M)
print(version)
╰─ python3 script1.py ─╯
['03.16.05.S', '15.5(3)S5,'] >> Recent IOS-XE versions have two naming conventions
['03.16.03.S', '15.5(3)S3,']
['15.1(4)M8,']
['15.1(4)M8,']
['03.13.04.S', '15.4(3)S4,']
Let’s modify the script to yield exactly what we need.
version = [item.split(',')[0] for item in version]
print(version)
╰─ python3 script1.py ─╯
['03.16.05.S', '15.5(3)S5']
['03.16.03.S', '15.5(3)S3']
['15.1(4)M8']
['15.1(4)M8']
['03.13.04.S', '15.4(3)S4']
For each item in the version list, we are splitting the item at ‘,’ and extracting the first item after split which effectively returns the item without comma. There are other ways to do this too. Now we can join the list items to return the items as strings.
print(' -- '.join(version))
╰─ python3 script1.py ─╯
03.16.05.S -- 15.5(3)S5
03.16.03.S -- 15.5(3)S3
15.1(4)M8
15.1(4)M8
03.13.04.S -- 15.4(3)S4
1 thought on “[Beginners] How to parse the output of show version”