Skip to content

Instantly share code, notes, and snippets.

@markberly
Last active February 8, 2017 05:30
Show Gist options
  • Save markberly/8940300 to your computer and use it in GitHub Desktop.
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.
#!/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()
@markberly
Copy link
Author

bash> ./getInfo2.py tm222 test test
VLAN  MAC Address          Type       Interface            Vendor                         LLDP Neighbor       
----- -----------------    ---------- -------------------- --------------------           --------------------
1     00:1c:73:0c:3d:5b    dynamic    Port-Channel2        Arista Networks, Inc.          switch1
1     00:1c:73:0c:3d:84    dynamic    Port-Channel2        Arista Networks, Inc.          switch1
1     00:25:90:32:ec:29    static     Ethernet2            Super Micro Computer, Inc.     switch1
10    00:1c:73:0c:18:90    dynamic    Port-Channel2        Arista Networks, Inc.          switch1
10    00:1c:73:0c:3d:5b    dynamic    Port-Channel2        Arista Networks, Inc.          switch1
10    00:1c:73:0c:3d:84    dynamic    Port-Channel2        Arista Networks, Inc.          switch1
20    00:00:00:00:00:01    dynamic    Port-Channel2        XEROX CORPORATION              switch1
20    00:00:d6:00:00:01    dynamic    Port-Channel2        PUNCH LINE HOLDING             switch1
20    00:1c:73:0c:18:67    dynamic    Port-Channel2        Arista Networks, Inc.          switch1
500   00:1c:73:01:a8:18    dynamic    Ethernet45           Arista Networks, Inc.          switch702
4084  00:1c:73:0c:18:67    dynamic    Port-Channel2        Arista Networks, Inc.          switch702
4084  00:1c:73:0c:18:90    dynamic    Port-Channel2        Arista Networks, Inc.          switch702
4084  00:1c:73:0c:3d:5b    dynamic    Port-Channel2        Arista Networks, Inc.          switch702
4084  00:1c:73:0c:3d:84    dynamic    Port-Channel2        Arista Networks, Inc.          switch702

@bwks
Copy link

bwks commented Jun 4, 2016

Great idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment