• Enabling eAPI on Multiple Devices using Netmiko

 
 
Print Friendly, PDF & Email

Mass Enabling eAPI with Python

Preface

In standard Arista deployments, one of the most powerful and common features that will be used is our EOS API (eAPI). It is good practice to enable this on every initial deployment. One of the best ways to do this is via our Zero Touch Provisioning (ZTP), but in some cases, ZTP is not an option. When this becomes the case, it can be easy to enable eAPI on all of your devices quickly through the use of the Python programming language and SSH (which is enabled by default on all Arista Switches).

Throughout this document, we will go over a simple script that can be built by utilizing YAML and Python. If you are new to Python, YAML is an easy to read data structure that can be utilized to hold mass Python variables to loop through.

Modules

The Python Modules that will be used in this script are the following:

  • PyYAML(yaml) – A simple module that will convert YAML Data Structures into a Python Dictionary Object.
    PyYAML Documentation
  • getpass – A built-in Python module for getting passwords, but not displaying them in the command line.
    getpass Documentation
  • Netmiko – An enhancement to the Paramiko Python Module that is tailored towards network configuration through SSH. We will specifically be using the “ConnectHandler” Module inside of Netmiko to establish SSH sessions and send commands to our devices.
    Netmiko Documentation

YAML File

The first step of our process will be to create a YAML file that holds the list of our devices and their IP Addresses for management. You can do this by simply creating a new file and giving it the .yaml file extension. In this file, I recommend using the hostname: ‘ip’ format as it is easy to read and quick to type. Note that the ip is in quotes. Since IP addresses are made of numbers, using quotes will automatically convert the variable to a string for future use. In our script, the hostname will only be used for informational output, so you can make it whatever you understand your device to be. Below is an example of a YAML File that I will use with 4 devices.

DCA-Leaf1: '10.10.10.201'
DCA-Leaf2: '10.10.10.202'
DCA-Spine1: '10.10.10.101'
DCA-Spine2: '10.10.10.102'

Save this file into a .yaml file extension in the same directory that you will use for your Python code and we are done with our device list.

Code

The first thing we will do is import our modules. For PyYAML and Netmiko, you will need to place the modules either in your Python directory or your Python virtual environment.

import yaml, getpass # Import PyYAML and getpass
from netmiko import ConnectHandler # import ConnectHandler from the netmiko module

It is good practice to store your Python code inside of seperate functions. Although this script will only contain one function, we will build it as if it were going to add other modules to it in the future by placing our code into a main() function.

def main(): # Define main Python Module for the script

The next step of the code will be to get use file operations inside of Python to open the YAML file and load it into a variable.

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)

The next step will be to utilize the “input” method as well as getpass to collect the credentials we will use later to establish the SSH session. We will be using a method inside of getpass as well called “getpass” that will act similarly to the “input”, but hide the text entry.

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)
    
    # Collect username from user and store it to a variable called "cred_user"   
    cred_user = input("Please enter the username for ssh: ") 
   
    ''' Collect password from user, but use getpass in order to 
    keep the password from dispalying on the screen. '''
    cred_pass = getpass.getpass("Please enter the password for ssh: ")

Now that we have the credentials stored for our SSH connection, we will create a for loop to be able to loop through these devices. We will also print a note that we are establishing the connection in order to inform the user on what is happening.

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)
    
    # Collect username from user and store it to a variable called "cred_user"   
    cred_user = input("Please enter the username for ssh: ") 
   
    ''' Collect password from user, but use getpass in order to 
    keep the password from dispalying on the screen. '''
    cred_pass = getpass.getpass("Please enter the password for ssh: ")
    
    ''' Begin for loop to loop through devices and perform
    the necessary actions to enable eAPI via the Netmiko connection handler.'''
   
    for hostname, ip in devices.items():
            print("Establishing connection to %s..." % hostname)

Next, we will “try” to establish a connection to the device and store the connection to a variable called “net_connect” that will hold the session and its details. If we have an exception, we will simply print that there was an error connecting to the device for the sake of simplicity. More extensive exception handling could be added in the future if you wish. Note that the username, and password are stored in variables we created and the ip address is stored in our loop defined variable.

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)
    
    # Collect username from user and store it to a variable called "cred_user"   
    cred_user = input("Please enter the username for ssh: ") 
   
    ''' Collect password from user, but use getpass in order to 
    keep the password from dispalying on the screen. '''
    cred_pass = getpass.getpass("Please enter the password for ssh: ")
    
    ''' Begin for loop to loop through devices and perform
    the necessary actions to enable eAPI via the Netmiko connection handler.'''
   
    for hostname, ip in devices.items():
            print("Establishing connection to %s..." % hostname)
            # Try to establish a connection to the device.
            try: 
                net_connect = ConnectHandler(device_type='arista_eos', ip=ip, 
                username=cred_user, password=cred_pass)

