CLI Python Module
Python Programmability provides a Python module that allows users to interact with IOS using CLIs.
• Finding Feature Information, on page 1
• Information About CLI Python Module, on page 1
• Updating the Cisco CLI Python Module, on page 4
• Additional References for the CLI Python Module, on page 4
• Feature Information for the CLI Python Module, on page 5
Finding Feature Information
Your software release may not support all the features documented in this module. For the latest caveats and
feature information, see Bug Search Tool and the release notes for your platform and software release. To
find information about the features documented in this module, and to see a list of the releases in which each
feature is supported, see the feature information table at the end of this module.
Use Cisco Feature Navigator to find information about platform support and Cisco software image support.
To access Cisco Feature Navigator, go to [Link] An account on [Link] is not
required.
Information About CLI Python Module
About Python
The Cisco IOS XE devices support Python Version 2.7 in both interactive and non-interactive (script) modes
within the Guest Shell. The Python scripting capability gives programmatic access to a device's CLI to perform
various tasks and Zero Touch Provisioning or Embedded Event Manager (EEM) actions.
Python Scripts Overview
Python run in a virtualized Linux-based environment, Guest Shell. For more information, see the Guest Shell
chapter. Cisco provides a Python module that allows user’s Python scripts to run IOS CLI commands on the
host device.
CLI Python Module
1
CLI Python Module
Interactive Python Prompt
Interactive Python Prompt
When you execute the guestshell run python command on a device, the interactive Python prompt is opened
inside the Guest Shell. The Python interactive mode allows users to execute Python functions from the Cisco
Python CLI module to configure the device.
The following example shows how to enable the interactive Python prompt:
Device# guestshell run python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Device#
Python Script
Python scripts can run in non-interactive mode by providing the Python script name as an argument in the
Python command. Python scripts must be accessible from within the Guest Shell. To access Python scripts
from the Guest Shell, save the scripts in bootflash/flash that is mounted within the Guest Shell.
The following sample Python script uses different CLI functions to configure and print show commands:
Device# more flash:sample_script.py
import sys
import cli
intf= [Link][1:]
intf = ''.join(intf[0])
print "\n\n *** Configuring interface %s with 'configurep' function *** \n\n" %intf
[Link](["interface loopback55","ip address [Link] [Link]","no
shut","end"])
print "\n\n *** Configuring interface %s with 'configure' function *** \n\n"
cmd='interface %s,logging event link-status ,end' % intf
[Link]([Link](','))
print "\n\n *** Printing show cmd with 'executep' function *** \n\n"
[Link]('show ip interface brief')
print "\n\n *** Printing show cmd with 'execute' function *** \n\n"
output= [Link]('show run interface %s' %intf)
print (output)
print "\n\n *** Configuring interface %s with 'cli' function *** \n\n"
[Link]('config terminal; interface %s; spanning-tree portfast edge default' %intf)
print "\n\n *** Printing show cmd with 'clip' function *** \n\n"
[Link]('show run interface %s' %intf)
To run a Python script from the Guest Shell, execute the guestshell run python
/flash/[Link] command
at the device prompt.
The following example shows how to run a Python script from the Guest Shell:
CLI Python Module
2
CLI Python Module
Supported Python Versions
The following example shows how to run a Python script from the Guest Shell:
Device# guestshell run python /flash/sample_script.py loop55
*** Configuring interface loop55 with 'configurep' function ***
Line 1 SUCCESS: interface loopback55
Line 2 SUCCESS: ip address [Link] [Link]
Line 3 SUCCESS: no shut
Line 4 SUCCESS: end
*** Configuring interface %s with 'configure' function ***
*** Printing show cmd with 'executep' function ***
Interface IP-Address OK? Method Status Protocol
Vlan1 unassigned YES NVRAM administratively down down
GigabitEthernet0/0 [Link] YES NVRAM up up
GigabitEthernet1/0/1 unassigned YES unset down down
GigabitEthernet1/0/2 unassigned YES unset down down
GigabitEthernet1/0/3 unassigned YES unset down down
:
:
:
Te1/1/4 unassigned YES unset down down
Loopback55 [Link] YES TFTP up up
Loopback66 unassigned YES manual up up
*** Printing show cmd with 'execute' function ***
Building configuration...
Current configuration : 93 bytes
!
interface Loopback55
ip address [Link] [Link]
logging event link-status
end
*** Configuring interface %s with 'cli' function ***
*** Printing show cmd with 'clip' function ***
Building configuration...
Current configuration : 93 bytes
!
interface Loopback55
ip address [Link] [Link]
logging event link-status
end
Supported Python Versions
Guest Shell is pre-installed with Python Version 2.7. Guest Shell is a virtualized Linux-based environment,
designed to run custom Linux applications, including Python applications for automated control and
CLI Python Module
3
CLI Python Module
Updating the Cisco CLI Python Module
management of Cisco devices. Platforms with Montavista CGE7 support Python Version 2.7.11, and platforms
with CentOS 7 support Python Version 2.7.5.
The following table provides information about Python versions and the supported platforms:
Table 1: Python Version Support
Python Version Platform
Python Version 2.7.11 • Cisco Catalyst 3850 Series Switches
Platforms with CentOS 7 support the installation of Redhat Package Manager (RPM) from the open source
repository.
Updating the Cisco CLI Python Module
The Cisco CLI Python module and EEM module are pre-installed on devices. However, when you update the
Python version by using either Yum or prepackaged binaries, the Cisco-provided CLI module must also be
updated.
Note When you update to Python Version 3 on a device that already has Python Version 2, both versions of Python
exist on the device. Use one of the following IOS commands to run Python:
• The guestshell run python2 command enables Python Version 2.
• The guestshell run python3 command enables Python Version 3.
• The guestshell run python command enables Python Version 2.
Use one of the following methods to update the Python version:
• Standalone tarball installation
• PIP install for the CLI module
Additional References for the CLI Python Module
Related Documents
Related Topic Document Title
Guest Shell • Guest Shell in Catalyst 3850 Switch
EEM Python Module • Python Scripting in EEM
CLI Python Module
4
CLI Python Module
Feature Information for the CLI Python Module
Technical Assistance
Description Link
The Cisco Support website provides extensive online resources, including [Link]
documentation and tools for troubleshooting and resolving technical issues
with Cisco products and technologies.
To receive security and technical information about your products, you can
subscribe to various services, such as the Product Alert Tool (accessed from
Field Notices), the Cisco Technical Services Newsletter, and Really Simple
Syndication (RSS) Feeds.
Access to most tools on the Cisco Support website requires a [Link] user
ID and password.
Feature Information for the CLI Python Module
The following table provides release information about the feature or features described in this module. This
table lists only the software release that introduced support for a given feature in a given software release
train. Unless noted otherwise, subsequent releases of that software release train also support that feature.
Use the Cisco Feature Navigator to find information about platform support and Cisco software image support.
To access Cisco Feature Navigator, go to [Link]/go/cfn. An account on [Link] is not required.
Table 2: Feature Information for the CLI Python Module
Feature Name Release Feature Information
CLI Python Module Cisco IOS XE Everest Python programmabilty provides a Python
16.5.1a module that allows users to interact with IOS
using CLIs.
In Cisco IOS XE Everest 16.5.1a, this feature
was implemented on the following platforms:
• Cisco Catalyst 3850 Series Switches
CLI Python Module
5
CLI Python Module
Feature Information for the CLI Python Module
CLI Python Module
6