<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069661#M1992</link>
    <description>&lt;P&gt;I would suggest Cisco Genie if you working with Cisco primarily -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.cisco.com/developer/pyats-genie-transform" target="_blank" rel="noopener"&gt;https://blogs.cisco.com/developer/pyats-genie-transform&lt;/A&gt;&lt;/P&gt;&lt;P&gt;but of course, its a personal choice end of the day to get the job done.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Apr 2020 11:34:37 GMT</pubDate>
    <dc:creator>omz</dc:creator>
    <dc:date>2020-04-20T11:34:37Z</dc:date>
    <item>
      <title>Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069348#M1989</link>
      <description>&lt;P&gt;&lt;BR /&gt;This is sample output of Cisco Switch show version command.&lt;/P&gt;&lt;PRE&gt;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.&lt;/PRE&gt;&lt;P&gt;Objective: If string 'Cisco IOS Software' is found, I would like to print the whole line.&lt;/P&gt;&lt;P&gt;To make it easier to understand, I put `show version` output in variable `shvar`&lt;/P&gt;&lt;PRE&gt;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.
'''&lt;/PRE&gt;&lt;P&gt;Search with `if`&lt;/P&gt;&lt;PRE&gt;&amp;gt;&amp;gt;&amp;gt; if 'Cisco IOS Software' in shvar:
...     print('Found ... print line')
... 
Found ... print line
&amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;P&gt;Search with `find`&lt;/P&gt;&lt;PRE&gt;&amp;gt;&amp;gt;&amp;gt; if shvar.find('Cisco IOS Software') &amp;gt; 0:
... print('Found ... print line')
...
Found ... print line
&amp;gt;&amp;gt;&amp;gt;&lt;/PRE&gt;&lt;P&gt;The question is how do I print the line matches with 'Cisco IOS Software'?&lt;/P&gt;&lt;P&gt;Desired Output&lt;/P&gt;&lt;PRE&gt;Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(2)SE, RELEASE SOFTWARE (fc1)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 05:19:17 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069348#M1989</guid>
      <dc:creator>write_erase</dc:creator>
      <dc:date>2020-04-20T05:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069583#M1990</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this a duplicate post or there is a different requirement for this?&lt;/P&gt;&lt;P&gt;Did my reply to the other post help?&lt;/P&gt;&lt;P&gt;I used -&amp;nbsp;&lt;A href="https://regex101.com/#python" target="_blank"&gt;https://regex101.com/#python&lt;/A&gt;&lt;/P&gt;&lt;P&gt;to build the regex&amp;nbsp;&lt;/P&gt;&lt;P&gt;more on regex -&amp;nbsp;&lt;A href="https://www.w3schools.com/python/python_regex.asp" target="_blank"&gt;https://www.w3schools.com/python/python_regex.asp&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 09:55:37 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069583#M1990</guid>
      <dc:creator>omz</dc:creator>
      <dc:date>2020-04-20T09:55:37Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069654#M1991</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;A href="https://pynet.twb-tech.com/blog/automation/netmiko-textfsm.html" target="_self"&gt;https://pynet.twb-tech.com/blog/automation/netmiko-textfsm.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Sergiu&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 11:19:21 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069654#M1991</guid>
      <dc:creator>Sergiu.Daniluk</dc:creator>
      <dc:date>2020-04-20T11:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069661#M1992</link>
      <description>&lt;P&gt;I would suggest Cisco Genie if you working with Cisco primarily -&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.cisco.com/developer/pyats-genie-transform" target="_blank" rel="noopener"&gt;https://blogs.cisco.com/developer/pyats-genie-transform&lt;/A&gt;&lt;/P&gt;&lt;P&gt;but of course, its a personal choice end of the day to get the job done.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 11:34:37 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069661#M1992</guid>
      <dc:creator>omz</dc:creator>
      <dc:date>2020-04-20T11:34:37Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069771#M1993</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/453357"&gt;@write_erase&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(love the name btw) &amp;nbsp;You have some great suggestions here and as you can see you have lots of options:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;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)&lt;/P&gt;&lt;P&gt;3. Python script with Netmiko and TextFMS separately - you still get a string response that you then send to the TextFSM engine&lt;/P&gt;&lt;P&gt;for parsing and you get back &lt;STRONG&gt;structured data&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://gratuitous-arp.net/a-quick-example-of-using-textfsm-to-parse-data-from-cisco-show-commands-python3-version/" target="_blank" rel="noopener"&gt;https://gratuitous-arp.net/a-quick-example-of-using-textfsm-to-parse-data-from-cisco-show-commands-python3-version/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://gratuitous-arp.net/building-a-custom-textfsm-template/" target="_blank" rel="noopener"&gt;https://gratuitous-arp.net/building-a-custom-textfsm-template/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;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 &lt;STRONG&gt;structured data&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;5. Python script or CLI using PyATS which gives you &lt;STRONG&gt;structured data&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are more options but lets focus on python and netmiko.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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. &amp;nbsp;Getting things back in structured way lets you get at that information faster and easier.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example for 1:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    # 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)&lt;/PRE&gt;&lt;P&gt;At the end you get back:&lt;/P&gt;&lt;PRE&gt;******** FOUND LINE! ******
 NXOS: version 9.3(3)&lt;/PRE&gt;&lt;P&gt;Now you may really want "9.3(3)" so you have to parse some more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example 2 - see the response from&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/513338"&gt;@omz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example 4 - Netmiko is now TextFMS aware so you can reduce your coding effort &amp;nbsp;by just using the "use_textfsm=True" option within nemiko. &amp;nbsp; You need to set your environment up properly and you need to clone the Network to Code textfms templates library.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;f"\n===============  Netmiko with TEXTFSM OPTION  ==============="&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;try&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;BR /&gt;&lt;/SPAN&gt;    dev_conn &lt;SPAN&gt;= &lt;/SPAN&gt;netmiko&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;ConnectHandler&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;**&lt;/SPAN&gt;dev&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;    dev_conn&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;enable&lt;/SPAN&gt;&lt;SPAN&gt;()&lt;BR /&gt;&lt;/SPAN&gt;    response &lt;SPAN&gt;= &lt;/SPAN&gt;dev_conn&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;send_command&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'show version'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;use_textfsm&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;True&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;f"\nResponse is of type {&lt;/SPAN&gt;&lt;SPAN&gt;type&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;response&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;SPAN&gt;}\n"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;response&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;f"The OS is {&lt;/SPAN&gt;response&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;][&lt;/SPAN&gt;&lt;SPAN&gt;'os'&lt;/SPAN&gt;&lt;SPAN&gt;]&lt;/SPAN&gt;&lt;SPAN&gt;}"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;f"The Platform is {&lt;/SPAN&gt;response&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;][&lt;/SPAN&gt;&lt;SPAN&gt;'platform'&lt;/SPAN&gt;&lt;SPAN&gt;]&lt;/SPAN&gt;&lt;SPAN&gt;}"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;f"The boot image is {&lt;/SPAN&gt;response&lt;SPAN&gt;[&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;][&lt;/SPAN&gt;&lt;SPAN&gt;'boot_image'&lt;/SPAN&gt;&lt;SPAN&gt;]&lt;/SPAN&gt;&lt;SPAN&gt;}"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;except &lt;/SPAN&gt;&lt;SPAN&gt;Exception &lt;/SPAN&gt;&lt;SPAN&gt;as &lt;/SPAN&gt;e&lt;SPAN&gt;:&lt;BR /&gt;&lt;/SPAN&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;e&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;Here is what you get:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;===============  Netmiko with TEXTFSM OPTION  ===============

Response is of type &amp;lt;class 'list'&amp;gt;

[{'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&lt;/PRE&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One final note. I've worked with TextFMS alot because I can't always dynamically get to the devices I'm working with. &amp;nbsp;Many times I don't have access to a clients network initially so they share files of show commands. &amp;nbsp;TextFSM does not care how you get the data to it to parse. &amp;nbsp;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. &amp;nbsp;I just wanted to mention that distinction in case it was helpful for you.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 14:39:56 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069771#M1993</guid>
      <dc:creator>Claudia de Luna</dc:creator>
      <dc:date>2020-04-20T14:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko: How to print out specific line matches with 'Cisco IOS Software' in the 'show version' command</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069827#M1994</link>
      <description>Thanks omz, I accepted your solution. I was not aware of the duplicate post</description>
      <pubDate>Mon, 20 Apr 2020 15:21:32 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-how-to-print-out-specific-line-matches-with-cisco/m-p/4069827#M1994</guid>
      <dc:creator>write_erase</dc:creator>
      <dc:date>2020-04-20T15:21:32Z</dc:date>
    </item>
  </channel>
</rss>

