CLI

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.

Syntax

CLI (' device-CLI-command ' [ \n ' device-CLI-command ' ] [ [ do_print = ] { True | False } ] )

Parameters

device-CLI-command
An SLX-OS CLI command. You separate additional commands with \n.
do_print =
Specify whether or not to print the output of device-CLI-command to the default device. The default is to print the output.
True
Print the output.
False
Do not print the output.

Modes

Python command shell

Usage Guidelines

Divergences between the CLI syntax and Python syntax include the following differences:
  • Although in general, the CLI syntax is not case-sensitive, our convention is to use lower-case.
  • Python syntax is case sensitive. Regarding the syntax documented in the current topic, note the following:
    • The syntax of the command is upper case (CLI) and not lower case (cli).
    • The syntax of the do_print = options is to capitalize the first letter: { True | False }

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.

Within a script or interactive session, if you assign a CLI command or series of commands to a Python variable, you can then append the following functions to the variable:
  • .rerun()—updates the variable from a new run of the CLI command or series of 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_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
    !
    !
  • .get_output()—returns the value of a new run of the CLI command or series of commands, as a list. Running this script displays the "Firmware name" line of the show version command.
    #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])

Examples

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')