cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
9118
Views
25
Helpful
5
Replies

Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command

write_erase
Level 1
Level 1


This is sample output of Cisco Switch show version command.

Switch#show version
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2012 by Cisco Systems, Inc.

Objective: If string 'Cisco IOS Software' is found, I would like to print the whole line.

To make it easier to understand, I put `show version` output in variable `shvar`

shvar = '''
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2012 by Cisco Systems, Inc.
'''

Search with `if`

>>> if 'Cisco IOS Software' in shvar:
...     print('Found ... print line')
... 
Found ... print line
>>> 

Search with `find`

>>> if shvar.find('Cisco IOS Software') > 0:
... print('Found ... print line')
...
Found ... print line
>>>

The question is how do I print the line matches with 'Cisco IOS Software'?

Desired Output

Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)

 

1 Accepted Solution

Accepted Solutions

omz
VIP Alumni
VIP Alumni

Hi 

Is this a duplicate post or there is a different requirement for this?

Did my reply to the other post help?

I used - https://regex101.com/#python

to build the regex 

more on regex - https://www.w3schools.com/python/python_regex.asp

View solution in original post

5 Replies 5

omz
VIP Alumni
VIP Alumni

Hi 

Is this a duplicate post or there is a different requirement for this?

Did my reply to the other post help?

I used - https://regex101.com/#python

to build the regex 

more on regex - https://www.w3schools.com/python/python_regex.asp

Thanks omz, I accepted your solution. I was not aware of the duplicate post

Sergiu.Daniluk
VIP Alumni
VIP Alumni

Hello,

If you are interested in going further with your analysis and parse outputs that have a specific schema/structure (show interface, show version, show module, show inventory), you can consider using TextFSM.

https://pynet.twb-tech.com/blog/automation/netmiko-textfsm.html 

 

Cheers,

Sergiu

omz
VIP Alumni
VIP Alumni

I would suggest Cisco Genie if you working with Cisco primarily - 

https://blogs.cisco.com/developer/pyats-genie-transform

but of course, its a personal choice end of the day to get the job done. 

Claudia de Luna
Spotlight
Spotlight

Hi @write_erase 

 

(love the name btw)  You have some great suggestions here and as you can see you have lots of options:

 

1. Python script with Netmiko - this gives you a string response that you have to manipulate and "parse" and you can do basic parsing just with Python

2. Python script with Netmiko and regular expressions using the re module - you still get a string response that you still have to manipulate to do basic parsing but you can do better parsing with the re module (regular expressions)

3. Python script with Netmiko and TextFMS separately - you still get a string response that you then send to the TextFSM engine

for parsing and you get back structured data

https://gratuitous-arp.net/a-quick-example-of-using-textfsm-to-parse-data-from-cisco-show-commands-python3-version/

https://gratuitous-arp.net/building-a-custom-textfsm-template/

4. Python script with Netmiko and TextFMS together (Netmiko can take your response and send it directly to TextFMS so you have parsed output into structured data

5. Python script or CLI using PyATS which gives you structured data

 

There are more options but lets focus on python and netmiko.

 

When I first started working with network programming this was one of the first things I did and I wasn't looking for that line as much as I was looking for the version of software on the device.  Getting things back in structured way lets you get at that information faster and easier.

 

Example for 1:

 

    # RAW Parsing with Python
    print(f"\n===============  Netmiko ONLY ===============")
    try:
        dev_conn = netmiko.ConnectHandler(**dev)
        dev_conn.enable()
        response = dev_conn.send_command('show version')
        print(f"\nResponse is of type {type(response)}\n")
        print(response)
        # because the response is a string we need to do some string manipulation
        # first we need to split the string into lines
        resp = response.splitlines()
        # now we should have a list in rest over which we can iterate
        print(f"\nSplit Response is of type {type(resp)}\n")
        print(resp)
        find_string = "NXOS: version"
        # look
        for line in resp:
            if find_string in line:
                print(f"******** FOUND LINE! ******\n{line}\n")

    except Exception as e:
        print(e)

At the end you get back:

******** FOUND LINE! ******
 NXOS: version 9.3(3)

Now you may really want "9.3(3)" so you have to parse some more.

 

Example 2 - see the response from @omz 

 

Example 3 - There is lots of information on this, I've included a write up I did a while ago and you have a link to Kurt's write up which is the definitive write up.

 

Example 4 - Netmiko is now TextFMS aware so you can reduce your coding effort  by just using the "use_textfsm=True" option within nemiko.   You need to set your environment up properly and you need to clone the Network to Code textfms templates library.

 

print(f"\n===============  Netmiko with TEXTFSM OPTION  ===============")
try:
dev_conn = netmiko.ConnectHandler(**dev)
dev_conn.enable()
response = dev_conn.send_command('show version', use_textfsm=True)
print(f"\nResponse is of type {type(response)}\n")
print(response)
print(f"The OS is {response[0]['os']}")
print(f"The Platform is {response[0]['platform']}")
print(f"The boot image is {response[0]['boot_image']}")

except Exception as e:
print(e)

Here is what you get:

 

===============  Netmiko with TEXTFSM OPTION  ===============

Response is of type <class 'list'>

[{'uptime': '0 day(s), 20 hour(s), 33 minute(s), 32 second(s)', 'last_reboot_reason': 'Unknown', 'os': '9.3(3)', 'boot_image': 'bootflash:///nxos.9.3.3.bin', 'platform': 'C9300v'}]
The OS is 9.3(3)
The Platform is C9300v
The boot image is bootflash:///nxos.9.3.3.bin

So depending on what you are trying to do you might want to look at the options that do less string manipulation and focus more on getting structured data back to you.

 

One final note. I've worked with TextFMS alot because I can't always dynamically get to the devices I'm working with.  Many times I don't have access to a clients network initially so they share files of show commands.  TextFSM does not care how you get the data to it to parse.  It can come from a nemiko connection as I showed above or it can come from a text file or a variable with the show commands result as you showed initially.  I just wanted to mention that distinction in case it was helpful for you.