0

I am sending a SNMP trap using pysnmp with the following code (trap.py) source,

Note: X.X.X.X is the public ip address of my computer

from pysnmp.hlapi import *
from pysnmp import debug

hostname = 'X.X.X.X' ##public IP address of receiver
debug.setLogger(debug.Debug('msgproc'))
next(sendNotification(SnmpEngine(),
     CommunityData('public'),
     UdpTransportTarget((hostname, 162)),
     ContextData(),
     'trap',
     # sequence of custom OID-value pairs
     [ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'), OctetString('my string')),
      ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'), Integer32(42))]))

I am receiving it using pysnmp with this code (receiver.py) source

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(('X.X.X.X', 162))
)
# 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

In receiver.py, I get an error

pysnmp.carrier.error.CarrierError: bind() for ('X.X.X.X', 162) failed: [WinError 10049] The requested address is not valid in its context

I would like to be able to send SNMP traps non-locally. So I'm testing test.py with my public IP address. Everything works locally (if I use 127.0.0.1) but not with public IP address. Should I install other SNMP software to listen to trap.py?

mionnaise
  • 188
  • 1
  • 18

1 Answers1

0

This error:

pysnmp.carrier.error.CarrierError: bind() for ('X.X.X.X', 162) failed: [WinError 10049] The requested address is not valid in its context

has nothing to do with SNMP or pysnmp. It comes from Windows TCP/IP stack. Make sure that:

  • The X.X.X.X address is present as a local IP interface at the machine where you run your receiver
  • Windows user privileges allow you to bind to ports below 1024 (e.g. 162 in this case). Alternatively, you can try to bind to a non-standard port e.g. 1024+ to see if that helps.
  • Google for Windows error 10049, what does it mean
Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • But my traps.py can send traps using public IP address (not local IP address)? I use https://whatismyipaddress.com/ for the IP address, is this right? How do I make a receiver? – mionnaise May 03 '18 at 23:51
  • Now I'm using an Ubuntu VM from the cloud, which uses snmptrapd to receive traps `snmptrapd -CdfLo --disableAuthorization=yes`. When I run traps.py on this VM, I'm able to receive it. But when I run it in a different machine, I can't. HELP? – mionnaise May 04 '18 at 02:36
  • I guess your TRAP receiving script should actually use local IP rather than public IP. Because your script binds to a specific IP interface which **must be local**. The NAT device in-between TRAP originator and your receiving script will to the network address translation from public IP into private (local) IP. – Ilya Etingof May 14 '18 at 06:27