Last active
February 8, 2017 05:30
-
-
Save markberly/8940300 to your computer and use it in GitHub Desktop.
A short script to provide more detail about the devices connected to an ethernet switch, enhancing 'show mac address' to include OUI vendor info (via API call so you must have access to the Internet) and LLDP neighbor information. Built using Arista EOS eAPI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
# Written by: Mark Berly | |
# | |
# Copyright (c) 2014, Arista Networks, Inc. | |
# 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 | |
from jsonrpclib import Server | |
# URL to get the OUI to vendor mapping | |
url = "http://api.macvendors.com/" | |
# 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 ) ) | |
# collect MAC address table | |
def getMacAddrTable( switch, enablePass ): | |
rc = switch.runCmds( 1, [ { "cmd": "enable", "input": enablePass }, | |
"configure", | |
"show mac address-table" ] ) | |
return rc[2] | |
# collect lldp tables | |
def getLldpInfo( switch, enablePass ): | |
rc = switch.runCmds( 1, [ { "cmd": "enable", "input": enablePass }, | |
"configure", | |
"show lldp neighbors" ] ) | |
return rc[2] | |
# Make a nice header for the output | |
def printHeader(): | |
print '{0:5} {1:20} {2:10} {3:20} {4:30} {5:20}'.format( | |
'VLAN', | |
'MAC Address', | |
'Type', | |
'Interface', | |
'Vendor', | |
'LLDP Neighbor' | |
) | |
print '{0:5} {1:20} {2:10} {3:20} {4:30} {5:20}'.format( | |
'-----', | |
'-----------------', | |
'----------', | |
'--------------------', | |
'--------------------', | |
'--------------------' | |
) | |
# print the info | |
def printInfo( macAddrTable, lldpInfo ) : | |
ln = {} | |
neighborInfo = "" | |
printHeader() | |
for lldpNeighbor in lldpInfo['lldpNeighbors'] : | |
ln.update({lldpNeighbor['port']:lldpNeighbor['neighborDevice']}) | |
for unicastMacAddr in macAddrTable['unicastTable']['tableEntries'] : | |
macOUI = unicastMacAddr['macAddress'] | |
if unicastMacAddr['interface'] in ln: | |
neighborInfo = ln[ unicastMacAddr['interface'] ] | |
# Look up the OUI and print out the info | |
try: | |
vendorOui = urllib2.urlopen(url+macOUI).read() | |
# if lookup fails or returns error then print with unknown vendor | |
except: | |
vendorOui = 'Unknown vendor' | |
print '{0:5} {1:20} {2:10} {3:20} {4:30} {5:20}'.format( | |
str( unicastMacAddr['vlanId'] ), | |
unicastMacAddr['macAddress'], | |
unicastMacAddr['entryType'], | |
unicastMacAddr['interface'], | |
vendorOui, | |
neighborInfo | |
) | |
def main(): | |
# Create the command line parser / arguments | |
parser = argparse.ArgumentParser(description='Get MAC table information') | |
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 ) | |
# Collect the mac address table | |
macAddrTable = getMacAddrTable ( switch, args.enable_password ) | |
# collect the lldp information | |
lldpInfo = getLldpInfo( switch, args.enable_password ) | |
printInfo( macAddrTable, lldpInfo ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment