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 that, 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, the result 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:

     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 show version in the documentation.

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

Here is an example of configuration commands named eapi_2.py. In our example, we wrote a function that takes the switch object and the list of commands as attributes: 

      #!/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's execution:

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

Now, do a quick check on the switch to verify the command's 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, which 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.220.139.95