• EAPI script to push extensions across list of EOS devices using Python

 
 
Print Friendly, PDF & Email

Introduction

This article demonstrates Arista’s EOS automation capabilities leveraging eAPI. This document walks you through a script to Secure Copy (SCP) an extension on Arista switches along with installing the extension on the list of switches defined in the script.

Pre-Requisites

This script is supported on Linux/Unix/Mac Operation Systems. The Arista eAPI must be enabled on the switches and can be configured as follows:

Arista> enable
Arista# configure terminal
Arista(config)# management api http-commands
Arista(config-mgmt-api-http-cmds)# [no] shutdown
Arista(config-mgmt-api-http-cmds)# [no] protocol https [port ]
Arista(config-mgmt-api-http-cmds)# [no] protocol http [port ]

Jsonrpclib python module, could be installed as follows:

[admin@Arista ~]$ pip install jsonrpclib

Deployment methods

There is a choice of different options to install extensions across devices.

1. Manual configuration through Cli

        a.  User needs to securely copy the file into flash across all the switches

        b.  User needs to run commands to install extensions across all switches

        c.  Further instructions on installing extensions on EOS can be found on this link.

2. GUI based installation  by creating an Image Bundle in Cloud Vision Portal (CVP)  with the desired extension and push it on the device or container. Further information can be found on this link

3. Using Python based eAPI script to install extensions across list of devices, we will discuss this script further in the next section of this article.

Script Components

        1. automated_patch_push.py

        2. hosts.txt

        3. output_logs.txt

Script Explanation

The script automated_patch_push.py reads the list of devices from the file hosts.txt and creates a list. The script communicates with the list of Switches using eAPI configured on the switch and the jsonrpclib module.

The script first copies the desired extensions on the switch and then runs the set of commands to install the extension on the switch. The script then logs the failure/success of the tasks in output_logs.txt file.

eAPI Script (automated_patch_push.py)

import subprocess
import getpass
from jsonrpclib import Server
import pexpect
import sys


myfile = "./SecurityAdvisory0030-Hotfix.swix" #Mention any extension that you are trying to push to the switches

with open("./hosts.txt") as host_list:
  ips = host_list.readlines()

user = raw_input("Username:") #Input the username of an account on the switch
password = getpass.getpass("Password:") #Input the password of an account on the switch

for ip in ips:
  ip = ip.rstrip()
  destination = "%s@%s:" % (user, ip)
  #Command to SCP extension on the switch

  scp_cmd = "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " + myfile + " " + destination   
  print "\nCOPYING FILE SecurityAdvisory0030-Hotfix.swix TO SWITCH %s \n Please wait for few secs....." %(ip)
  try:
    pexpect.run(scp_cmd)
    child = pexpect.spawn(scp_cmd)
    child.expect(r'.*assword:')
    child.sendline(password)
    child.expect(pexpect.EOF,timeout=5)
    output_lines = child.before
    print output_lines
  except:
    print "COPYING FILE SecurityAdvisory0030-Hotfix.swix TO SWITCH %s FAILED" % (ip)
  else:
    print "COPYING FILE SecurityAdvisory0030-Hotfix.swix TO SWITCH %s SUCCESFUL" % (ip)

  url = "http://%s:%s@%s/command-api" %(user,password,ip)
  #Commands to install extensions on the switch 
  cmds = ["enable","configure","copy flash:SecurityAdvisory0030-Hotfix.swix extension:","extension SecurityAdvisory0030-Hotfix.swix","copy installed-extensions boot-extensions"]
  try:
    switch = Server(url)
    extension_push = switch.runCmds(1,cmds,"json")
  except:
    print "INSTALLING EXTENSION SecurityAdvisory0030-Hotfix.swix FAILED for %s" % (ip)
    print "==========================================================================="
    with open('./output_logs.txt', 'a') as f:			
      status = "%s:             FAILED\n" % (ip)
      f.write(status)
  else:
    print "INSTALLING EXTENSION SecurityAdvisory0030-Hotfix.swix SUCCESFUL for %s" % (ip)
    print "============================================================================="
    with open('./output_logs.txt', 'a') as f:			
      status = "%s:            SUCCESFUL\n" % (ip)
      f.write(status)

Github Repo Script

The script can be downloaded on this link

Follow

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

Join other followers: