Client

Python Client for eAPI

This module provides the client for eAPI. It provides the primary functions for building applications that work with Arista EOS eAPI-enabled nodes. The first function is to provide a client for sending and receiving eAPI request and response objects on a per node basis. The second function provides a library for building API enabled data models for configuring EOS nodes.

This library allows for creating connections to EOS eAPI enabled nodes using the connect or connect_to function. Both functions will return an instance of a Node object that can be used to send and receive eAPI commands. The Node object can autoload API modules for a structured object oriented approach to configuring the EOS node with native Python objects.

Example

>>> import pyeapi
>>> conn = pyeapi.connect(host='10.1.1.1', transport='http')
>>> conn.execute(['show verson'])
{u'jsonrpc': u'2.0', u'result': [{u'memTotal': 2028008, u'version':
u'4.14.5F', u'internalVersion': u'4.14.5F-2209869.4145F', u'serialNumber':
u'', u'systemMacAddress': u'00:0c:29:f5:d2:7d', u'bootupTimestamp':
1421765066.11, u'memFree': 213212, u'modelName': u'vEOS', u'architecture':
u'i386', u'internalBuildId': u'f590eed4-1e66-43c6-8943-cee0390fbafe',
u'hardwareRevision': u''}], u'id': u'4312565648'}
>>> node = pyeapi.connect_to('veos01')
>>> node.enable('show version')
{u'jsonrpc': u'2.0', u'result': [{u'memTotal': 2028008, u'version':
u'4.14.5F', u'internalVersion': u'4.14.5F-2209869.4145F', u'serialNumber':
u'', u'systemMacAddress': u'00:0c:29:f5:d2:7d', u'bootupTimestamp':
1421765066.11, u'memFree': 213212, u'modelName': u'vEOS', u'architecture':
u'i386', u'internalBuildId': u'f590eed4-1e66-43c6-8943-cee0390fbafe',
u'hardwareRevision': u''}], u'id': u'4312565648'}

Additionally the node object can automatically load API modules to work with the resources in the configuration. The API autoloader supports automatic loading of modules in pyeapi.api as well as provides the ability to build custom API modules to be loaded from a different namespace.

Example

>>> import pyeapi
>>> node = pyeapi.connect_to('veos01')
>>> node.api('vlans').get(1)
{'state': 'active', 'name': 'default', 'vlan_id': 1, 'trunk_groups': []}

The API autoloader loads API modules by their filename.

The following objects are provide in this module for creating clients to interface with eAPI.

Node – Creates an instance of a node object that represents a single EOS device. Each EOS device to be managed should have a Node instance

Config – A subclass of ConfigParser.SafeConfigParser that handles the configuration file. The configuration file is an INI style file that contains the settings for nodes used by the connect_to function.

class pyeapi.client.Config(filename=None)

Bases: backports.configparser.ConfigParser

Conifguration instance for managing the eapi.conf file.

This class provides an instance for handling the configuration file. It should normally need to be instantiated. A single config object is instantiated by the module for working with the config.

filename

str – The full path to the loaded filename

Parameters:filename (str) – The full path to the filename to be loaded when the object is instantiated.
add_connection(name, **kwargs)

Adds a connection to the configuration

This method will add a connection to the configuration. The connection added is only available for the lifetime of the object and is not persisted.

Note

If a call is made to load() or reload(), any connections added with this method must be re-added to the config instance

Parameters:
  • name (str) – The name of the connection to add to the config. The name provided will automatically be prepended with the string connection:
  • **kwargs (dict) – configuration
autoload()

Loads the eapi.conf file

This method will use the module variable CONFIG_SEARCH_PATH to attempt to locate a valid eapi.conf file if a filename is not already configured. This method will load the first eapi.conf file it finds and then return.

The CONFIG_SEARCH_PATH can be overridden using an environment variable by setting EAPI_CONF.

connections

Returns all of the loaded connections names as a list

generate_tags()

Generates the tags with collection with hosts

get_connection(name)

Returns the properties for a connection name

This method will return the settings for the configuration specified by name. Note that the name argument should only be the name.

For instance, give the following eapi.conf file

[connection:veos01]
transport: http

The name to use to retrieve the configuration would be veos01

