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.
The code used in this tutorial is available in the VTYTutorial.py file located under <SDK Location>/python/tutorials/src/vty/VTYTutorial.py.
router(config)# onep router(config-onep)# service set vty
First, create the VtyService object.
vtyService = VtyService(tutorial.get_network_element())
Get a timeout of the VTY on the network element.
logger.info("VTY Time Out - %s", vtyService.timeout)
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 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)
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 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)
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()
Congratulations! You are now able to use your onePK application to create, open, cancel, close VTY and also run commands and check the response.