Question

ExtremeXOS - JSON Based CLI Output from python process - how?

  • 17 December 2019
  • 2 replies
  • 901 views

Hey Extreme,

 

So, I am trying to build a stateful python api that lives on the switch, reason being is I want to dump a json based document of a “state”, and have the switch get it’s self to this state - think automation without poking configure commands via ssh.

 

Mostly everything is working, if I run as a “script”, everything works via exsh python environment which lets me call commands, get XML back and process the data. I can also configure the switch to a state that I want it to be in nicely.

 

However, when running as a process on the switch, it’s not letting me import exsh (this is OK, I guess, as it’s a different environment). I am trying to use api.exec_cli, api.exec_exsh_cli et all to grab the data, however that’s … not working either. I can call api.exec_cli([‘’show port no]) to get unstructured output, however when I call api.exec_cli([‘run script cli2json.py show port 1’]), it’s not returning any data and is throwing a CLICommandError exception - with run script not supported. If I use the api.exec_exsh_cli method (that uses subprocesses).

(debug) pe1.dev.49 # show conf | inc process
create process test python-module test start auto vr VR-Mgmt

 

Per Dave Hammers reply in another thread, using subprocess here also is not working, I am getting a exit return 255 and it’s just .. not returning anything.

def showcmd(cmds):
try:
return subprocess.check_output(
"/exos/bin/exsh -c 'run script cli2json.py show port no'", shell=True
)

except subprocess.CalledProcessError as e:
logger.error("Exception hello(), {}".format(e), exc_info=True)

try:
return api.exec_cli(["run script cli2json.py show port no"])

except BaseException as e:
logger.error("Exception hello(), {}".format(e), exc_info=True)

ERROR:ExpyRunner:test:showcmd.46:: Exception hello(), Command '/exos/bin/exsh -c 'run script cli2json.py show port no'' returned non-zero exit status 255
… snip ...
CalledProcessError: Command '/exos/bin/exsh -c 'run script cli2json.py show port no'' returned non-zero exit status 255

 

The return data, from the second try block (that does return)

This command ("run script cli2json.py show port no") is not supported using execCLI XML

API.

 

Taking this a step further, if I break into developer_mode :wink: and run with a base python command, it actually works perfectly and gives the raw + json output. I feel like there is a permission issue that’s causing this within the expy environment used for processes - or am I doing something wrong?

/exos/bin # cat /tmp/test.py
import subprocess
print(subprocess.check_output("/exos/bin/exsh -c 'run script cli2json.py show port no'", shell=True ))

/exos/bin # python /tmp/test.py
[{"CLIoutput": "Port Summary\nPort ... snip ... "}, "status": "SUCCESS"}]

 

I am testing this all against a VirtualBox 30.2.1.8 based image. I’d need a solution that works on 16.1.4.2 and greater.

 

Any thoughts on how I can get structured data back (XML or JSON) from a show command, in a python process under expy, that actually works without running all the boxes in developer mode?

 

Cheers & Merry Christmas!
Nick


2 replies

Userlevel 2

Hi Nick,

old topic, but maybe still of interest: Have you had a look at the REST API provided by the switches themselves? As far as I have seen, that API returns JSON by default.

https://rawgit.com/extremenetworks/EXOS_Apps/master/REST/docs/RESTCONF.html
https://github.com/extremenetworks/EXOS_Apps/blob/master/REST/examples/restget.py

Userlevel 7

Hi,

 

old thread, but maybe still needing an answer?

If you want json output for any commands, you either have to use jsonrpc (21.1+), restconf (22.1+) if available or use debug cfgmgr show commands that are… undocumented and impossibe to guess as for the parameters to (eventually) provide.

As for running nested script, I doubt it works fine, because they are in different sessions.

Reply