Cisco Content Hub - CLI Python Module
Cisco Content Hub - 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.
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.
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 1/7
1/5/24, 2:24 PM Cisco Content Hub - CLI Python Module
The ip http server command must be configured for the import cli in Python to work.
Note
The following sample Python script uses different CLI functions to configure and print show commands:
import sys
import cli
intf= sys.argv[1:]
intf = ''.join(intf[0])
print "\n\n *** Printing show cmd with 'executep' function *** \n\n"
cli.executep('show ip interface brief')
print "\n\n *** Printing show cmd with 'execute' function *** \n\n"
output= cli.execute('show run interface %s' %intf)
print (output)
print "\n\n *** Printing show cmd with 'clip' function *** \n\n"
cli.clip('show run interface %s' %intf)
To run a Python script from the Guest Shell, execute the guestshell r
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 2/7
1/5/24, 2:24 PM Cisco Content Hub - CLI Python Module
The following example shows how to run a Python script from the Guest Shell:
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 3/7
1/5/24, 2:24 PM Cisco Content Hub - CLI Python Module
down
Loopback55 10.55.55.55 YES TFTP up
up
Loopback66 unassigned YES manual up
up
Building configuration...
Current configuration : 93 bytes
!
interface Loopback55
ip address 10.55.55.55 255.255.255.0
logging event link-status
end
Building configuration...
Current configuration : 93 bytes
!
interface Loopback55
ip address 10.55.55.55 255.255.255.0
logging event link-status
end
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 4/7
1/5/24, 2:24 PM Cisco Content Hub - CLI Python Module
Python Version 2.7.5 All supported platforms except for Cisco Catalyst 3650 Series
Switches and Cisco Catalyst 3850 Series Switches.
Python Version 3.6 Supported in Cisco IOS XE Amsterdam 17.1.1 and later
releases.
In Cisco IOS XE Amsterdam 17.1.1 and Cisco IOS XE
Amsterdam 17.2.1, Python V2 is the default. However, in
Cisco IOS XE Amsterdam 17.3.1 and later releases, Python
V3 is the default.
Cisco Catalyst 9200 Series Switches do not support Python Version 3.6 in Cisco IOS XE
Note
Amsterdam 17.1.1 and Cisco IOS XE Amsterdam 17.2.1. Cisco Catalyst 9200 Series
Switches support Python V3 in Cisco IOS XE Amsterdam 17.3.1 and later releases.
Note Not supported by Cisco Catalyst 3650 Series Switches and Cisco Catalyst 3850 Series
Switches.
Platforms with CentOS 7 support the installation of Redhat Package Manager (RPM) from the open source
repository.
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
The guestshell run python2 command enables Python Version 2.
Note The guestshell run python3 command enables Python Version 3.
The guestshell run python command enables Python Version 2.
Technical Assistance
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 5/7
1/5/24, 2:24 PM Cisco Content Hub - CLI Python Module
Description Link
The Cisco Support website provides extensive online resources, including http://www.cisco.com/support
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 Cisco.com
user ID and password.
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 6/7
1/5/24, 2:24 PM Cisco Content Hub - CLI Python Module
https://content.cisco.com/chapter.sjs?uri=/searchable/chapter/content/en/us/td/docs/ios-xml/ios/prog/configuration/1610/b_1610_pr… 7/7