Today we will learn how to autamate Arista switches by using AEM Automation Scripting feature. Many network engineers are focusing to use API in order to interact their devices from outside. However, you can use another feature of Arista that is Event-Handler and you can run any script from bash when any condition occurs on device.
Remember that Arista is not a machine which is running on Unix/Linux, it is itself Linux. As soon as you login to bash, you are free as you are in Linux machine. I will show small scripting from bash when any condition occur in device with below scenario by using AEM Automation Scripting
Scenario: BGP Prefix Advertisement Change and Adding Default Route while One of Interfaces Down/Up
There is BGP connection between Arista1 and Arista 2 over two links. Consider that ETH4 of Arista2 device is idle and traffic is passing only over ETH3 of Arista2. Arista2 advertises 192.168.1.0/24 subnet towards ETH3 of Arista2. There is a route-map that denies advertisement towards ETH4 of Arista2. Lastly 172.16.1.0/24 is prefered over ETH3 from Arista2. Thus, ETH3 is used and ETH4 link is idle as we want.
Our purpose here is add a static default route and starting advertisement of 192.168.1.0/24 towards EHT4 which was idle when any port flap status on ETH3. I chose using port up/down status condition for my event-handler because of virtual environment. However, you can measure bandwith of EHT3 and you can trigger the script as soon as it reach the threshould. Additionally, imagine that you can receive full routes or many routes in BGP and it will take some time to convert new link to insert FIB table. So you can add static default route in order to send traffic immediately to decrease traffic loss during outage. This post is simple script and it is up to your requirement in real worlds. You can send mail or anthing else for trigger action.
Event-Handler Config Sample
event-handler BGP
trigger on-intf Ethernet3 operstatus————>This is trigger condition
action bash python /mnt/flash/bgp.py———>This action that run script
Above config shows that any up/down status on ETH3 will be trigger to action of our bgp.py file script that is below. This script is doing that add static default route and remove deny rule from route_map of ETH4 to advertise any routes during outage.
bgp.py file:
import re
import os
import subprocess
show=subprocess.check_output('FastCli -c "show interfaces ethernet3 status"',shell=True) #to use cli command from bash for show,non-interactively commands
status=re.search('connected',show)
if status: #when EHT3 is up
komut="'configure\nroute-map BGP_2 deny 5\nend'"
komut2="'configure\nno ip route 0.0.0.0/0 40.40.40.2\nend'"
komut="printf "+ komut+ '| FastCli -p15' #use cli from bash for non-interactively commands
komut2="printf "+ komut2+ ' | FastCli -p15'
output=subprocess.check_output(komut,shell=True)
output2=subprocess.check_output(komut2,shell=True)
else: #when EHT3 is down
komut="'configure\nno route-map BGP_2 deny 5\nend'"
komut2="'configure\nip route 0.0.0.0/0 40.40.40.2\nend'"
komut="printf "+ komut+ '| FastCli -p15'
komut2="printf "+ komut2+ ' | FastCli -p15'
output=subprocess.check_output(komut,shell=True)
output2=subprocess.check_output(komut2,shell=True)
When ETH3 is UP
arista2#show interfaces ethernet 3 status
Port Name Status Vlan Duplex Speed Type Flags Encapsulation
Et3 connected routed full unconf EbraTestPhyPort
arista2#show interfaces ethernet 3 status
Port Name Status Vlan Duplex Speed Type Flags Encapsulation
Et3 connected routed full unconf EbraTestPhyPort
arista2#show ip route
B E 1.1.1.1/32 [200/0] via 10.10.10.1, Ethernet1
B E 2.2.2.2/32 [200/0] via 20.20.20.1, Ethernet2
C 3.3.3.3/32 is directly connected, Loopback0
B E 4.4.4.4/32 [200/0] via 30.30.30.2, Ethernet3
C 10.10.10.0/30 is directly connected, Ethernet1
B E 11.11.11.11/32 [200/0] via 10.10.10.1, Ethernet1
C 20.20.20.0/30 is directly connected, Ethernet2
B E 22.22.22.22/32 [200/0] via 20.20.20.1, Ethernet2
C 30.30.30.0/30 is directly connected, Ethernet3
C 40.40.40.0/30 is directly connected, Ethernet4
B E 172.16.1.0/24 [200/0] via 30.30.30.2, Ethernet3
B E 192.168.1.0/24 [200/0] via 20.20.20.1, Ethernet2
arista2#show route-map BGP_2
route-map BGP_2 deny 5---->Will be removed by SCRIPT when ETH3 becomes down
Description:
Match clauses:
SubRouteMap:
Set clauses:
route-map BGP_2 permit 10
Description:
Match clauses:
SubRouteMap:
Set clauses:
When ETH3 is DOWN:
arista2#show interfaces ethernet 3 status
Port Name Status Vlan Duplex Speed Type Flags Encapsulation
Et3 disabled routed full unconf EbraTestPhyPort
arista2#show ip route
Gateway of last resort:
S 0.0.0.0/0 [1/0] via 40.40.40.2, Ethernet4----Inserted by SCRIPT
B E 1.1.1.1/32 [200/0] via 10.10.10.1, Ethernet1
B E 2.2.2.2/32 [200/0] via 20.20.20.1, Ethernet2
C 3.3.3.3/32 is directly connected, Loopback0
B E 4.4.4.4/32 [200/0] via 40.40.40.2, Ethernet4
C 10.10.10.0/30 is directly connected, Ethernet1
B E 11.11.11.11/32 [200/0] via 10.10.10.1, Ethernet1
C 20.20.20.0/30 is directly connected, Ethernet2
B E 22.22.22.22/32 [200/0] via 20.20.20.1, Ethernet2
B E 30.30.30.0/30 [200/0] via 40.40.40.2, Ethernet4
C 40.40.40.0/30 is directly connected, Ethernet4
B E 172.16.1.0/24 [200/0] via 40.40.40.2, Ethernet4
B E 192.168.1.0/24 [200/0] via 20.20.20.1, Ethernet2
arista2#show route-map BGP_2
route-map BGP_2 permit 10----->Advertise all prefix towards ETH4 by SCRIPT when ETH3 is down
Description:
Match clauses:
SubRouteMap:
Set clauses: