Python Forum

Full Version: How to caputre an output of a CLI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to capture an output of a CLI "show lic ?" in a variable or in buffer.

How to do it in python. I was using tcl where it has "receive_buffer" to capture it, not sure how to do it on python.

Please find the output below

Switch#show lic ?
comments Displays comments about the licenses.
feature-version Indicates the version of the feature that is using or can
use this license.
right-to-use Displays all the RTU licenses.
Look at the Subprocess module.
You can capture and store the output in a variable by pointing sys.stdout to the variable and executing your command.
eg->
o_variable = StringIO()      # variable to store stdout stream
sys.stdout = o_variable      # pointing stdout to variable
#Execute your commands
sys.stdout = sys.__stdout__  #restore std out
output = o_variable.getvalue()
print(output)