Strony

Sunday, 25 November 2018

Accessing Arista device eAPI using python

This blog entry is about accessing Arista device eAPI using python.

Topics which will be covered:
- enabling eAPI and creating user for authentication on Arista device
- preparation of Docker container with python and needed library installed
- python code for accessing Arista eAPI

Prerequisites:
- Docker container environment
- Arista switch with management IP set (IP of Arista need to be accessible from Docker conatiner)
Arista vEOS with IP 192.168.42.101 was used in example.

1. User creation on Arista


Commands for user creation and setting enable password on Arista:
configure terminal
username userapi privilege 15 secret password1
enable secret test123

2. Enabling eAPI on Arista

Commands for enabling eAPI on Arista device: 
configure terminal
management api http-commands
protocol http
no shutdown


3. Files and folders


File and folder structure which need to be prepared on system where Docker image will be build.

Required file and folder structure:
[test@localhost for_docker]$ tree -f -i
.
./Alpine_python
./Alpine_python/Dockerfile

1 directory, 1 file

Content of Dockerfile:
[test@localhost Alpine_python]$ cat Dockerfile
FROM alpine:3.8

RUN apk add python3=3.6.6-r0 && \
    python3 -m ensurepip && \
    pip3 install --upgrade pip setuptools jsonrpclib-pelix==0.3.2

4. Preparing Docker container

Building docker image. Command need to be executed from within prepared folder "Alpine_python":
sudo docker build -f Dockerfile -t "alpine-python" .

When docker image is ready, docker container can be launched. At the same moment shell of the prepared container will be accessed and python interpreter will be started:
sudo docker run -it alpine-python /bin/sh -c "python3"

Example output:
[test@localhost Alpine_python]$ sudo docker run -it alpine-python /bin/sh -c "python3"
Python 3.6.6 (default, Aug 24 2018, 05:04:18)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

5. Accessing Arista eAPI using python - example 1

Python code for retrieving information about device version ('show version' command) which should be pasted to python interpreter:
from jsonrpclib import Server

#Setting variables for accessing Arista device
username = 'userapi'
password = 'password1'
device_ip_address = '192.168.42.101'

#Preparing url for accessing Arista device
#Example url:
#http://userapi:password1@192.168.42.101/command-api
url = 'http://{0}:{1}@{2}/command-api'.format(username, password, device_ip_address)

device = Server(url)

#Command which will be send to Arista device
cmd = ["show version"]

#Send prepared command to Arista device
response = device.runCmds(1, cmd)

#Print retrieved response
print(response)

Example output:
[test@localhost Alpine_python]$ sudo docker run -it alpine-python /bin/sh -c "python3"
Python 3.6.6 (default, Aug 24 2018, 05:04:18)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from jsonrpclib import Server
>>>
>>> #Setting variables for accessing Arista device
... username = 'userapi'
>>> password = 'password1'
>>> device_ip_address = '192.168.42.101'
>>>
>>> #Preparing url for accessing Arista device
... #Example url:
... #http://userapi:password1@192.168.42.101/command-api
... url = 'http://{0}:{1}@{2}/command-api'.format(username, password, device_ip_address)
>>>
>>> device = Server(url)
>>>
>>> #Command which will be send to Arista device
... cmd = ["show version"]
>>>
>>> #Send prepared command to Arista device
... response = device.runCmds(1, cmd)
>>>
>>> #Print retrieved response
... print(response)
[{'modelName': 'vEOS', 'internalVersion': '4.17.2F-3696283.4172F', 'systemMacAddress': '00:0c:29:bf:a4:cf', 'serialNumber': '', 'memTotal': 1893364, 'bootupTimestamp': 1518264267.48, 'memFree': 925664, 'version': '4.17.2F', 'architecture': 'i386', 'isIntlVersion': False, 'internalBuildId': 'c6362f13-ae6d-4c88-b5fd-4678d66018ab', 'hardwareRevision': ''}]
>>>

6. Accessing Arista eAPI using python - example 2

Python code for configuring new hostname which should be pasted to python interpreter:
from jsonrpclib import Server

#Setting variables for accessing Arista device
username = 'userapi'
password = 'password1'
enable_password = 'test123'
device_ip_address = '192.168.42.101'

#Preparing url for accessing Arista device
#Example url:
#http://userapi:password1@192.168.42.101/command-api
url = 'http://{0}:{1}@{2}/command-api'.format(username, password, device_ip_address)

device = Server(url)

#Command which will be send to Arista device to retrieve current hostname
cmd = ["show hostname"]

#Send prepared command to Arista device (retrieve configured hostname)
response = device.runCmds(1, cmd)

#Print retrieved response
print(response)

#Commands which will be send to Arista device to set new hostname
cmd = [{"cmd": "enable","input": enable_password}, "configure", "hostname NewHostname02"]

#Send prepared commands to Arista device (setting new hostname)
response = device.runCmds(1, cmd)

#Command which will be send to Arista device to retrieve configured hostname
#verification if hostname was modified
cmd = ["show hostname"]

#Send prepared command to Arista device (retrieve configured hostname)
response = device.runCmds(1, cmd)

#Print retrieved response
print(response)

Example output:
[test@localhost Alpine_python]$ sudo docker run -it alpine-python /bin/sh -c "python3"
Python 3.6.6 (default, Aug 24 2018, 05:04:18)
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from jsonrpclib import Server
>>>
>>> #Setting variables for accessing Arista device
... username = 'userapi'
>>> password = 'password1'
>>> enable_password = 'test123'
>>> device_ip_address = '192.168.42.101'
>>>
>>> #Preparing url for accessing Arista device
... #Example url:
... #http://userapi:password1@192.168.42.101/command-api
... url = 'http://{0}:{1}@{2}/command-api'.format(username, password, device_ip_address)
>>>
>>> device = Server(url)
>>>
>>> #Command which will be send to Arista device to retrieve current hostname
... cmd = ["show hostname"]
>>>
>>> #Send prepared command to Arista device (retrieve configured hostname)
... response = device.runCmds(1, cmd)
>>>
>>> #Print retrieved response
... print(response)
[{'fqdn': 'hostname01', 'hostname': 'hostname01'}]
>>>
>>> #Commands which will be send to Arista device to set new hostname
... cmd = [{"cmd": "enable","input": enable_password}, "configure", "hostname NewHostname02"]
>>>
>>> #Send prepared commands to Arista device (setting new hostname)
... response = device.runCmds(1, cmd)
>>>
>>> #Command which will be send to Arista device to retrieve configured hostname
... #verification if hostname was modified
... cmd = ["show hostname"]
>>>
>>> #Send prepared command to Arista device (retrieve configured hostname)
... response = device.runCmds(1, cmd)
>>>
>>> #Print retrieved response
... print(response)
[{'fqdn': 'NewHostname02', 'hostname': 'NewHostname02'}]
>>>



1 comment:

  1. Bitmelody: Accessing Arista Device Eapi Using Python >>>>> Download Now

    >>>>> Download Full

    Bitmelody: Accessing Arista Device Eapi Using Python >>>>> Download LINK

    >>>>> Download Now

    Bitmelody: Accessing Arista Device Eapi Using Python >>>>> Download Full

    >>>>> Download LINK Qv

    ReplyDelete