Python Forum
Receiving snmp traps with more than one Community String
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receiving snmp traps with more than one Community String
#1
I have the below snmp trap receiver. I have two community string, public and private.
I want to get traps from the devices who has com. string public and private.
In my db my com. strings are:
public
private

My code only get traps from private community strings.
How can i use it with more community strings?


#!/usr/bin/env python3.8

import asyncio
import concurrent.futures

from pysnmp.entity import engine, config
from pysnmp.carrier.asyncio.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv
import json
from datetime import datetime

exec(open("/etc/epp/SNMP/mysqlconnector.py").read())

query= "SELECT community_string FROM nat_snmp_string WHERE status ='enable'"
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
com_str = cursor.fetchall()
for data in com_str:
   # print(data['community_string'])
    community = data['community_string']
    print(community)


class SnmpTrapDaemon():
#    @staticmethod
    def run_daemon(pool):
    
        # Create SNMP engine with autogenernated engineID and pre-bound
        # to socket transport dispatcher
        snmpEngine = engine.SnmpEngine()
    
        # Transport Setup
        config.addTransport(
            snmpEngine,
            udp.domainName,
            udp.UdpTransport().openServerMode(('192.168.1.200', '162'))
        )
    
        # SNMPv1/2c setup
        config.addV1System(
            snmpEngine, community, community)
    
        # Callback function for receiving notifications
        # noinspection PyUnusedLocal
        def cbFun(snmpEngine, stateReference, contextEngineId,
                contextName, varBinds, cbCtx):
    
            trap = {}
            for oid, val in varBinds:
                trap[oid.prettyPrint()] = val.prettyPrint()
            pool.submit(asyncio.run, process_trap(trap))
    
        # Register SNMP Application at the SNMP engine
        ntfrcv.NotificationReceiver(snmpEngine, cbFun)
    
        snmpEngine.transportDispatcher.jobStarted(1)
        try:
            print('Trap Listener started on port 162. Press Ctrl-c to quit.')
            snmpEngine.transportDispatcher.runDispatcher()
        except KeyboardInterrupt:
            print('user quit')
        finally:
            snmpEngine.transportDispatcher.closeDispatcher()

async def process_trap(trap):
    print('Processing TRAP - this might take while...')
    await asyncio.sleep(3)
    for item in trap.items():
        print(item)
    print('...done')

def main():
    print('Starting SNMP-TRAP Processor')
    query= "SELECT community_string FROM nat_snmp_string WHERE status ='enable'"
    cursor = connection.cursor(dictionary=True)
    cursor.execute(query)
    com_str = cursor.fetchall()
    for data in com_str:
   # print(data['community_string'])
     community = data['community_string']
     print(community)


    pool = concurrent.futures.ThreadPoolExecutor(max_workers=50)
    SnmpTrapDaemon.run_daemon(pool)

if __name__ == '__main__':
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receiving this error in my "response" and causes script to return wrong status cubangt 18 1,911 Aug-13-2023, 12:16 AM
Last Post: cubangt
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,407 Jul-31-2022, 08:20 PM
Last Post: paulyan
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,567 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  [Selenium]Timed out receiving message from renderer: 10.000 wood_6636 0 2,564 Jun-26-2020, 08:59 AM
Last Post: wood_6636
  Receiving XML exception from nmap.scan() results. PythonNmap 4 4,034 Jan-21-2020, 04:41 AM
Last Post: PythonNmap
  Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. PythonNmap 21 10,637 Jan-19-2020, 07:54 PM
Last Post: PythonNmap
  First Byte of a string is missing while receiving data over TCP Socket shahrukh1987 3 4,168 Nov-20-2019, 10:34 AM
Last Post: shahrukh1987
  Pycharm community edition venv folder tantony 3 3,273 Oct-18-2019, 12:28 PM
Last Post: tantony
  snmp hex-string to date time anna 10 9,578 Oct-15-2019, 10:50 AM
Last Post: anna
  Not receiving serial data from arduino dinoel_cool 4 3,636 Aug-26-2018, 06:16 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

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