0

I'm running the example from pysnmp but

from pysnmp.entity import engine, config
from pysnmp.carrier.asyncore.dgram import udp
from pysnmp.entity.rfc3413 import ntfrcv

# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()

# Transport setup

# UDP over IPv4, first listening interface/port
config.addTransport(
    snmpEngine,
    udp.domainName + (1,),
    udp.UdpTransport().openServerMode(("127.0.0.1", 162))
)

# UDP over IPv4, second listening interface/port
config.addTransport(
    snmpEngine,
    udp.domainName + (2,),
    udp.UdpTransport().openServerMode(("127.0.0.1", 2161))
)

# SNMPv1/2c setup

# SecurityName <-> CommunityName mapping
config.addV1System(snmpEngine, 'my-area', 'public')


# Callback function for receiving notifications
# noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def cbFun(snmpEngine, stateReference, contextEngineId, contextName,
          varBinds, cbCtx):
    print('Notification from ContextEngineId "%s", ContextName "%s"' %                     (contextEngineId.prettyPrint(),
                                                                        contextName.prettyPrint()))
for name, val in varBinds:
    print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))


# Register SNMP Application at the SNMP engine
ntfrcv.NotificationReceiver(snmpEngine, cbFun)

snmpEngine.transportDispatcher.jobStarted(1)  # this job would never finish

# Run I/O dispatcher which would receive queries and send confirmations
try:
    snmpEngine.transportDispatcher.runDispatcher()
except:
    snmpEngine.transportDispatcher.closeDispatcher()
    raise

and then execute "snmptrap -v2c -c public 127.0.0.1:162 123 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.5.0 s test" but it seems that message is not received. I was trying to cahnge 127.0.0.1 to 0.0.0.0 but result is same (callback function is not called). I'm on windows 10. Do I need to configure snmp service or snmp trap in windows settings? Or it should work as is.

  • I was trying to change port from 161 to 162 (22, 25 thought there are firewall issues) , netstat shows UDP 127.0.0.1:162 *:* – Tim Barabanov Nov 29 '18 at 14:46
  • Sounds like a firewall issue... Can you check your firewall settings to make sure it allows inbound 162/udp? Also, are you trying to `snmptrap` locally? Local interface (`127.0.0.1`) won't be reachable from the other machine. – Ilya Etingof Nov 29 '18 at 16:25
  • I've tried to completely desable firewall but it doesn't help. Funny thing is that: timofei@LAPTOP-DFF2U3J5:/mnt/c/Users/Timofei$ nc -uv 127.0.0.1 162 Connection to 192.168.1.61 162 port [udp/snmp-trap] succeeded!" but still nothing in the console! – Tim Barabanov Nov 29 '18 at 18:42
  • solved. just run the script in powershell as administator. – Tim Barabanov Nov 29 '18 at 19:25
  • If you solved it, post an answer. The likely answer is that a non-privileged process is not permitted to listen on such a low port, but your script should be improved also so that it _says_ when starting the server failed. Total lack of error handling in your script has led to you wasting your time! – Lightness Races in Orbit Nov 30 '18 at 17:01

0 Answers0