There is a way to do this but it's a bit involved. We do something similar to return JSON from CLI commands.when writing EXOS python apps.
Create a script cli2xml.py with the following 3 lines:
import sys
cmd = ' '.join(sys.argv[1:])
print exsh.clicmd(cmd, xml=True)You can test this by the CLI command: run script cli2xml.py show port 1 no-refresh
It's also handy when you want to see what the XML output of a command looks like before writing any code.
In your python application, invoke the EXOS command shell to run cli2xml.py with your command.
Below is an example:
# This is an example of a class that would interface to cli2xml.py in the Python application # environment
import subprocess
class ExosCliToXml(object):
def __init__(self):
self.dummy = None
@staticmethod
def exos_cli2xml(cmd):
shell_cmd = "/exos/bin/exsh -n 0 -b -c 'run script cli2xml.py {0}'".format(cmd)
p = subprocess.Popen([shell_cmd], shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdoutdata, stderrdata) = p.communicate()
return (stdoutdata, stderrdata)
# test stub
cli = ExosCliToXml()
while True:
cmd = raw_input('enter command > ')
if cmd in ['quit', 'exit']:
break
(stdoutdata, stderrdata) = cli.exos_cli2xml(cmd) # print the XML output from the command
print stdoutdata
If you wanted to use JSON, there is already a script shipping with EXOS: cli2json.py
E.g. run script cli2json.py show port 1In the class, change cli2xml.py to cli2json.py for JSON output.to change from XML to JSON