cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
577
Views
2
Helpful
5
Replies

pyats - store stdout from configure

yazeed-fataar
Level 1
Level 1

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

class Apply_Config(aetest.Testcase):
"""Apply Config Checks"""
@aetest.test
def setup(self, testbed, device_name)
""" Testcase Setup section """
# connect to device
self.device = testbed.devices[device_name]

@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
'''
common_setup.console_output += self.device.configure(config_commands)

 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.

 

2024-08-07 00:02:53,346: %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 1396 bytes to 925 bytes[OK]
SW1(config-if)#        
SW1(config-if)#end
SW1#

 

5 Replies 5

Not sure this will give you the 100% results you want, try the output attribute 

output = self.device.configure(config_commands)
console_output += output

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Hi @bigevilbeard 

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]

 

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().

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Hi @bigevilbeard 

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?

 

@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
'''
output = self.device.configure(config_commands)
dialog = output.dialog
common_setup.console_output += output

 

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'

Alexander Stevenson
Cisco Employee
Cisco Employee

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:

  • self.device.configure(config_commands): This sends the configuration commands to the device and returns the output as a string.
  • common_setup.console_output += output: This appends the command output to your console_output.
  • self.logger.info(output): This logs the output for debugging purposes.
  • assert "Building configuration..." in output: This is an optional assertion to check that the configuration was saved successfully.