我正在尝试编写简单的 python 应用程序来发送 SNMP 陷阱。我已经编写了 MIB 表并将陷阱发送到 localhost 工作正常。
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)
现在我需要修改代码以将陷阱发送到专用子网。
我有它的 IP 地址、子网掩码和网关 IP。假设:
IP:20.40.34.14
子网掩码:255.255.255.224
网关:20.40.34.10
有没有办法通过适当的ntforg.UdpTransportTarget(...)
论据来解决这个问题?我查找了这个类 ( target.py
) 的源代码,它在内部使用:
socket.getaddrinfo(transportAddr[0], # localhost in example
transportAddr[1], # 162 in example
socket.AF_INET,
socket.SOCK_DGRAM,
socket.IPPROTO_UDP)[0][4][:2]