ExtremeSwitching (EXOS)

Expand all | Collapse all

Getting clean output from Extreme switch CLI using Python

  • 1.  Getting clean output from Extreme switch CLI using Python

    Posted 10-06-2017 14:35
    I am writing a python script to run some show commands on Extreme switches and storing output in a variable. I am able to accomplish that but the output is not as I would like. I am seeing the command and also switch name in my output but I would like to just see the result of my show command and nothing else.

    This is what I am seeing:

    print "ISC Port is: ", ISC

    ISC Port is: sh vlan ISC | i Tag:
    Tag: 1:1g
    Ext-SW1.com.5 #

    This is what I would like to see:

    ISC Port is: Tag: 1:1g

    Here is my code where I am reading output:

    ISC = "sh vlan ISC | i Tag:"

    tn.write(ISC + "\n")
    idx, obj, output = tn.expect(["\.\r\n\r\*"], int(timeout))
    print "ISC Port is: ", output[/code]How do I get rid of device name and my commands from output to show only output. I played around with "read_until" but can't seem to get it cleaned up. Example:

    tn.write(ISC + "\n")
    tn.read_until(".2 #")
    idx, obj, output = tn.expect(["\.\r\n\r\*"], int(timeout))
    print "ISC Port is: ", output[/code]Any help will be appropriated.



  • 2.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-06-2017 14:47
    If the switch is running EXOS 21.x or later, you could use JSONRPC instead of screen scraping the telnet display. An example script is here: https://github.com/extremenetworks/ExtremeScripting/tree/master/EXOS/Python/jsoncli


  • 3.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-08-2017 08:18
    As Dave mentionned, if you are running 21 or later, you really should consider JSONRPC.


  • 4.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-09-2017 12:21
    No, we are running 15.x.


  • 5.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-09-2017 16:15
    Since JSONRPC isn't available, I'd probably use regex to pull out the specific part you are looking for. I put together a quick example below:
    import re output = 'sh vlan ISC | i Tag:\nTag: 1:1g\nExt-SW1.com.5 #' p = re.compile('(Tag:\s*\d:\dg?)') m = p.search(output) output = m.group(1) print output [/code]
    This won't work for non-stacks, but it should give you an idea.



  • 6.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-09-2017 16:15
    And for what it returns:
    bash-3.2$ python regex.py Tag: 1:1g
    [/code]


  • 7.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-09-2017 16:30
    I am already using regex to extract what I need but I was hoping there is a cleaner way. My regex is not pretty due to all extra junk I am scrapping.


  • 8.  RE: Getting clean output from Extreme switch CLI using Python

    Posted 10-30-2017 16:10
    It's important to be using a good regex tool to cut down time on developing the right regex. There are lot's of good tools out there. I paid for an app regexbuddy but there is also https://regex101.com/ that has python support and a very nice interface.