Run Python Scripts from Cisco Memorandum_EEM

A scary script that revives like a zombie no matter how many times you shut down

See Cisco Memorandum _ config input with Python for basic settings to run Python scripts in IOS-XE.

Script placement path

Path: / flash / ファイル:eem_script.py Enter guestshell to create or transfer files

isp-sw#guestshell
[guestshell@guestshell ~]$ 
[guestshell@guestshell ~]$ cd /flash/
[guestshell@guestshell flash]$ vi eem_script.py 

Script behavior

The content that the IF of Loopback55 is no shut and the IF status is displayed with sh ip int brief. This time, the trigger for script execution is specified in the EEM applet.

eem_script.py


import sys
from cli import cli,clip,configure,configurep, execute, executep
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')

How to start the script manually

You can also run the script manually in EXEC mode as well as from the EEM applet

isp-sw#guestshell run python /flash/eem_script.py loop55
This script is going to unshut interface loop55 and then print show ip interface brief
Line 1 SUCCESS: int loop55
Line 2 SUCCESS: no shut 
Line 3 SUCCESS: end
Interface              IP-Address      OK? Method Status                Protocol
Vlan1                  unassigned      YES NVRAM  administratively down down    
---abridgement---
Loopback13             10.13.13.13     YES manual up                    up      
Loopback55             10.55.55.55     YES TFTP   up                    up      

EEM applet creation

In setting mode Contents to execute the script to adminUP with the syslog of admindown as a trigger

isp-sw(config)#event manager applet intshut
isp-sw(config-applet)# event syslog pattern "Interface Loopback55, changed state to administratively down"
isp-sw(config-applet)# action 0.0 cli command "en"
isp-sw(config-applet)# action 1.0 cli command "guestshell run python /flash/eem_script.py loop55"
isp-sw(config-applet)#end

Specify the script path on config

In setting mode Specify the placement path

isp-sw(config)#event manager directory user policy "flash:/"

EEM startup status check

In EXEC mode Displayed if registered normally

isp-sw#sh event manager policy registered 
No.  Class     Type    Event Type          Trap  Time Registered           Name
1    applet    user    syslog              Off   Thu Mar 28 16:37:52 2020  intshut
 pattern {Interface Loopback55, changed state to administratively down}
 maxrun 20.000
 action 0.0 cli command "en"
 action 1.0 cli command "guestshell run python /flash/eem_script.py loop55"

Try to shut down manually

Enter the shutdown command in the setting mode, output the log to the terminal with ter mon, and check the operation. It is automatically UP after shutdown from the log.

isp-sw(config-if)#shutdown 
isp-sw(config-if)#
Mar 28 16:56:22.428 JST: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback55, changed state to down
Mar 28 16:56:22.428 JST: %LINK-5-CHANGED: Interface Loopback55, changed state to administratively down
isp-sw(config-if)#
Mar 28 16:56:29.880 JST: %LINEPROTO-5-UPDOWN: Line protocol on Interface Loopback55, changed state to up
isp-sw(config-if)#
Mar 28 16:56:29.880 JST: %LINK-3-UPDOWN: Interface Loopback55, changed state to up
isp-sw(config-if)#

Check the history of events

In EXEC mode It is running normally by EEM with success

isp-sw#sh event manager history events 
No.  Job Id Proc Status   Time of Event            Event Type        Name
1    1      Actv success  Thu Mar28 16:36:19 2020  syslog            applet: intshut 
2    2      Actv success  Thu Mar28 16:43:43 2020  syslog            applet: intshut 
3    3      Actv success  Thu Mar28 16:56:22 2020  syslog            applet: intshut 

Confirmed model and version

Model: Catalyst 3850 Ver:Cisco IOS XE 16.06.05 Everest


Reference link

Programmability Configuration Guide, Cisco IOS XE Everest 16.6.x

Recommended Posts

Run Python Scripts from Cisco Memorandum_EEM
Run Python scripts synchronously from C #
Run python from excel
Run Aprili from Python with Orange
Python error detection run from Powershell
Run Ansible from Python using API
Check installed modules from Python scripts
Run Cloud Dataflow (Python) from App Engine
sql from python
MeCab from Python
Run a Python script from a C # GUI application
Type Python scripts to run in QGIS Processing
Run a python script from excel (using xlwings)
Notes on oct2py calling Octave scripts from Python
Use thingsspeak from python
Touch MySQL from Python 3
Run Python from Excel VBA with xlwings & tutorial supplement
Run Python with VBA
Operate Filemaker from Python
Use fluentd from python
Run prepDE.py with python3
Changes from Python 2 to Python 3.0
Python from or import
Use MySQL from Python
Install python from source
Execute command from Python
Run Blender with python
Operate neutron from Python!
Use MySQL from Python
Import classes in jar files directly from Python scripts
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Run BigQuery from Lambda
Execute command from python
Cloud Run tutorial (python)
Quickly profile Python scripts
Use mecab-ipadic-neologd from python
Run iperf with python
How to run a Python program from within a shell script
Deep Python learned from DEAP
Run Openpose on Python (Windows)
Cisco Memorandum _ Python config input
Run python with PyCharm (Windows)
Grammar features added from Python3.6
Cheating from PHP to Python
Run Python with CloudFlash (arm926ej-s)
Run automatic jobs in python
Dynamically import scripts in Python
Let's run Excel with Python
Run shell commands in python
Run Python unittests in parallel
OCR from PDF in Python
Use MySQL from Anaconda (python)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Study from Python Hour4: Object-oriented ②
Query Athena from Lambda Python
Access Oracle DB from Python
Start / stop GCE from python
Run Python CGI on CORESERVER