├── README.markdown └── stpPortStatus.py /README.markdown: -------------------------------------------------------------------------------- 1 | # Python Cisco Switch SNMP Monitoring for Spanning-tree 2 | This small script outputs a list of active VLANs, the ports associated with them and their STP port status: disabled, blocking, forwarding, and so forth. It might be used to monitor Spanning-tree. 3 | ## How it works 4 | The script polls for the active VLANs on the switch. It then retrieves STP port status by VLAN. 5 | ## How to use 6 | Simply change the IP address, port and username of your switch's SNMP service. You can easily adapt the script for automation over multiple devices. Please refer to the pySNMP documentation for more advanced SNMP development. 7 | ## Support 8 | Feel free to send me an email at lussier at the IEEE organization domain name. 9 | -------------------------------------------------------------------------------- /stpPortStatus.py: -------------------------------------------------------------------------------- 1 | from pysnmp.entity.rfc3413.oneliner import cmdgen 2 | 3 | ################################################################################################# 4 | # Global Settings 5 | deviceIPAddress = "192.168.254.250" 6 | snmpPort = 161 7 | snmpUsername = "public" 8 | # Port Status Displayed Information 9 | statusID = {1:"Disabled", 2:"Blocking", 3:"Listening", 4:"Learning", 5:"Forwarding", 6:"Broken"} 10 | ################################################################################################# 11 | 12 | # Retrieve VLAN Table 13 | errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('agent',snmpUsername,0),cmdgen.UdpTransportTarget((deviceIPAddress,snmpPort)),(1,3,6,1,4,1,9,9,46,1,3,1,1,2)) 14 | # Store VLAN Table 15 | vlans = [] 16 | for row in varBinds: 17 | for item in row: 18 | vlans.append(item[0][15]) 19 | # Retrieve STP port status information 20 | vlanAndSTP = {} 21 | for vlan in vlans: 22 | username = '%s@%s' % (snmpUsername,vlan) 23 | errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('agent',username,0),cmdgen.UdpTransportTarget((deviceIPAddress,snmpPort)),(1,3,6,1,2,1,17,2,15,1,3)) 24 | # Store STP port status information 25 | stpPortInfoForThisVLAN = {} 26 | for row in varBinds: 27 | for item in row: 28 | port = item[0][11] 29 | status = item[1] 30 | stpPortInfoForThisVLAN[port] = status 31 | vlanAndSTP[vlan] = stpPortInfoForThisVLAN 32 | # Display final dictionary 33 | for item in vlanAndSTP.iteritems(): 34 | if item[1]: 35 | for subitem in item[1].iteritems(): 36 | vlanID = str(item[0]) 37 | portNumber = str(subitem[0]) 38 | portStatusID = subitem[1] 39 | portStatusDescription = statusID[portStatusID] 40 | vlanStr = "VLAN " + vlanID 41 | space1 = " "*(15-len(vlanStr)) 42 | portStr = "PORT " + portNumber 43 | space2 = " "*(15-len(portStr)) 44 | print vlanStr + space1 + portStr + space2 + portStatusDescription 45 | --------------------------------------------------------------------------------