Python Client for Arista eAPI
pyeapi is a Python client library for interacting with Arista EOS devices via their eAPI. It provides a simple, Pythonic interface for sending commands, parsing responses, and managing network configurations. The current version is 1.0.4, with releases occurring periodically to address bugs and introduce minor enhancements.
Common errors
-
No module named 'pyeapi'
cause The pyeapi library is not installed in the active Python environment.fixInstall the library using pip: `pip install pyeapi`. -
pyeapi.eapilib.EapiError: Failed to connect to eAPI: Connection refused
cause The eAPI service is not running or accessible on the target device, or there's a network connectivity issue (firewall, routing).fixVerify network connectivity to the device (e.g., `ping`). Ensure eAPI is enabled on the Arista switch (`management api http-command` or `management api https-command` in config mode). Check firewall rules and the specified port (default 80 for HTTP, 443 for HTTPS). -
pyeapi.eapilib.EapiError: Failed to connect to eAPI: Authentication failed
cause Incorrect username or password provided for the eAPI connection.fixDouble-check the username and password configured for eAPI access on the Arista switch. Ensure they match the credentials passed to `pyeapi.connect`. -
pyeapi.eapilib.EapiError: 'interface Ethernet1' failed with error 'Invalid command'
cause The command sent to the device is syntactically incorrect, not supported, or used in the wrong mode for the current eAPI context.fixReview the exact command syntax for Arista EOS. Test the command directly on the switch CLI to ensure it's valid. Some commands require 'enable' mode or specific configuration contexts.
Warnings
- breaking pyeapi v1.0.0 and later are Python 3 only. Python 2.7 is no longer supported.
- breaking pyeapi v1.0.0 and later require Python 3.7 as the minimum supported Python version.
- gotcha For on-box deployments (pyeapi running directly on the Arista switch), Arista EOS 4.22 or later is required for pyeapi v1.0.0+.
- gotcha Version 1.0.2 provided a critical bug fix. It is strongly advised to upgrade to 1.0.2 or later to avoid potential issues introduced in earlier 1.x releases.
Install
-
pip install pyeapi
Imports
- Node
import pyeapi.client.Node
from pyeapi.client import Node
- connect
import pyeapi node = pyeapi.connect(host='10.0.0.1')
Quickstart
import pyeapi
import os
# Configure connection details (e.g., in a YAML file or environment variables)
# For quickstart, using environment variables as placeholders:
HOST = os.environ.get('PYEAPI_HOST', '10.0.0.1')
USERNAME = os.environ.get('PYEAPI_USERNAME', 'admin')
PASSWORD = os.environ.get('PYEAPI_PASSWORD', 'arista')
# A more robust setup would use a pyeapi.conf file or direct dictionary config
# For simplicity, we'll create a dictionary config for direct connection
node_config = {
'transport': 'https',
'host': HOST,
'username': USERNAME,
'password': PASSWORD,
'port': 443,
'timeout': 30
}
try:
# Connect to the Arista EOS device
node = pyeapi.connect(**node_config)
print(f"Successfully connected to {HOST}")
# Run a 'show version' command
response = node.run_commands(['show version'])
print("\n--- Device Version ---")
print(f"EOS Version: {response[0]['version']}")
print(f"Hostname: {response[0]['hostname']}")
# Run a 'show interfaces' command
interfaces_response = node.run_commands(['show interfaces'])
print("\n--- Interfaces Status ---")
for intf_name, intf_data in interfaces_response[0]['interfaces'].items():
print(f" {intf_name}: {intf_data['lineProtocolStatus']} (Admin: {intf_data['interfaceStatus']})")
except pyeapi.eapilib.EapiError as e:
print(f"EAPI Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")