The following tools are leveraged to achieve the stated requirements:
- Embedded Event Manager (EEM)
- To track the port up/down events
- Python
- To execute a script based on an EEM argument to take the host port up or down.
The first stage is to create the EEM script to track the uplink port states and pass the argument based on state UP or DOWN:
event manager applet link_reboot
event syslog pattern "Module 1 current-status is"
action 1 syslog priority critical msg Running linkchange6 down
action 2 cli source linkchange6.py down
event manager applet link_up
event track 30 state up
action 1 syslog priority critical msg Running linkchange6 up
action 2 cli source linkchange6.py up
!
track 1 interface Ethernet2/1 line-protocol
track 2 interface Ethernet2/2 line-protocol
track 3 interface Ethernet2/3 line-protocol
track 4 interface Ethernet2/4 line-protocol
track 5 interface Ethernet2/1/1 line-protocol
track 6 interface Ethernet2/1/2 line-protocol
track 7 interface Ethernet2/1/3 line-protocol
track 8 interface Ethernet2/1/4 line-protocol
track 9 interface Ethernet2/2/1 line-protocol
track 10 interface Ethernet2/2/2 line-protocol
track 11 interface Ethernet2/2/3 line-protocol
track 12 interface Ethernet2/2/4 line-protocol
track 13 interface Ethernet2/3/1 line-protocol
track 14 interface Ethernet2/3/2 line-protocol
track 15 interface Ethernet2/3/3 line-protocol
track 16 interface Ethernet2/3/4 line-protocol
track 17 interface Ethernet2/4/1 line-protocol
track 18 interface Ethernet2/4/2 line-protocol
track 19 interface Ethernet2/4/3 line-protocol
track 20 interface Ethernet2/4/4 line-protocol
track 30 list boolean or
object 1
object 2
object 3
object 4
object 5
object 6
object 7
object 8
object 9
object 10
object 11
object 12
object 13
object 14
object 15
object 16
object 17
object 18
object 19
object 20
The second phase is to create the Python script that takes the argument from the EEM script and does the following:
ARGV = Down
Check the host ports and shut them down and place on a flat file
ARGV = UP
Read flat file and bring the host ports back online
#!/usr/bin/python
import sys
import os
import re
from cisco import cli
hostlistfile = "hostlist"
print cli('pwd')
try:
sys.argv[1]
except IndexError:
print "Error: Missing argument, need either 'up' or 'down'"
exit()
if sys.argv[1] == "up":
# At this point we should have a file containing ports to bring up.
dirresult = cli("dir hostlist")
if dirresult == "No such file or directory" :
print "No hostlist found, exiting"
exit()
file = cli("show file hostlist")
for match in file[0:-1].split('\n'):
if match[0:8] == "Ethernet":
cli("config terminal ; interface %s ; no shutdown" % match)
cli ("delete hostlist no-prompt")
exit()
if sys.argv[1] == "down":
# Uplinks are not yet up, lets see which hosts are active, bring those
# down and save to a file
print "Generating host list dynamically."
result = cli("show interface")
for rline in result[0:-1].split('\n'):
match = re.match(r'^(Ethernet[\S]+).*', rline)
if match:
#Check name against Ethenert2/ we will skip these uplinks.
match2 = re.match(r'Ethernet2/.*', match.group(1))
if match2:
continue
# If we made it this far, check state then adjust
match3 = re.match(r'Ethernet1/.*', match.group(1))
if match3:
match4 = re.match(r'.*(Administratively down|SFP not inserted).*', rline)
if match4 == None:
print match.group(1)
cli("echo '%s '>> hostlist" %match.group(1))
#Change state
cli("config t ; interface %s ; shutdown" % match.group(1))