>>> pyeapi.client.config.get_connection('veos01')
Parameters:name (str) – The name of the connection to return
Returns:
A Python dictionary object of key/value pairs that represent
the node configuration. If the name provided in the argument is not found, then None is returned.
load(filename)

Loads the file specified by filename

This method works in conjunction with the autoload method to load the file specified by filename.

Parameters:filename (str) – The full path to the file to be loaded
read(filename)

Reads the file specified by filename

This method will load the eapi.conf file specified by filename into the instance object. It will also add the default connection localhost if it was not defined in the eapi.conf file

Parameters:filename (str) – The full path to the file to load
reload()

Reloades the configuration

This method will reload the configuration instance using the last known filename. Note this method will initially clear the configuration and reload all entries.

class pyeapi.client.Node(connection, **kwargs)

Bases: object

Represents a single device for sending and receiving eAPI messages

The Node object provides an instance for communicating with Arista EOS devices. The Node object provides easy to use methods for sending both enable and config commands to the device using a specific transport. This object forms the base for communicating with devices.

connection

EapiConnection – The connection property represents the underlying transport used by the Node object to communicate with the device using eAPI.

running_config

str – The running-config from the device. This property is lazily loaded and refreshed over the life cycle of the instance.

startup_config

str – The startup-config from the device. This property is lazily loaded and refreshed over the life cycle of the instance.

autorefresh

bool – If True, the running-config and startup-config are refreshed on config events. If False, then the config properties must be manually refreshed.

settings

dict – Provides access to the settings used to create the Node instance.

Parameters:
  • connection (EapiConnection) – An instance of EapiConnection used as the transport for sending and receiving eAPI requests and responses.
  • **kwargs – An arbitrary list of keyword arguments
api(name, namespace='pyeapi.api')

Loads the specified api module

This method is the API autoload mechanism that will load the API module specified by the name argument. The API module will be loaded and look first for an initialize() function and secondly for an instance() function. In both cases, the node object is passed to the module.

Parameters:
  • name (str) – The name of the module to load. The name should be the name of the python file to import
  • namespace (str) – The namespace to use to load the module. The default value is ‘pyeapi.api’
Returns:

The API module loaded with the node instance.

config(commands, **kwargs)

Configures the node with the specified commands

This method is used to send configuration commands to the node. It will take either a string or a list and prepend the necessary commands to put the session into config mode.

Parameters:
  • commands (str, list) – The commands to send to the node in config mode. If the commands argument is a string it will be cast to a list. The list of commands will also be prepended with the necessary commands to put the session in config mode.
  • **kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request
Returns:

The config method will return a list of dictionaries with the

output from each command. The function will strip the response from any commands it prepends.

connection
enable(commands, encoding='json', strict=False, send_enable=True, **kwargs)

Sends the array of commands to the node in enable mode

This method will send the commands to the node and evaluate the results. If a command fails due to an encoding error, then the command set will be re-issued individual with text encoding.

Parameters:
  • commands (list) – The list of commands to send to the node
  • encoding (str) – The requested encoding of the command output. Valid values for encoding are JSON or text
  • strict (bool) – If False, this method will attempt to run a command with text encoding if JSON encoding fails
  • send_enable (bool) – If True the enable command will be prepended to the command list automatically.
  • **kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request
Returns:

A dict object that includes the response for each command along

with the encoding

Raises:
  • TypeError – This method does not support sending configure commands and will raise a TypeError if configuration commands are found in the list of commands provided

    This method will also raise a TypeError if the specified encoding is not one of ‘json’ or ‘text’

  • CommandError – This method will raise a CommandError if any one of the commands fails.

enable_authentication(password)

Configures the enable mode authentication password

EOS supports an additional password authentication mechanism for sessions that want to switch to executive (or enable) mode. This method will configure the password, if required, for entering executive mode

Parameters:password (str) – The password string in clear text used to authenticate to exec mode
get_config(config='running-config', params=None, as_string=False)

Retreives the config from the node

This method will retrieve the config from the node as either a string or a list object. The config to retrieve can be specified as either the startup-config or the running-config.

Parameters:
  • config (str) – Specifies to return either the nodes startup-config or running-config. The default value is the running-config
  • params (str) – A string of keywords to append to the command for retrieving the config.
  • as_string (boo) – Flag that determines the response. If True, then the configuration is returned as a raw string. If False, then the configuration is returned as a list. The default value is False
