Skip to content

Instantly share code, notes, and snippets.

@markberly
Created February 17, 2014 20:30
Show Gist options
  • Save markberly/9058507 to your computer and use it in GitHub Desktop.
Save markberly/9058507 to your computer and use it in GitHub Desktop.
Arista EOS eAPI tool to get Event Monitor data and convert from text format to string to make it easier to manipulate
#!/usr/bin/python
#
# Copyright (c) 2014
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - Neither the name of Arista Networks nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse, urllib2, subprocess, os
from jsonrpclib import Server
# connect to the switch either via http or https
def connect( sIpOrHostname, username, password, secure=True ) :
return Server( "%s://%s:%s@%s/command-api"
% ( "http" if not secure else "https",
password, username, sIpOrHostname ) )
def getMacEventMonitor( switch, enablePass, macAddr = None ) :
rc = switch.runCmds( 1, [ { "cmd": "enable", "input": enablePass },
"configure",
"show event-monitor mac match-mac " + macAddr ],
"text" )
return rc[2]
def main():
# Create the command line parser / arguments
parser = argparse.ArgumentParser(description='Get MAC table information')
parser.add_argument( 'macAddress', help='MAC address to get details' )
parser.add_argument( 'sIpOrHostname', help='Switch IP address or Hostname' )
parser.add_argument( 'username', help='Username' )
parser.add_argument( 'password', help='Password' )
parser.add_argument( "--http", help="Use unsecure http connection (default is https)",
action="store_false", default=True )
parser.add_argument( "--enable-password", help="Enable-mode password",
default="" )
args = parser.parse_args()
# connect to switch
switch = connect ( args.sIpOrHostname, args.username, args.password, args.http )
# get mac address records from event-monitor
eventRecords = getMacEventMonitor( switch, args.enable_password, args.macAddress )
# convert the text output to strings to make it easier to work with
for macAddress in eventRecords.itervalues() :
# get back text and separate into records delimiter \n
i = eventRecords.values()
record = i[0].split('\n')
for line in record :
# if an empty string break out of for loop
if line == '' : break
# take records and create lists delimiter |
recordDetails = line.split('|')
# Put a ' -' in front of the out to make it more readable
print ' -',
# This position in the string is the action (e.g. add / remove)
# Check to see the positional argument is '' if so then use the
# next value, this is due to how I built the string
if recordDetails[4] == '' : print '{0:17} | '.format ( recordDetails[5] ),
else : print '{0:17} | '.format ( recordDetails[4] ),
# This position in the string is the date / time
print '{0:20} | '.format ( recordDetails[0] ),
# This position in the string is the interface it was learned
# If no interface (case of removed MAC) print N/A
if recordDetails[3] == '' : print '{0:15} | '.format ( 'N/A' ),
else : print '{0:15} | '.format ( recordDetails[3] ),
# This position in the string is the VLAN it was learned
print '{0:5} | '.format ( recordDetails[1] )
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment