In a Python shell, runs a device CLI command or series of commands. You can also assign the output of such commands to a Python object.
('
device-CLI-command
'
[
\n
'
device-CLI-command
'
]
[
[
do_print =
]
{
True
|
False
}
]
)
Python command shell
In Python, double quotes ("
) and single quotes ('
) are equivalent.
As delimiter between multiple CLI commands, use \n.
There is a difference between running a sequence of
SLX-OS CLI commands in the Python shell rather than in the standard
SLX-OS interface. Whereas in the standard interface the result of a command is persistent, in the Python shell each
CLI( )
statement is independent of any preceding ones.
For support of the
CLI( )
command, although a Python script must include a
from CLI import CLI
statement, this statement is automatically implemented when launching the Python interpreter interactively.
device# python
Python 3.5.2 (default, Apr 11 2019, 13:05:18)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cmd_show_running_ve = CLI('show running-config interface ve')
!Command: show running-config interface ve
!Time: Mon Aug 22 16:53:13 2016
% No entries found.
# The SLX-OS show running-config interface ve command is run,
# and that command is assigned to the Python variable cmd_show_running_ve.
>>> cmd_config_ve = CLI('configure \n interface ve 101-103')
# A series of three commands are run and assigned to the Python variable cmd_config_ve.
!Command: configure
interface ve 101-103
!Time: Mon Aug 22 16:53:13 2016
>>> cmd_show_running_ve.rerun()
# The rerun() function appended to cmd_show_running_ve gives the following output:
!Command: show running-config interface ve
!Time: Mon Aug 22 16:53:13 2016
interface Ve 101
shutdown
!
interface Ve 102
shutdown
!
interface Ve 103
shutdown
!
!
#Required in all scripts for SLX: from CLI import CLI # Import the Python Regular Expressions (re) module: import re # Create Python objects: slot_firmware = {} cmd_show_ver = CLI("show ver", False) # Using .get_output(), assign the result of show ver to a Python object named output: output = cmd_show_ver.get_output() for line in output: found = re.search(r'^(Firmware name:)\s+(\S+)$', line, re.M) if found: slot_firmware[found.group(1)] = found.group(2) print("FIRMWARE:\n") for key in slot_firmware: print("\t", key, "\t=> ", slot_firmware[key])
The following example launches the Python shell and then both assigns a series of CLI configuration commands to a Python variable and runs those commands.
device# python Python 3.5.2 (default, Apr 11 2019, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> cmd_config_ve = CLI('configure \n interface ve 101-103') !Command: configure interface ve 101-103 !Time: Mon Aug 22 16:57:36 2016 >>>
The following example launches the Python shell and then both assigns a CLI operational command (reload system) to a Python variable and runs that command.
device# python Python 3.5.2 (default, Apr 11 2019, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> cmd_reload_system = CLI('reload system \n y')