Returns:

This method will return either a string or a list depending on the states of the as_string keyword argument.

Raises:

TypeError – If the specified config is not one of either ‘running-config’ or ‘startup-config’

model
refresh()

Refreshes the instance config properties

This method will refresh the public running_config and startup_config properites. Since the properties are lazily loaded, this method will clear the current internal instance variables. One the next call the instance variables will be repopulated with the current config

run_commands(commands, encoding='json', send_enable=True, **kwargs)

Sends the commands over the transport to the device

This method sends the commands to the device using the nodes transport. This is a lower layer function that shouldn’t normally need to be used, preferring instead to use config() or enable().

Parameters:
  • commands (list) – The ordered list of commands to send to the device using the transport
  • encoding (str) – The encoding method to use for the request and excpected response.
  • send_enable (bool) – If True the enable command will be prepended to the command list automatically.
  • **kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request
Returns:

This method will return the raw response from the connection

which is a Python dictionary object.

running_config
section(regex, config='running_config')

Returns a section of the config

Parameters:
  • regex (str) – A valid regular expression used to select sections of configuration to return
  • config (str) – The configuration to return. Valid values for config are “running_config” or “startup_config”. The default value is “running_config”
Returns:

The configuration section as a string object.

startup_config
version
version_number
pyeapi.client.config_for(name)

Function to get settings for named config

This function will return the settings for a specific connection as specified by name. Its a convenience function that calls get_connection on the global config instance

Parameters:name (str) – The name of the connection to return. The connection name is specified as the string right of the : in the INI file
Returns:
A Python dictionary object of key/value pairs that represent the
nodes configuration settings from the config instance
pyeapi.client.connect(transport=None, host='localhost', username='admin', password='', port=None, timeout=60, return_node=False, **kwargs)

Creates a connection using the supplied settings

This function will create a connection to an Arista EOS node using the arguments. All arguments are optional with default values.

Parameters:
  • transport (str) – Specifies the type of connection transport to use. Valid values for the connection are socket, http_local, http, and https. The default value is specified in DEFAULT_TRANSPORT
  • host (str) – The IP addres or DNS host name of the connection device. The default value is ‘localhost’
  • username (str) – The username to pass to the device to authenticate the eAPI connection. The default value is ‘admin’
  • password (str) – The password to pass to the device to authenticate the eAPI connection. The default value is ‘’
  • port (int) – The TCP port of the endpoint for the eAPI connection. If this keyword is not specified, the default value is automatically determined by the transport type. (http=80, https=443)
  • return_node (bool) – Returns a Node object if True, otherwise returns an EapiConnection object.
Returns:

An instance of an EapiConnection object for the specified transport.

pyeapi.client.connect_to(name)

Creates a node instance based on an entry from the config

This function will retrieve the settings for the specified connection from the config and return a Node instance. The configuration must be loaded prior to calling this function.

Parameters:name (str) – The name of the connection to load from the config. The name argument should be the connection name (everything right of the colon from the INI file)
Returns:
This function will return an instance of Node with the settings
from the config instance.
Raises:AttributeError – raised if the specified configuration name is not found in the loaded configuration
pyeapi.client.hosts_for_tag(tag)

Returns the hosts assocated with the specified tag

This function will return the hosts assoicated with the tag specified in the argument. It will return an array of connecition names.

Parameters:tag (str) – The name of the tag to retrieve the list of hosts for
Returns:
A Python list object that includes the list of hosts assoicated
with the specified tag.

None: If the specified tag does not exist, then None is returned.

Return type:list
pyeapi.client.load_config(filename)

Function method that loads a conf file

This function will load the file specified by filename into the config instance. Its a convenience function that calls load on the config instance

Parameters:filename (str) – The full path to the filename to load
pyeapi.client.make_connection(transport, **kwargs)

Creates a connection instance based on the transport

This function creates the EapiConnection object based on the desired transport. It looks up the transport class in the TRANSPORTS global dictionary.

Parameters:
  • transport (string) – The transport to use to create the instance.
  • **kwargs – Arbitrary keyword arguments.
Returns:

An instance of a connection object based on the transport

Raises:

TypeError – A TypeError is raised if the transport keyword is not found in the list (keys) of available transports.