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]