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

How to parse data using python

Posted on July 29, 2021August 15, 2021 by Gurpreet Kochar

Assume you want to extract below details from the output of “show version” from multiple devices at once. This post is going to talk about how to parse data using python

Using this method you can extract anything you want from any other output too. For instance, you can extract all BGP neighbour info from the output of “show ip bgp summary”. The primary skill required to understand how to parse data using python is basic understanding of regular expressions and ofcourse little bit of python.

How to read files in bulk with python for Network Engineers
https://networkautomationlane.in/how-to-read-files-in-bulk-with-python/

You could use a site like regex101 to test your regular expressions.

Sample output of show version from cisco device

rtr-012-ce01#show version

Cisco IOS XE Software, Version 03.16.05.S - Extended Support Release
Cisco IOS Software, ISR Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 15.5(3)S5, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Thu 19-Jan-17 09:28 by mcpre

Lets see how we can parse data using python

Use Case#1 — Extract Hostname

How to parse data using python
Regular expression to capture hostname

We know that hostname is found at start of line followed by a # and word “show”. To cater to this extract we could use a regular expression like “^(\S+)#show”. May be in some other devices the hostname may be followed by a “>” or any other symbol. You can modify the regex accordingly.

Lets break this regular expression

  • ^ asserts position at the start of a line.
  • ( ) represents a capturing group, basically the match that you are interested in.
  • \S represents any non-whitespace character
  • + matches the previous token between one and unlimited times, as many times as needed.
  • #show represents the actual character match
import os
import re

for file in os.listdir('input'):
    with open('input/' + file, 'r') as f:
        data = f.read()
        print(re.findall('^(\S+)#show', data, re.M))
╰─ python3 script1.py                                                                                                                                                                      ─╯
['rtr-012-ce01']
['rtr-012-ce02']
['rtr-039-ce02']
['rtr-039-ce01']
['rtr-017-ce01']

Since there is only 1 item in the returned list. We can use list indexing to fetch the first item from list.

        print(re.findall('^(\S+)#show', data, re.M)[0])
╰─ python3 script1.py                                                                                                                                                                      ─╯
rtr-012-ce01
rtr-012-ce02
rtr-039-ce02
rtr-039-ce01
rtr-017-ce01

Explanation:-

We are importing python’s re module and using findall method. We are telling python to find all matches that match that regular expression pattern we have specified in the data variable using re.M flag. re.M or re.MULTILINE tells python to understand ^ as start of line and $ as end of line.

re.findall returns a list of matches, instead of using re.findall, you could have also used re.search, re.match, i just prefer to use re.findall. For a single deterministic match, re.search/re.match is probably better but out of personal experience, re.findall is better in the long run when you are dealing with complex matches where you want to return multiple matches from the same output. For example, you might want to return all interfaces from the output of “show ip int br | ex unass” or all bgp neighbor IPs from “show ip bgp summary”.

This is just an introduction on how to parse data using python of command outputs. In other blog posts i will combine all these concepts into a real world use case.

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 “How to parse data using python”

  1. Pingback: How to read files in bulk with python for Network Engineers. - Network Automation

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
 

Loading Comments...
 

    %d