Python Forum
How to caputre an output of a CLI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to caputre an output of a CLI
#1
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.
Reply
#2
Look at the Subprocess module.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020