Goal

This tutorial shows you how to open VTY on the network element, how to write a command, read the response, and understand any error codes.

Tutorial Code

The code used in this tutorial is available in the VTYTutorial.py file located under <SDK Location>/python/tutorials/src/vty/VTYTutorial.py.

Requirements/Prerequisites

  • These steps assume the application can connect properly to a network element. Please see the Connecting to a Network Element tutorial for information on how to make the initial connection.
  • To configure the network element to run the VTY tutorial, you must first enable the VTY Service Set:
router(config)# onep
router(config-onep)# service set vty

Steps In Detail

Create VTY Object

First, create the VtyService object.

vtyService = VtyService(tutorial.get_network_element()) 

Open VTY

Open VTY on the network element.

vtyService.open()

Get VTY Timeout

Get a timeout of the VTY on the network element.

logger.info("VTY Time Out - %s", vtyService.timeout)

Write to VTY

Write a string to the VTY on the network element.

TEST_CMD1 = "show onep status";
cli_result = vtyService.write(TEST_CMD1)
logger.info("Test Command : %s", TEST_CMD1)
logger.info("CLI Result for Test Command : %s", cli_result)

Get Parser State

Get the parser state and its attributes on the network element.

parser_state = vtyService.get_parser_state()
logger.info("ParserState prompt - %s", parser_state.prompt)
logger.info("ParserState overallrc - %s", parser_state.overallRC)
cmd_results = parser_state.results
for cmd_result in cmd_results:
    logger.info("ParserState::cmdresult:inputline - %s", cmd_result.input_line)
    logger.info("ParserState::cmdresult:parsereturncode - %s", cmd_result.parse_return_code)
    logger.info("ParserState::cmdresult:errorlocation - %s", cmd_result.error_location)

Get VTY State

Get the VTY state on the network element.

logger.info("State - %s", vtyService.state)

Set Max Response Length

Set the maximum response length from the network element in bytes. Responses will be truncated to this length. Zero indicates no limit.

MAX_RESPONSE_LENGTH = 110
vtyService.max_response = MAX_RESPONSE_LENGTH
logger.info("MaxResponse - %s", vtyService.max_response)

Get Max Response Length

Get the maximum response length from the network element in bytes. Responses will be truncated to this length. Zero indicates no limit.

logger.info("MaxResponse - %s", vtyService.max_response)

Cancel Command Execution

Cancel the command execution on the network element.

vtyService.cancel()

Close VTY

Close the VTY connection on the network element.

vtyService.close()

Destroy VTY

Destroy the VtyService. This method provides a way for the application to explicitly clean up the VTY resource so that the IDs of the VTY in the network element are returned to the pool when the application has finished using them. Note that this method can only be called after the VTY is closed by the close() method.

vtyService.destroy()

Result

Congratulations! You are now able to use your onePK application to create, open, cancel, close VTY and also run commands and check the response.