eAPI examples

We can then write a simple program called eapi_1.py to look at the response text:

      #!/usr/bin/python2

from __future__ import print_function
from jsonrpclib import Server
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

switch = Server("https://admin:[email protected]/command-api")

response = switch.runCmds( 1, [ "show version" ] )
print('Serial Number: ' + response[0]['serialNumber'])
Note since this is Python 2, in the script I used the from __future__ import print_function to make future migration easier. The ssl related lines are for Python version > 2.7.9, for more information please see https://www.python.org/dev/peps/pep-0476/.

This is the response I received from the previous runCms() method:

    [{u'memTotal': 3978148, u'internalVersion': u'4.16.6M-
3205780.4166M', u'serialNumber': u'<omitted>', u'systemMacAddress':
u'<omitted>', u'bootupTimestamp': 1465964219.71, u'memFree':
277832, u'version': u'4.16.6M', u'modelName': u'DCS-7050QX-32-F',
u'isIntlVersion': False, u'internalBuildId': u'373dbd3c-60a7-4736-
8d9e-bf5e7d207689', u'hardwareRevision': u'00.00', u'architecture':
u'i386'}]

As you can see, this is a list containing one dictionary item. If we need to grab the serial number, we can simply reference the item number and the key in line 12:

     print('Serial Number: ' + response[0]['serialNumber'])

The output will contain only the serial number:

$ python eapi_1.py
Serial Number: <omitted>

To be more familiar with the command reference, I recommend that you click on the Command Documentation link on the eAPI page, and compare your output with the output of version in the documentation.

As noted earlier, unlike REST, JSON-RPC client uses the same URL endpoint for calling the server resources. You can see from the previous example that the runCmds() method list of commands. For the execution of configuration commands, you can follow the same framework, and place the list of commands inside the second argument.

Here is an example of configuration commands called eapi_2.py:

      #!/usr/bin/python2

from __future__ import print_function
from jsonrpclib import Server
import ssl, pprint

ssl._create_default_https_context = ssl._create_unverified_context

# Run Arista commands thru eAPI
def runAristaCommands(switch_object, list_of_commands):
response = switch_object.runCmds(1, list_of_commands)
return response


switch = Server("https://admin:[email protected]/command-
api")

commands = ["enable", "configure", "interface ethernet 1/3",
"switchport acc ess vlan 100", "end", "write memory"]

response = runAristaCommands(switch, commands)
pprint.pprint(response)

Here is the output of the command execution:

$ python2 eapi_2.py
[{}, {}, {}, {}, {}, {u'messages': [u'Copy completed successfully.']}]

Now, do a quick check on the switch to verify command execution:

arista1#sh run int eth 1/3
interface Ethernet1/3
switchport access vlan 100
arista1#

Overall, eAPI is fairly straightforward and simple to use. Most programming languages have libraries similar to jsonrpclib that abstracts away JSON-RPC internals. With a few commands you can start integrating Arista EOS automation into your network.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.202