See Cisco Memorandum _ config input with Python for basic settings to run Python scripts in IOS-XE.
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
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')
You can also run the script manually in EXEC mode as well as from the EEM applet
sh ip int brief
command in the script is executed from EXEC mode, it is not executed by 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
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
In setting mode Specify the placement path
isp-sw(config)#event manager directory user policy "flash:/"
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"
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)#
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
Model: Catalyst 3850 Ver:Cisco IOS XE 16.06.05 Everest
Programmability Configuration Guide, Cisco IOS XE Everest 16.6.x
Recommended Posts