We will now code our operations that will take place after the SSH session is established. Refer to the Netmiko documentation to learn more about the methods it offers. In this example, we will only be using the “enable()” method in order to enter Privileged EXEC mode, “config_mode()” in order to enter Global Configuration, and lastly “send_config_set()” to send a list of variables with our commands. These commands will be sent in the order that you place them in the list. When the commands are sent, we will then inform the user that the task has been completed.

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)
    
    # Collect username from user and store it to a variable called "cred_user"   
    cred_user = input("Please enter the username for ssh: ") 
   
    ''' Collect password from user, but use getpass in order to 
    keep the password from dispalying on the screen. '''
    cred_pass = getpass.getpass("Please enter the password for ssh: ")
    
    ''' Begin for loop to loop through devices and perform
    the necessary actions to enable eAPI via the Netmiko connection handler.'''
   
    for hostname, ip in devices.items():
            print("Establishing connection to %s..." % hostname)
            # Try to establish a connection to the device.
            try: 
                net_connect = ConnectHandler(device_type='arista_eos', ip=ip, 
                username=cred_user, password=cred_pass)
            
                # Enter Privileged EXEC mode
                net_connect.enable()
                
                # Enter Global Configuration
                net_connect.config_mode()
                
                ''' Print an output letting the user know that you are sending 
                the commands and then send the list of commands to enable eAPI '''
                print("Enabling eAPI...")
                net_connect.send_config_set(['management api http-commands', 'no shutdown', 'write'])
                
                # Inform user of completion on the device
                print("eAPI enabled on %s." % hostname)

Earlier, we set the code to handle exceptions. We now need to enter the “except” part of this to complete the process and tell the code what to do if it encounters an exception.

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)
    
    # Collect username from user and store it to a variable called "cred_user"   
    cred_user = input("Please enter the username for ssh: ") 
   
    ''' Collect password from user, but use getpass in order to 
    keep the password from dispalying on the screen. '''
    cred_pass = getpass.getpass("Please enter the password for ssh: ")
    
    ''' Begin for loop to loop through devices and perform
    the necessary actions to enable eAPI via the Netmiko connection handler.'''
   
    for hostname, ip in devices.items():
            print("Establishing connection to %s..." % hostname)
            # Try to establish a connection to the device.
            try: 
                net_connect = ConnectHandler(device_type='arista_eos', ip=ip, 
                username=cred_user, password=cred_pass)
            
                # Enter Privileged EXEC mode
                net_connect.enable()
                
                # Enter Global Configuration
                net_connect.config_mode()
                
                ''' Print an output letting the user know that you are sending 
                the commands and then send the list of commands to enable eAPI '''
                print("Enabling eAPI...")
                net_connect.send_config_set(['management api http-commands', 'no shutdown', 'write'])
                
                # Inform user of completion on the device
                print("eAPI enabled on %s." % hostname)
                
            # If connection has an error, tell the user
            except:
                print("*****Error connecting to %s*****" % hostname)

Our logic is now completed.

Lastly, since we created our code inside of the main() function, we will need to call this function at runtime with the following code:

if __name__ == '__main__':
        main()

Completed Code

import yaml, getpass # Import PyYAML and getpass
from netmiko import ConnectHandler # import ConnectHandler from the netmiko module

def main(): # Define main Python Module for the script
    # Open YAML file as "deviceYAML" and save it to a variable called "devices"
    with open('devices.yaml') as device_yaml: 
        devices = yaml.safe_load(device_yaml)
    
    # Collect username from user and store it to a variable called "cred_user"   
    cred_user = input("Please enter the username for ssh: ") 
   
    ''' Collect password from user, but use getpass in order to 
    keep the password from dispalying on the screen. '''
    cred_pass = getpass.getpass("Please enter the password for ssh: ")
    
    ''' Begin for loop to loop through devices and perform
    the necessary actions to enable eAPI via the Netmiko connection handler.'''
   
    for hostname, ip in devices.items():
            print("Establishing connection to %s..." % hostname)
            # Try to establish a connection to the device.
            try: 
                net_connect = ConnectHandler(device_type='arista_eos', ip=ip, 
                username=cred_user, password=cred_pass)
            
                # Enter Privileged EXEC mode
                net_connect.enable()
                
                # Enter Global Configuration
                net_connect.config_mode()
                
                ''' Print an output letting the user know that you are sending 
                the commands and then send the list of commands to enable eAPI '''
                print("Enabling eAPI...")
                net_connect.send_config_set(['management api http-commands', 'no shutdown', 'write'])
                
                # Inform user of completion on the device
                print("eAPI enabled on %s." % hostname)
                
            # If connection has an error, tell the user
            except:
                print("*****Error connecting to %s*****" % hostname)
            
if __name__ == '__main__':
        main()
Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: