Network OS Administration Guide

Supporting Network OS 6.0.1a

Part Number: 53-1003768-04

Working interactively in the Python shell

Use this procedure to access a Python shell, within which you can use Python commands that call and manipulate Network OS commands.

NOTE
The Python shell is accessible only to admin-role users.

Python syntax is case-sensitive.

  1. In privileged EXEC mode, enter python to access the Python shell.
    device# python
    The device# prompt changes to a Python prompt:
    Python 3.3.2 (default, Jun 5 2013, 11:04:06)
    [GCC 4.3.2] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  2. To exit the Python shell and return to the Network OS prompt, enter either:
    • exit()
    • Ctrl-D
  3. To run a Network OS command from within the Python shell, enter the CLI() command.
    >>> cmd_show_running_vlan = CLI('show running-config interface vlan')
    !Command: show running-config interface vlan
    !Time: Tue Jan  6 00:12:37 2015
    interface Vlan 1
    >>>
    The statement entered above does two things:
    • Runs the show running-config interface vlan command and displays the result.
    • Assigns that command to a Python variable named cmd_show_running_vlan
  4. To run a series of Network OS commands from within the Python shell, separate the commands with \n.
    >>> cmd_config_vlans = CLI('configure \n interface vlan 101-103 \n exit')
    !Command: configure \n interface vlan 101-103 \n exit
    !Time: Tue Jan  6 00:24:17 2015
    >>>
    NOTE
    There is a difference between running a sequence of Network OS CLI commands in the Python shell rather than in the standard Network 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.

In the following example, the lines beginning with # are added for explanation.

device# python
Python 3.3.2 (default, Jun  5 2013, 11:04:06) 
[GCC 4.3.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cmd_show_running_vlan = CLI('show running-config interface vlan')
# The Network OS show running-config int vlan command is run,
# and that command is assigned to the Python variable cmd_show_running_vlan.
!Command: show running-config interface vlan
!Time: Tue Jan  6 00:12:37 2015
interface Vlan 1
!
>>> cmd_config_vlans = CLI('configure \n interface vlan 101-103 \n exit')
# A series of three commands are run and assigned to the Python variable cmd_config_vlans.
!Command: configure \n interface vlan 101-103 \n exit
!Time: Tue Jan  6 00:24:17 2015

>>> cmd_show_running_vlan.rerun()
# The rerun() function appended to cmd_show_running_vlan gives the following output:
!Command: show running-config interface vlan
!Time: Tue Jan  6 00:24:34 2015
interface Vlan 1
!
interface Vlan 101
!
interface Vlan 102
!
interface Vlan 103
!