ā08-06-2024 09:21 AM - edited ā08-06-2024 09:24 AM
Hi All
I have following test created to run multiple commands against a a device within testbed. I would like to log the output to a variable (including the switch/router prompt) and print out as result. I know this functionality is already in the "pyats logs view" however I would like to store this in a global variable to be used later.
Code Snippet
I have created "console_output" as global variable (string formatted) and print the output in cleanup class.
How do I capture the below output into variable.
ā08-06-2024 12:35 PM
Not sure this will give you the 100% results you want, try the output attribute
output = self.device.configure(config_commands)
console_output += output
ā08-06-2024 08:48 PM - edited ā08-06-2024 09:00 PM
Thank you for your response. I tried that previously and it works however it ommits the switch/router prompt (eg SW1(config-if)# ) from the output. Any idea how to include this in the output?
2024-08-07T11:45:55: %AETEST-INFO: +------------------------------------------------------------------------------+
2024-08-07T11:45:55: %AETEST-INFO: | Starting section apply_config_multi |
2024-08-07T11:45:55: %AETEST-INFO: +------------------------------------------------------------------------------+
interface lo1000
ip address 1.1.1.1 255.255.255.255
description pyats_configured
no shutdown
do wr mem
Building configuration...
Compressed configuration from 1295 bytes to 864 bytes[OK]
ā08-06-2024 11:55 PM
No problem, sort of guessing here now. But try and add the dialog attribute this might contains the entire conversation
output = self.device.configure(config_commands)
dialog = output.dialog
console_output += dialog
only other way I can think is enable the capture buffer on the device be something like: self.device.cli_handler.capture_buffer_enable().
ā08-07-2024 12:22 PM
I have updated and tried as you mentioned and recieved the below error. Obviously I am missing something fundamental in terms of python. Regarding the cli_handler method, can you guide me what would be required here? The desired output comes from UNICON-INFO , is there no easy way just to duplicate the output it is already outputing when it hits that method in my test?
2024-08-08T03:17:52: %AETEST-INFO: +------------------------------------------------------------------------------+
2024-08-08T03:17:52: %AETEST-INFO: | Starting section apply_config_multi |
2024-08-08T03:17:52: %AETEST-INFO: +------------------------------------------------------------------------------+
2024-08-08 03:17:52,662: %UNICON-INFO: +++ SW1 with via 'cli': configure +++
config term
Enter configuration commands, one per line. End with CNTL/Z.
SW1(config)#
SW1(config)#interface lo1000
SW1(config-if)#ip address 1.1.1.1 255.255.255.255
SW1(config-if)#description pyats_configured
SW1(config-if)#no shutdown
SW1(config-if)#do wr mem
Building configuration...
Compressed configuration from 1392 bytes to 928 bytes[OK]
SW1(config-if)#
SW1(config-if)#end
SW1#
2024-08-08T03:17:52: %AETEST-ERROR: Caught an exception while executing section apply_config_multi:
2024-08-08T03:17:52: %AETEST-ERROR: Traceback (most recent call last):
2024-08-08T03:17:52: %AETEST-ERROR: File "/root/pyats/Tests/config_check/apply_config.py", line 81, in apply_config_multi
2024-08-08T03:17:52: %AETEST-ERROR: dialog = output.dialog
2024-08-08T03:17:52: %AETEST-ERROR: AttributeError: 'str' object has no attribute 'dialog'
ā09-04-2024 01:41 PM
Hi @yazeed-fataar,
The error you're encountering is because self.device.configure(config_commands) returns a string (command output), not an object with a dialog attribute. This is why you're getting the 'str' object has no attribute 'dialog' error when you try to access output.dialog.
To simply capture the output from the configure command, you don't need to access any dialog attribute. Instead, you can directly append the output to your console_output, e.g.
@aetest.test
def apply_config_multi(self):
config_commands = '''
interface lo1000
ip address 1.1.1.1 255.255.255.255
description pyats_configured
no shutdown
do wr mem
'''
# Send the configuration commands to the device
output = self.device.configure(config_commands)
# Capture the output and add it to your console output
common_setup.console_output += output
# Optionally, print or log the output for debugging
self.logger.info(output)
# Optionally, include assertions to verify that the configuration was applied correctly
assert "Building configuration..." in output, "Configuration was not saved successfully."
Explanation:
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide