In this post, I will give small sample about python scripting code for Arista devices. As you know, network engineering has been evolving to learn automation with network equipments. Many companies has started to drop CLI in devices and using automation tools to operate their devices. So this tend network engineers to learn scripting languages. Because of many aspects, Python is more suitable for network engineers.
Current project that I am involving now, we need to count all used ports in our datacenter for each month in order to give a “usage report” to our managers. This datacenter is so large and to count those ports in each month is so exhausting via CLI to each devices. So I wrote simple code to connect each devices and check up down status,speed of them and type of them. I share this post to help beginner engineers for automation and show them what they can simply do with python.
In this code, I used “pyeapi”. Pyeapi is a module that Arista has created and involve many submodules and methods for arista commands. Pyeapi provides connection to Arista switches with API interfaces. So you can send related commands, get related information from devices. REST-API is really good for getting and sending commands to Arista. For example, when you send a command ” show interfaces status” , you can get JSON output sttructure and process this data as you want for your script.
When you run below script, you will get total sum copper 1gi,10gi and fiber 1gi and 10gi connected ports number separately from 100 switches in Arista.
import pprint import pyeapi import re import traceback switches = i=101 while i<201: fourh_octet=i IP= "10.129.34."+str(fourh_octet) i = i+1 switches.append(IP) genelbakir1gi=0 genelbakir10gi=0 genelfiber1gi=0 genelfiber10gi=0 try: for i in switches: try: node = pyeapi.connect(transport="https", host=i, username="xxxxx", password="xxxxx", port=None) print i a=1 version=node.execute((["show version"])) parse=re.split(r'-',version['result']['modelName']) parsing=re.split('0',parse) print parsing copperorfiber= parsing sum1gi=0 sum10gi=0 while 0<a<49: showinterface= node.execute(["show interfaces ethernet"+' '+str(a) ]) for m in showinterface['result']['interfaces']: if showinterface['result']['interfaces'][m]['interfaceStatus']=='connected': print m speed=showinterface['result']['interfaces'][m]['bandwidth']/1000000000 print speed if speed==10: sum10gi=sum10gi+1 elif speed==1: sum1gi=sum1gi+1 else: continue a=a+1 print sum1gi print sum10gi if copperorfiber == 'TR': genelbakir1gi=genelbakir1gi+sum1gi genelbakir10gi = genelbakir10gi + sum10gi elif copperorfiber == 'SR': genelfiber1gi=genelfiber1gi+sum1gi genelfiber10gi = genelfiber10gi + sum10gi except Exception as e: print "connection problem>>" + str(e) print(traceback.format_exc()) print "toplam connnected bakir 1gi sayisi:"+' '+str(genelbakir1gi) print "toplam connnected bakir 10gi sayisi:"+' '+str(genelbakir10gi) print "toplam connnected fiber 1gi sayisi:"+' '+str(genelfiber1gi) print "toplam connnected fiber 10gi sayisi:"+' '+str(genelfiber10gi) except Exception as e: print 'Error:Any problem exists in your code >> '+str(e) print(traceback.format_exc()) OUTPUT: toplam connnected bakir 1gi sayisi: 108 toplam connnected bakir 10gi sayisi: 0 toplam connnected fiber 1gi sayisi: 0 toplam connnected fiber 10gi sayisi: 217
Remember that you can write this output a file or excel and send mail to your manager directly.