Python Forum
How to caputre an output of a CLI - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to caputre an output of a CLI (/thread-4308.html)



How to caputre an output of a CLI - vel9194 - Aug-07-2017

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.


RE: How to caputre an output of a CLI - sparkz_alot - Aug-07-2017

Look at the Subprocess module.


RE: How to caputre an output of a CLI - hbknjr - Aug-07-2017

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)