Skip to content

Instantly share code, notes, and snippets.

@benhaan
Created January 6, 2015 05:35
Show Gist options
  • Save benhaan/4be25d1158cfc6325de4 to your computer and use it in GitHub Desktop.
Save benhaan/4be25d1158cfc6325de4 to your computer and use it in GitHub Desktop.
Fetch all the interfaces from a Cisco switch and return its config, specifically VLAN, name, description, and type (access or trunk)
from __future__ import print_function
from snimpy.manager import Manager as M
from snimpy.manager import load
from snimpy.snmp import SNMPNoSuchInstance
#import pymysql
# Load up SNMP MIBs
load("IF-MIB")
load("./mibs/CISCO-SMI.txt")
load("./mibs/CISCO-TC.txt")
load("./mibs/CISCO-VTP-MIB.txt")
load("./mibs/CISCO-VLAN-MEMBERSHIP-MIB.txt")
load("./mibs/CISCO-STACK-MIB.txt")
load("./mibs/CISCO-PRODUCTS-MIB.txt")
# Create our SNMP connection
manager = M(host="10.0.0.1", community="public", version=2)
try:
for ifIndex in manager.ifDescr:
# Only include ethernet interfaces
if manager.ifType[ifIndex] == 6:
ifAlias = manager.ifDescr[ifIndex]
ifDescription = manager.ifAlias[ifIndex]
# Check if we have a trunk or access port
if manager.vlanTrunkPortDynamicState[ifIndex] == 1:
ifType = 'trunk'
# Set to native vlan to 0, since it's a trunk
ifNativeVlan = 0
else:
ifType = 'access'
# Since this is an access port, we'll also get the native VLAN
ifNativeVlan = manager.vmVlan[ifIndex]
ifStatus = manager.ifAdminStatus[ifIndex]
ifLastStateChange = manager.ifLastChange[ifIndex]
values = (str(ifAlias), ifType, int(ifNativeVlan), int(ifIndex), 1, str(ifDescription), int(ifStatus))
print(values)
except SNMPNoSuchInstance:
pass
finally:
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment