0

I'm trying to write simple python app to send SNMP traps. I've already wrote MIB table and sending traps to localhost works fine.

from pysnmp.entity.rfc3413.oneliner import ntforg

ntfOrg = ntforg.NotificationOriginator()

errorIndication = ntfOrg.sendNotification(
    ntforg.CommunityData('public'),
    ntforg.UdpTransportTarget(('localhost', 162)),
    'trap',
    ntforg.MibVariable('MY-MIB', 'my_trap'),
    ( ntforg.MibVariable('MY-MIB', 'my_trap_var'), 0xAABBCCDD )
)

if errorIndication:
    print('Notification not sent: %s' % errorIndocation)

Now I need to modify code to send traps to private subnetwork.

I have its IP address, subnet mask and gateway IP. Let assume:

IP: 20.40.34.14

Subnet Mask: 255.255.255.224

Gateway: 20.40.34.10

Is there any way to solve this through proper ntforg.UdpTransportTarget(...) arguments? I looked up source code for this class (target.py) and internally it uses:

socket.getaddrinfo(transportAddr[0], # localhost in example
                   transportAddr[1], # 162 in example
                   socket.AF_INET,
                   socket.SOCK_DGRAM,
                   socket.IPPROTO_UDP)[0][4][:2]
Community
  • 1
  • 1
kostr22
  • 576
  • 7
  • 27
  • Have you tried replacing localhost with 20.40.32.14? Can you ping the address? – Robert Jacobs Jul 28 '15 at 18:14
  • To be honest, I didn't have time to try it yet. I thought subnet IP address isn't enough to route packet, is it? – kostr22 Jul 28 '15 at 18:54
  • Normally you don't broadcast SNMP traffic. If you have more than one SNMP Manager for an Agent, it's customary to target one trap packet at each of the managers. I'm sure it would work for traps, though. – Jolta Jul 31 '15 at 15:59

1 Answers1

0

This is not SNMP specific. Probably more relevant to IP routing.

As you get IP connectivity between hosts, just put 20.40.34.14 into UdpTransportTarget. That should work alright for as long as UDP/162 packets could reach your Notification Receiver host.

There's also an option of sending broadcast TRAPs.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21