Configure Arista eAPI with Python

It is possible to manage Arista switches with the Beta Devops Extension, however this is not yet production worthy. But Arista has a much more cooler extension which is far better usable; the Arista eAPI.

With Arista eAPI you can use industry standard CLI commands to send requests, and get returned data that is structured and documented. These requests and responses using JSON-RPC, a lightweight data-interchange format that is easy to read and write and to parse and generate.

Note: eAPI is first supported in the 4.12 release.

First you need to enable the eAPI on the switch:
management api http-commands
protocol https
no shut

To verify the configuration:
show management api http-commands
HTTPS server: running, set to use port 443
HTTP server: shutdown, set to use port 80
VRF: default
Hits: 10
Last hit: 1900 seconds
Bytes in: 312
Bytes out: 34260
Requests: 2
Commands: 2
Duration: 0.558 seconds

User Sessions Hits Bytes in Bytes out Last hit
--------- ------------- ---------- -------------- --------------- -------------
q 1 10 312 34260 1923 seconds
session count limit 10 sessions
session inactivity timeout 3600 seconds

Once the eAPI service is running you should be able to log into the switch via HTTPS and use the command explorer and look at the documentation.
Just type the URL or IP address of the server into the navigation bar of your favorite browser and you are off and running.


Now you can enter commands and see what the JSON output is on the right pane, for example 'show version'.


This is nice, but it is ofcourse much more fun to do this from a script. In this case I used Python and the jsonrpclib ( In order to be more flexible I created a configuration file "interfaces.json". In this file I entered all the commands I want to use on the switch. In this case I would like to configure interface 38 and 39 and I want to create port-channel 40.


Now you have the configuration file, lets start with the Python script to read the configuration file, connect to the switch and execute the commands from the file. First you need to open the connection to the switch:

#!/usr/bin/env python
import json
from jsonrpclib import Server
# connect to the Arista switch
switch = Server( "https://username:password@" )

The following code reads the configuration file and puts it in "interface_data".

from pprint import pprint
# reading json file for import
with open('interfaces.json') as data_file:
interfaces_data = json.load(data_file)

Because you would like to configure an interface and a port-channel you will need two loops to fetch the data from "interfaces_data":

# configure interface
for record in interfaces_data['interface']:
post_cmd = [ conf_mode, "interface ethernet " + (record["eth"]), "description " + (record["desc"]), "switchport access vlan " + (record["vlan"]), "logging event " + (record["logging"]), "switchport mode " + (record["mode"]) ]
response = switch.runCmds( 1, post_cmd )

# configure port-channel
for record in interfaces_data['port-channel']:
post_cmd = [ conf_mode, "interface port-channel " + (record["port"]), "description " + (record["desc"]), "switchport access vlan " + (record["vlan"]), "logging event " + (record["logging"]), "switchport mode " + (record["mode"]) ]
response = switch.runCmds( 1, post_cmd )

Now to verify what the code did on the switch, logon to the switch and verify the interface ports.


Next thing of course is to put the code into Opscode Chef to control the switch configuration.


Not Published

0/1000 characters
Go Top