The Python programming language uses three APIs that can execute CLI commands. The APIs are available from the Python CLI module.
You must enable the APIs with the from cli import * command. The arguments for these APIs are strings of CLI commands. To execute a CLI command through the Python interpreter, you enter the CLI command as an argument string of one of the following APIs:
1. cli() returns the raw output of CLI commands, including control or special characters. The interactive Python interpreter prints control or special characters “escaped.” A carriage return is printed as ‘\n’ and gives results that can be difficult to read. The clip() API gives results that are more readable.
Example: string = cli (“cli-command”)
2. clid() returns JSON output for the CLI command, if XML support exists for the command; otherwise, an exception is thrown. This API can be useful when searching the output of show commands.
Example: json_string = clid (“cli-command”)
3. clip() prints the output of the CLI command directly to stdout and returns nothing to Python.
Example: clip (“cli-command”)
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”)
Note
Commands are separated with “ ; ” as shown in the example. The semicolon ( ; ) must be surrounded with single blank characters.
Python in Interactive Mode
To enter the Python shell, enter the python command from the NX-OS command line with no parameters. You can enter lines of Python code to execute a block of code. A colon (:) at the end of a line tells the Python interpreter that the subsequent lines will form a code block. After the colon, you must indent the subsequent lines in the block, following Python indentation rules. After you have typed the block, press Return or Enter twice to execute the code.
Note
The Python interpreter is designated with the >>> or … prompt.
Example 17-4 shows how to invoke Python from the CLI and run Python commands interactively.
Example 17-4 Interactive Python Example
switch# python
Python 2.7.5 (default, Feb 8 2019, 23:59:43)
[GCC 4.6.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from cli import *
>>> import json
>>> cli(‘configure terminal ; interface loopback 5 ; no shut’)
‘’
>>> intflist=json.loads(clid(‘show interface brief’))
>>> i=0
>>> while i < len(intflist[‘TABLE_interface’][‘ROW_interface’]):
… intf=intflist[‘TABLE_interface’][‘ROW_interface’][i]
… i=i+1
… if intf[‘state’] == ‘up’:
… print intf[‘interface’]
…
mgmt0
Ethernet2/7
Ethernet4/7
loopback0
loopback5
>>>
The preceding example brings the loopback 5 interface UP and shows how to query the interfaces running on the switch.
To exit the Python shell, type exit():
>>>exit()
switch#
Leave a Reply