This CLI module contains three methods that can execute CLI commands. To use these methods, you must import them using the following command:
from cli import *
The arguments for these methods are strings of CLI commands. Commands are separated with " ; " as shown in the examples. The semicolon must be surrounded by single spaces.
When two or more commands are run individually, the state is not persistent from one command to subsequent commands.
In the following example, the second command fails because the state from the first command does not persist for the second command:
cli("conf t")
cli("interface eth4/1")
When two or more commands are run together, the state is persistent from one command to subsequent commands.
In the following example, the second command is successful because the state persists for the second and third commands:
cli("conf t ; interface eth4/1 ; shut")
cli
Use the cli() command to return the raw output of CLI commands, including control/special characters.
Note: The interactive Python interpreter prints control/special characters 'escaped'. A carriage return is printed as '\n' and gives results that might be difficult to read. For more readable results, use the "clip" method.
Executes CLI commands. Takes CLI command string and returns show command output in a plain string form.
Arguments:
- cmd: Single CLI command or a batch of CLI commands. Delimiter for multiple CLI commands is a space followed by a semicolon followed by a space. Configuration commands must be in a fully qualified form.
Returns:
- string: CLI output string for show commands or an empty string for configuration commands.
Raises:
- cli_syntax_error: CLI command is not a valid NX-OS command.
- cmd_exec_error: Execution of CLI command is not successful.
clip
Prints the output of the CLI command directly to stdout and returns nothing to Python.
Arguments:
- cmd: Single CLI command or a batch of CLI commands. Delimiter for multiple CLI commands is a space followed by a semicolon followed by a space. Configuration commands must be in a fully qualified form.
Returns:
- string: Printed CLI output for show commands.
Raises:
- cli_syntax_error: CLI command is not a valid NX-OS command.
- cmd_exec_error: Execution of CLI command is not successful.
clid
For CLI commands that support XML, the clid() method returns JSON output.
An exception is thrown when XML is not used. Executes CLI commands. Takes CLI command string and returns show command output in a JSON form.
Note: The "clid" API can be useful when searching the output of show commands using JSON tools as shown in the example.
Arguments:
- cmd: Single CLI command or a batch of CLI commands. Delimiter for multiple CLI commands is a space followed by a semicolon. Configuration commands must be in a fully qualified form.
Returns:
- string: JSON-formatted output of show commands.
Raises:
- cli_syntax_error: CLI command is not a valid NX-OS command.
- cmd_exec_error: Execution of CLI command is not successful.