Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu

[Beginners] How to parse the output of show version

Posted on August 1, 2021August 1, 2021 by Gurpreet Kochar

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.

  • Remove the extra , at the end and make a string out of the list items.
  • 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

    Know someone who may benefit? Share this:

    • Tweet
    • Click to share on Telegram (Opens in new window) Telegram
    • Click to share on WhatsApp (Opens in new window) WhatsApp
    • Click to email a link to a friend (Opens in new window) Email
    • More
    • Click to print (Opens in new window) Print
    • Click to share on Reddit (Opens in new window) Reddit
    • Share on Tumblr
    • Pocket

    Like this:

    Like Loading...

    Related

    1 thought on “[Beginners] How to parse the output of show version”

    1. Pingback: How to read write csv with python for Network Automation Part 2

    Leave a ReplyCancel reply

    All Blog Posts
    My Resume

    Upcoming Posts

    Sorry - nothing planned yet!

    Recent Posts

    • How to backup configuration to TFTP Server using Ansible – Part II
    • How to backup network devices using Ansible – Part I
    • Netmiko SSH Proxy/JumpServer
    • A short note on SASE
    • Understanding Ansible

    Recent Comments

    1. Jack on Multithreading with Python for Network Engineers
    2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
    3. Jasper Horng on Netmiko SSH Proxy/JumpServer
    4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
    5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

    Archives

    • September 2022
    • February 2022
    • January 2022
    • December 2021
    • November 2021
    • October 2021
    • September 2021
    • August 2021
    • July 2021
    Topic Request / Suggestion
    Loading
    © 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
    %d