Skip to content

Instantly share code, notes, and snippets.

@yujiterada
Created November 3, 2022 03:42
Show Gist options
  • Save yujiterada/92c801794d2585462927e5958aad76b6 to your computer and use it in GitHub Desktop.
Save yujiterada/92c801794d2585462927e5958aad76b6 to your computer and use it in GitHub Desktop.
Receive SNMP Trap from Meraki Dashboard
import socket
from pysnmp.proto import api
from pyasn1.codec.ber import decoder
port = 162
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
while 1:
data, addr = s.recvfrom(4048)
print(addr[0])
version = int(api.decodeMessageVersion(data))
if version in api.protoModules:
protocol_module = api.protoModules[version]
request_message, whole_message = decoder.decode(
data, asn1Spec=protocol_module.Message(),
)
pdu = protocol_module.apiMessage.getPDU(request_message)
var_binds = protocol_module.apiPDU.getVarBinds(pdu)
for oid, val in var_binds:
print(' %s = %s' % (oid.prettyPrint(), val.prettyPrint()))
else:
print('Unsupported SNMP version %s' % version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment