my cheat sheet on cisco guestshell python
1 Guestshell with python 2.7
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 management of Cisco devices. Montavista CGE7 platforms support Python Version 2.7.11, and platforms with CentOS 7 support Python Version 2.7.5.
2 Cisco cli module (python)
The guestshell includes python also has the Cisco CLI module and the EEM module are pre-installed on the devices. When you update the Python version by using either Yum or prepackaged binaries, the Cisco-provided CLI module must also be updated.
3 Updating python to 3
Python 2.7 will stay, you just add python 3 after which you can run
python 2.7 by using guestshell run python or guestshell run python2
and python3 by using guestshell run python3.
4 script stored on flash
Device# more flash:sample_script.py
import sys
import cli
intf= sys.argv[1:]
intf = ''.join(intf[0])
print "\n\n *** Configuring interface %s with 'configurep' function *** \n\n"
%intf cli.configurep(["interface loopback55","ip address 10.55.55.55 255.255.255.0","no shut","end"])
print "\n\n *** Configuring interface %s with 'configure' function *** \n\n"
cmd='interface %s,logging event link-status ,end' % intf cli.configure(cmd.split(','))
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 *** Configuring interface %s with 'cli' function *** \n\n" cli.cli('config terminal; interface %s; spanning-tree portfast edge default' %intf)
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, run:
guestshell: run python /flash/script.py loopback55
at the device prompt.
5 EEM with python
EEM policies support python scripts. So, an event on the device can trigger a python script to collect data, or change routing, or bounce and interface,or other. You just "register a python script as an EEM policy", and use the same syntax as an EEM TCL policy.
Example:
1. enable 2. configure terminal 3. event manager directory user policy path 4. event manager policy policy-filename 5. exit 6. show event manager policy registered 7. show event manager history events
Configured EEM policies run within the guest shell.
The Python package includes the following application programming interfaces (APIs):
- Action APIs — Perform EEM actions and have default parameters.
- CLI-execution APIs — Run IOS commands, and return the output. The following are
the list of CLI-execution APIs:
eem_cli_open()eem_cli_exec()eem_cli_read()eem_cli_read_line()eem_cli_run()eem_cli_run_interactive()eem_cli_read_pattern()eem_cli_write()eem_cli_close()
5.1 Environment variables-accessing APIs
Get the list of built-in or user-defined variables. The following are the environment variables-accessing APIs:
eem_event_reqinfo ()- Returns the built-in variables list.eem_user_variables()- Returns the current value of an argument.
6 Example output of show event manager policy registered
The following is sample output from the show event manager policy registered command:
Device# show event manager policy registered
No. Class Type Event Type Trap Time Registered Name
1 script user multiple Off Tue Aug 2 22:12:15 2016 multi_1.py
1: syslog: pattern {COUNTER}
2: none: policyname {multi_1.py} sync {yes} trigger delay 10.000
correlate event 1 or event 2
attribute tag 1 occurs 1
nice 0 queue-priority normal maxrun 100.000 scheduler rp_primary Secu none
2 script user multiple Off Tue Aug 2 22:12:20 2016 multi_2.py 1: syslog: pattern {COUNTER}
2: none: policyname {multi_2.py} sync {yes}
trigger
correlate event 1 or event 2
nice 0 queue-priority normal maxrun 100.000 scheduler rp_primary Secu none
3 script user multiple Off Tue Aug 2 22:13:31 2016 multi.tcl
Data Models Configuration Guide
37
This following is sample output from the guestshell run python command.
EEM Python Module
Running Python Scripts as Part of EEM Applet Actions
1: syslog: pattern {COUNTER}
2: none: policyname {multi.tcl} sync {yes} trigger
correlate event 1 or event 2
attribute tag 1 occurs 1
nice 0 queue-priority normal maxrun 100.000 scheduler rp_primary Secu none
7 example python script
import sys from cli import cli,clip,execute,executep,configure,configurep intf= sys.argv[1:] intf = ''.join(intf[0]) print ('This script is going to unshut interface %s and then print show ip interface brief'%intf) if intf == 'loopback55': configurep(["interface loopback55","no shutdown","end"]) else : cmd='int %s,no shut ,end' % intf configurep(cmd.split(',')) executep('show ip interface brief')