Python Forum
How to know coming snmp trap version in python pysnmp?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to know coming snmp trap version in python pysnmp?
#1
I have a snmp trap receiver for snmp v2. I want to get snmp version in coming traps. If snmp version is 1 or 3 or anything else, return an error. How can i do it, how can i check the version of coming trap?



# python snmp v2 trap receiver
from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
from datetime import datetime

snmpEngine = engine.SnmpEngine()

# replace the agent and port with you host machine and port
TrapAgentAddress = '192.168.1.27'
Port = 1162

print('Agent is listening SNMP2 Trap on {} , Port : {}'.format(
    TrapAgentAddress, Port))
print(
    '--------------------------------------------------------------------------'
)
config.addTransport(
    snmpEngine, udp.domainName + (1, ),
    udp.UdpTransport().openServerMode((TrapAgentAddress, Port)))

# Configure community here
config.addV1System(snmpEngine, ' ', 'public')


def cbFun(snmpEngine, stateReference, contextEngineId, contextName, varBinds,
          cbCtx):
    print('\n{0}New trap message received on {1} {0}'.format(
        '-' * 20,
        datetime.now().strftime('%d-%b-%Y at %H:%M:%S')))
    execContext = snmpEngine.observer.getExecutionContext(
            'rfc3412.receiveMessage:request')
    print('Trap is coming from %s:%s' % execContext['transportAddress'])
    print('snmpEngine : {0}'.format(snmpEngine))
    print('stateReference : {0}'.format(stateReference))
    print('contextEngineId : {0}'.format(contextEngineId))
    print('contextName : {0}'.format(contextName))
    print('cbCtx : {0}'.format(cbCtx))
    for name, val in varBinds:
        print('{0} = {1}'.format(name.prettyPrint(), val.prettyPrint()))
    print('{0}Trap message ends{0}\n'.format('-' * 20))
    print('System Description:')

ntfrcv.NotificationReceiver(snmpEngine, cbFun)

snmpEngine.transportDispatcher.jobStarted(1)

try:
    snmpEngine.transportDispatcher.runDispatcher()
except:
    snmpEngine.transportDispatcher.closeDispatcher()
    raise
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  run part of a script with a different version of Python jdog 2 460 Jan-09-2024, 08:49 PM
Last Post: jdog
  How to find out from outside Python (in Windows) the current version of Python? pstein 4 751 Oct-04-2023, 10:01 AM
Last Post: snippsat
  How to resolve version conflicts in Python? taeefnajib 0 932 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  Python venv and PIP version issue JanOlvegg 2 1,279 Feb-22-2023, 02:22 AM
Last Post: JanOlvegg
  Python Version upgrade nitinkukreja 1 912 Feb-04-2023, 10:27 PM
Last Post: Larz60+
  Can't update new python version on Pycharm GOKUUUU 6 3,867 Jul-23-2022, 09:24 PM
Last Post: GOKUUUU
  Building python (3.9.5) with different libexpat version (2.4.6) raghupcr 0 1,321 Feb-25-2022, 11:29 AM
Last Post: raghupcr
  Python keeps running the old version of the code quest 2 3,789 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime
  Receiving snmp traps with more than one Community String ilknurg 0 2,232 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  const or #define coming from C jamie_01 11 3,388 Sep-16-2021, 05:39 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020