Skip to content

Instantly share code, notes, and snippets.

@ASolchen
Created March 17, 2023 16:11
Show Gist options
  • Save ASolchen/5d32e24cbed8416d967d04642b09a093 to your computer and use it in GitHub Desktop.
Save ASolchen/5d32e24cbed8416d967d04642b09a093 to your computer and use it in GitHub Desktop.
Get SNMP 5 min average switchport traffic on a 48-port Cisco 2960
"""
!add this to the switch config:
snmp-server community public RO
snmp-server location in some closet
snmp-server contact [email protected]
"""
from pysnmp.hlapi import *
# SNMPv2c Configuration
community_name = 'public'
ip_address = 'switch-ip-address'
oids = [ObjectType(ObjectIdentity(f'.1.3.6.1.4.1.9.2.2.1.1.6.101{str(port).zfill(2)}')) for port in range(1,53)]
# Create SNMP GET Command
iterator = getCmd(
SnmpEngine(),
CommunityData(community_name),
UdpTransportTarget((ip_address, 161)),
ContextData(),
*oids
)
# Execute the Command
for error_indication, error_status, error_index, var_binds in iterator:
if error_indication:
print(f"Error: {error_indication}")
else:
for var_bind in var_binds:
addr, val = var_bind
print(f"Rx <- Gi1/0/{str(addr[-1]%100).zfill(2)} - {'{:.3f}'.format(int(val)*0.000001)} Mbit/s")
oids = [ObjectType(ObjectIdentity(f'.1.3.6.1.4.1.9.2.2.1.1.8.101{str(port).zfill(2)}')) for port in range(1,53)]
# Create SNMP GET Command
iterator = getCmd(
SnmpEngine(),
CommunityData(community_name),
UdpTransportTarget((ip_address, 161)),
ContextData(),
*oids
)
# Execute the Command
for error_indication, error_status, error_index, var_binds in iterator:
if error_indication:
print(f"Error: {error_indication}")
else:
for var_bind in var_binds:
addr, val = var_bind
print(f"Tx -> Gi1/0/{str(addr[-1]%100).zfill(2)} - {'{:.3f}'.format(int(val)*0.000001)} Mbit/s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment